android – setSupportActionBar()抛出Nullpointer异常

前端之家收集整理的这篇文章主要介绍了android – setSupportActionBar()抛出Nullpointer异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 android新手,我正在关注Material Design Toolbar的以下教程:
http://www.android4devs.com/2014/12/how-to-make-material-design-app.html

但是在执行代码之后. logcat中显示以下错误

04-01 19:16:10.214    2246-2253/com.example.bhaskar.ddit_results E/art﹕ Failed sending reply to debugger: Broken pipe
04-01 19:16:11.985    2246-2246/com.example.bhaskar.ddit_results E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.bhaskar.ddit_results,PID: 2246
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bhaskar.ddit_results/com.example.bhaskar.ddit_results.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
        at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:95)
        at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:88)
        at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:84)
        at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:175)
        at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
        at com.example.bhaskar.ddit_results.MainActivity.onCreate(MainActivity.java:41)
        at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)   
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)    
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

这是由NullPointerException引起的.我正在使用Android Studio 1.0.1.

styles.xml:

<resources>

<!-- Base application theme. -->
<style name="MyTheme" parent="MyTheme.Base">

</style>

<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">#ff32cd32</item>
    <item name="colorPrimaryDark">#ff04cd2c</item>
    <item name="colorAccent">#ff4081</item>
</style>

V21 / styles.xml:

<resources>
<style name="MyTheme" parent="MyTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>

在toolbar.xml中我有:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>

我还在activity_main.xml文件中包含了工具栏布局.
在MainActivity.java中:

public class MainActivity extends ActionBarActivity {

....



private Toolbar toolbar;
public static final int RECORD_ENTRY_REQUEST_CODE=3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set our view from the "main" layout resource

     toolbar = (Toolbar)findViewById(R.id.toolbar);

    //Toolbar will now take on default actionbar characteristics
    setContentView(R.layout.activity_main);
    setSupportActionBar(toolbar);
    setTitle("DDIT_Results");
    ....

}

搜索了这个问题并且没有提出任何解决方案.希望有人认识到这个错误并且有一个快速的答案 – 否则我可以提供更多的信息,如果要求.任何帮助将是appriciated.

解决方法

您需要在添加任何视图(如工具栏)之前调用setContentView.
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);         // <-- call this first

    toolbar = (Toolbar) findViewById(R.id.toolbar); // <-- setup views after

    setSupportActionBar(toolbar);
    setTitle("DDIT_Results");
}

请参阅官方Android开发者博客http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

行动吧

To use Toolbar as an Action Bar,first disable the decor-provided
Action Bar. The easiest way is to have your theme extend from
Theme.AppCompat.NoActionBar (or its light variant).

Second,create a Toolbar instance,usually via your layout XML:

<android.support.v7.widget.Toolbar
    android:id=”@+id/my_awesome_toolbar”
    android:layout_height=”wrap_content”
    android:layout_width=”match_parent”
    android:minHeight=”?attr/actionBarSize”
    android:background=”?attr/colorPrimary” />

The height,width,background,and so on are totally up to you; these
are just good examples. As Toolbar is just a ViewGroup,you can style
and position it however you want.

Then in your Activity or Fragment,set the Toolbar to act as your
Action Bar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blah);

    final Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
}

From this point on,all menu items are displayed in your Toolbar,populated via the standard options menu callbacks.

猜你在找的Android相关文章