사용자가 Imageview
을 클릭하면 popup menu
을 표시하려고합니다. 어느 쪽이든 IllegalStateException
또는 코드가 컴파일되지 않습니다.사용자가 내 Imageview를 클릭하면 팝업 메뉴를 표시하려고합니다. 어느 쪽이든 IllegalStateException이 발생하거나 코드가 컴파일되지 않습니다.
NullPointerException
이 Imageview
으로 선언 된 행에 표시됩니다. 그러나 그 메소드를 구현할 때 코드는 컴파일되지 않습니다. 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" />
불일치 이름 * showPupup *, 필수 * showPopup *, 확인할 수 있습니까? –