I want to display a popup menu if a user clicks on

2019-04-17 16:36发布

问题:

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.

For this I am getting a NullPointerException on the line where I've declared Imageview. But when I implement its methods the code won't compile. onCreateOptions isn't working either. Please rectify the error. Thanks in advance

@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" />

回答1:

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.



回答2:

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


回答3:

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.