【问题标题】:NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference [duplicate]NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.View android.widget.RelativeLayout.findViewById(int)”[重复]
【发布时间】:2015-09-17 08:34:14
【问题描述】:

我试图在操作栏中放置一个通知计数器,它在onCreateOptionsMenu 方法中给出了这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference
        at ro.oce.store.MainActivity.onCreateOptionsMenu(MainActivity.java:112)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2823)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:262)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:241)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
        at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448)
        at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:65)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

代码如下:

 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.shopping_cart).getActionView();
    TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
    tv.setText("12");
    return true;
}

在我添加最后 3 行代码之前,我的应用程序没有产生任何错误。有人可以帮帮我吗?

【问题讨论】:

  • 一件事如何在 menu.xml 中拥有RelativeLayout
  • 这种方式永远行不通
  • @sourabhbans 这不起作用。它说不兼容的类型Required: android.view.ViewFound: void
  • 您想在操作栏中的哪个位置放置通知计数器?还是在选项菜单里面?
  • @sourabhbans 菜单内

标签: java android nullpointerexception


【解决方案1】:

两个建议:

  • 通过id查找时去掉“badgeLayout” - (TextView)findViewById();

  • 检查布局不为空,错误“空对象引用”,表示调用实例为空。所以在这种情况下,“badgeLayout”为空。执行以下操作:

    -> 检查您引用的布局 xml 是否存在。

    -> 删除getActionView()

【讨论】:

  • 我尝试了第二个建议,它给了我这个例外ClassCastException: android.support.v7.internal.view.menu.MenuItemImpl cannot be cast to android.widget.RelativeLayout。第一个建议给了我另一个例外:NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
猜你喜欢
  • 1970-01-01
  • 2022-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-24
相关资源
最近更新 更多