I want to display a popup menu if a user clicks on my Imageview. Either i get an IllegalStateException or the Code won't Compile

断了今生、忘了曾经 提交于 2019-12-04 21:09:48

You declared your ImageView at the wrong place, try this:

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;
    }
}

Your onClick event will also not work, like the others mentioned, you made a spelling mistake.

@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;
    }
}

The following line in your code is the culprit

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

You can not call findViewById() in static context

You should define ImageView imageView; in global context. but then assign value inside some non-static method, most appropriate would be onCreate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!