使用getSupportActionBar()时的NullPointerException .setTitlte()

时间:2016-08-02 00:08:21

标签: android

在我的项目中,我正在尝试使用getSupportActionBar().setTitle()更改ActionBar标题,但我不断获得NullPointerException

错误

08-02 06:58:48.645 16441-16441/? E/AndroidRuntime: FATAL EXCEPTION: main
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime: Process: com.mynotescode.apps.layout, PID: 16441
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mynotescode.apps.layout/com.mynotescode.apps.layout.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2326)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:147)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5264)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.mynotescode.apps.layout.MainActivity.onCreate(MainActivity.java:18)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5975)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:147) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5264) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695) 

这是我的MainActivity.java:

package com.mynotescode.apps.layout;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();

        actionBar.setTitle("Hotel Hilton");
        actionBar.setSubtitle("Isla Nublar");
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    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);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_bookmark) {
            return true;
        }

        if (id == android.R.id.home){
            finish();
        }

        return super.onOptionsItemSelected(item);
    }
}

请帮帮我。谢谢。

3 个答案:

答案 0 :(得分:0)

首先,此问题与Android Studio无关。其次,请确保您没有在styles.xml中使用最后包含NoActionBar的任何主题,例如 - Theme.Light.NoActionBar

这是一个例子 -

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

如果有帮助,请告诉我。

答案 1 :(得分:0)

尝试使用工具栏而不是像这样的操作栏,这是来自我正在制作的应用程序的示例:

// Set your content view before this point.
Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolBar);
ActionBar actionBar = getSupportActionBar();
// Call any actionbar methods you like at this point!
actionBar.setHomeAsUpIndicator(R.drawable.back_home_ind);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle((Html.fromHtml("<font color=\"#0288D1\">" + friendName + "</font>")));//By default set the color to grey

另外值得注意的是,如果您的布局中有工具栏,则需要在调用getSupportActionBar()之前将其设置为操作栏,如果您的布局中已有工具栏并且正在使用noActionBar主题,则必须遵循这个例子。

答案 2 :(得分:0)

生成NullPointerException的原因是你缺少某些东西。在您的代码中,您必须缺少actionBar。虽然您的MainActivity扩展了AppCompatActivity,但如果在您的XML文件中使用NoActionBar主题,如Theme.AppCompat.Light.NoActionBar,您将生成NullPointerException。