ImageButton中的OpenOptionMenu给出了空指针异常

时间:2016-07-19 06:43:15

标签: android contextmenu menuitem optionmenu inflate-exception

我试图在onClick侦听器中从ImageButton调用optionMenu,但是会遇到以下异常。

07-19 01:08:09.693: E/AndroidRuntime(15133): java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.getItem(int)' on a null object reference


    public class myApp extends Activity  implements  OnClickListener {

      @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

       }

    /** Called when the activity is first created. */
    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        setContentView(R.layout.main);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

        menu_icon  = (ImageView) findViewById(R.id.icon);
        infoBtn = (ImageButton) findViewById(R.id.pfkey_button);
        menu_icon.setImageResource(R.drawable.info_icon);

        menu_icon.setOnClickListener(this);
        infoBtn.setOnClickListener(this);

        //Create Terminal View
        terminalView = (TerminalView)findViewById(R.id.terminalView);//new TerminalView(this);     

        registerForContextMenu(terminalView);

    }


    public void onClick(View v) {


            KeyEvent event = null;

            if (v == infoBtn) {
                openOptionsMenu();
            }

            if (v == menu_icon) {
                openOptionsMenu();
            }

        }

}

我也试过`infoBtn =(ImageButton)myApp.this.findViewById(R.id.pfkey_button);但没有工作

3 个答案:

答案 0 :(得分:1)

我不知道为什么你压倒 onPostCreate ,我想你会在 onCreate 中做那些事。

除此之外, openOptionsMenu 在我的应用中无效。

所以我使用工具栏来替换 ActionBar 。这是代码:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mToolbar.showOverflowMenu();
            }
        });
    }

    // override this method to inflat the menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(TAG, "onOptionsItemSelected: "+item.getTitle().toString());
        return super.onOptionsItemSelected(item);
    }
}

答案 1 :(得分:0)

确保首先在onCreateOptionsMenu()

内对菜单进行充气
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the navigation_menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.your_menu, menu);
    return true;
}

菜单本身可能没有膨胀,从而导致异常。如果不是这样,请提供完整的代码

答案 2 :(得分:0)

虽然实施是正确的,但它崩溃了。

@Override
    public boolean onMenuOpened(int featureId, Menu menu) {

        /*if (!hostcom.isConnected())
            menu.getItem(0).setTitle("Login");
        else
            menu.getItem(0).setTitle("Logout");*/

        return true;

    }