如果用户点击我的popup menu
,我想显示Imageview
。我得到IllegalStateException
或代码不会编译。
为此,我在我宣布NullPointerException
的行上获得Imageview
。但是当我实现它的方法时,代码将无法编译。
onCreateOptions
也无效。请纠正错误。提前致谢
@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 :(得分: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;
}
}
答案 1 :(得分:0)
代码中的以下行是罪魁祸首
ImageView imageView = (ImageView) findViewById(R.id.aaa);
您不能在静态上下文中调用findViewById()
您应该定义ImageView imageView;在全球范围内。但是在某些非静态方法中分配值,最合适的方法是onCreate。