无法在操作栏android

时间:2017-02-26 19:30:45

标签: android nullpointerexception android-actionbar

您好我是Android新手,我正在尝试在每个活动的操作栏中添加徽标和搜索查看。我尝试在我的一个活动上实现徽标但是我收到此错误并且应用程序崩溃了。谁能告诉我如何摆脱这个?还有其他办法可以做我想做的事吗?

我直接从此网站调整了图标大小 https://romannurik.github.io/AndroidAssetStudio/index.html

错误

02-27 00:58:28.859 13851-13851/com.golstars.www.glostars E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.golstars.www.glostars, PID: 13851
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.golstars.www.glostars/com.golstars.www.glostars.MainFeed}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
                                                                               at com.golstars.www.glostars.MainFeed$override.onCreate(MainFeed.java:96)
                                                                               at com.golstars.www.glostars.MainFeed$override.access$dispatch(MainFeed.java)
                                                                               at com.golstars.www.glostars.MainFeed.onCreate(MainFeed.java:0)
                                                                               at android.app.Activity.performCreate(Activity.java:6664)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

my mainfeed onCreate方法

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_feed);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.drawable.ic_action_logoandtext);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

activity_main_feed.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.golstars.www.glostars.MainFeed">



    <include
        layout="@layout/activity_test"

        />

    <include
        layout="@layout/fabs"
        />



</android.support.design.widget.CoordinatorLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.golstars.www.glostars">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:debuggable="true"
        android:supportsRtl="true"
        android:name="android.support.multidex.MultiDexApplication"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainFeed"
            android:label="@string/title_activity_main_feed"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".user_profile"
            android:label="@string/title_activity_user_profile"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Fabs"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".testfeed"
            android:label="@string/title_activity_test"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".notification"
            android:label="@string/title_activity_notification"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".upload"
            android:label="@string/title_activity_upload"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".edit_profile"
            android:label="@string/title_activity_edit_profile"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".competitionUser"
            android:label="@string/title_activity_competition_user"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".competitionAll"
            android:label="@string/title_activity_competition_all"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".imagefullscreen"
            android:label="@string/title_activity_image_full_screen"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".commentModel"
            android:label="@string/title_activity_comment_model"
            android:theme="@style/AppTheme.NoActionBar"/>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:2)

onCreate中,先设置toolbar

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

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_action_logoandtext);
getSupportActionBar().setDisplayUseLogoEnabled(true);