-2

사용자가 Imageview을 클릭하면 popup menu을 표시하려고합니다. 어느 쪽이든 IllegalStateException 또는 코드가 컴파일되지 않습니다.사용자가 내 Imageview를 클릭하면 팝업 메뉴를 표시하려고합니다. 어느 쪽이든 IllegalStateException이 발생하거나 코드가 컴파일되지 않습니다.

NullPointerExceptionImageview으로 선언 된 행에 표시됩니다. 그러나 그 메소드를 구현할 때 코드는 컴파일되지 않습니다. onCreateOptions도 작동하지 않습니다. 오류를 바로 잡으십시오. 다른 사람이 언급 한 것처럼,

ImageView imageView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); 
    setSupportActionBar(toolbar); 
    // Set the content of the activity to use the activity_main.xml layout file 
    setContentView(R.layout.app); 

    imageView = (ImageView) findViewById(R.id.aaa); 

    // Find the view pager that will allow the user to swipe between fragments 
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 

    // Create an adapter that knows which fragment should be shown on each page 
    WhatsappFragmentPagerAdapter adapter = new WhatsappFragmentPagerAdapter(this, getSupportFragmentManager()); 

    // Set the adapter onto the view pager 
    viewPager.setAdapter(adapter); 

    // Give the TabLayout the  ViewPager 
    TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); 
    tabLayout.setupWithViewPager(viewPager); 
} 


public void showPopup(View v) { 
    PopupMenu popup = new PopupMenu(this, v); 

    // This activity implements OnMenuItemClickListener 
    popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) this); 
    popup.inflate(R.menu.app_menu); 
    popup.show(); 
} 

public boolean onMenuItemClick(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.main: 
      startActivity(new Intent(App.this, App_Main.class)); 
      return true; 
     case R.id.help: 
      startActivity(new Intent(App.this, App_Main.class)); 
      return true; 
     default: 
      return false; 
    } 
} 

귀하의 onClick 이벤트도 작동하지 않습니다 : 사전

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); 
    setSupportActionBar(toolbar); 
    // Set the content of the activity to use the activity_main.xml layout file 
    setContentView(R.layout.app); 

    // Find the view pager that will allow the user to swipe between fragments 
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 

    // Create an adapter that knows which fragment should be shown on each page 
    WhatsappFragmentPagerAdapter adapter = new WhatsappFragmentPagerAdapter(this, getSupportFragmentManager()); 

    // Set the adapter onto the view pager 
    viewPager.setAdapter(adapter); 

    // Give the TabLayout the ViewPager 
    TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); 
    tabLayout.setupWithViewPager(viewPager); 
} 

    ImageView imageView = (ImageView) findViewById(R.id.aaa); 

public void showPupup(View v) { 
    PopupMenu popup = new PopupMenu(this, v); 

    // This activity implements OnMenuItemClickListener 
    popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) this); 
    popup.inflate(R.menu.app_menu); 
    popup.show(); 
} 

public boolean onMenuItemClick(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.main: 
      startActivity(new Intent(App.this, App_Main.class)); 
      return true; 
     case R.id.help: 
      startActivity(new Intent(App.this, App_Main.class)); 
      return true; 
     default: 
      return false; 
    } 
} 

} 감사드립니다

 <ImageView 
     android:id="@+id/aaa" 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:src="@drawable/whatsapp_settings" 
     android:layout_alignParentRight="true" 
     android:layout_marginLeft="10dp" 
     android:onClick="showPopup" /> 
+0

불일치 이름 * showPupup *, 필수 * showPopup *, 확인할 수 있습니까? –

답변

1

당신은 당신의 ImageView 잘못된 장소에서,이 시도 선언 너 맞춤법 실수를 했어.

+0

시도해 보셨습니까? –

+1

방금 ​​"(Popupmenu.OnMenuItemClickListener)"를 제거하고 문제가 없습니다. –

0
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); 
    setSupportActionBar(toolbar); 
    // Set the content of the activity to use the activity_main.xml 
layout file 
    setContentView(R.layout.app); 

    // Find the view pager that will allow the user to swipe between 
fragments 
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 

    // Create an adapter that knows which fragment should be shown on each page 
    WhatsappFragmentPagerAdapter adapter = new 
WhatsappFragmentPagerAdapter(this, getSupportFragmentManager()); 

    // Set the adapter onto the view pager 
    viewPager.setAdapter(adapter); 

    // Give the TabLayout the ViewPager 
    TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); 
    tabLayout.setupWithViewPager(viewPager); 

    ImageView imageView = (ImageView) findViewById(R.id.aaa); 
} 


public void showPupup(View v) { 
    PopupMenu popup = new PopupMenu(this, v); 

    // This activity implements OnMenuItemClickListener 

popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) 
this); 
    popup.inflate(R.menu.app_menu); 
    popup.show(); 
} 

public boolean onMenuItemClick(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.main: 
      startActivity(new Intent(App.this, App_Main.class)); 
      return true; 
     case R.id.help: 
      startActivity(new Intent(App.this, App_Main.class)); 
      return true; 
     default: 
      return false; 
    } 
} 
+0

java.lang.IllegalStateException : 부모 또는 조상에서 showPopup (View) 메서드를 찾을 수 없습니다. onroid : onClick에 대한 컨텍스트가에 정의 됨 id 'aaa'가 포함 된 android.support.v7.widget.AppCompatImageView보기 이 오류가 표시됩니다 ... 오류가 어떤 행에 표시되지 않는지 ... 도와주세요 –

+0

신경 쓰지 마세요 ... 알아 들었습니다. ! 문제가 생겨서 죄송합니다 –

+0

XML 또는 자바의 showPopup 맞춤법을 확인하십시오. 동일해야합니다. –

관련 문제