繁体   English   中英

NullPointerException在ActionBar上

[英]NullPointerException on ActionBar

好的,我现在已经阅读了很多有关此错误的信息,以至于我发疯了以下是我的Activity的onCreate

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_event);

    i_name= (EditText)findViewById(R.id.input_event_name);
    i_loc = (EditText)findViewById(R.id.input_event_location);
    i_date = (EditText)findViewById(R.id.input_event_date);
    i_time = (EditText)findViewById(R.id.input_event_time);
    i_desc = (EditText)findViewById(R.id.input_event_description);

    final LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.action_bar_edit_mode, null);

   this.getActionBar().setDisplayShowCustomEnabled(true);
   this.getActionBar().setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

我从这里获取代码,获得2个操作栏按钮。 我在笔记本电脑上浏览了所有内容,现在我想让它在我的PC上正常工作。

现在,我读到一些有关“我的活动中没有动作栏”的信息,并且读到一些有关“我没有标题集”的信息。 在我的笔记本电脑上工作正常。 我尝试设置Feature_action栏。 getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

我也尝试设置另一个主题,但没有任何帮助。

minSdkVersion 11目标SdkVersion 21

logcat的

java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5147)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.test.test2.NewEvent.onCreate(NewEvent.java:92)

任何人都知道发生了什么,还有另一个可以尝试的想法吗?

由于您正在使用ActionBarActivity (来自支持库),因此需要调用support方法:

getSupportActionBar();

编辑-回复评论:

用同样的方式:

getSupportActionBar().setCustomView(view, new ActionBar.LayoutParams(ViewGroup
        .LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

但是,请确保仅从支持库中导入ActionBar:

import android.support.v7.app.ActionBar;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM