android.app.actionbar上的空指针异常

时间:2015-01-21 11:39:55

标签: android

我在运行时收到空指针异常。我在下面的代码中指出了错误行。

我收到此错误:

  

java.lang.RuntimeException:无法启动活动   java.lang.NullPointerException:尝试调用虚方法'void   空对象上的android.app.ActionBar.setHomeButtonEnabled(boolean)'   参考

堆栈跟踪:

01-21 11:12:12.060: E/AndroidRuntime(2102):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-21 11:22:37.975: E/AndroidRuntime(2176): FATAL EXCEPTION: main
01-21 11:22:37.975: E/AndroidRuntime(2176): Process: com.qrme.quranmadeeasy, PID: 2176
01-21 11:22:37.975: E/AndroidRuntime(2176): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.qrme.quranmadeeasy/com.qrme.quranmadeeasy.ChapterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.os.Looper.loop(Looper.java:135)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.app.ActivityThread.main(ActivityThread.java:5221)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at java.lang.reflect.Method.invoke(Native Method)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at java.lang.reflect.Method.invoke(Method.java:372)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-21 11:22:37.975: E/AndroidRuntime(2176): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
01-21 11:22:37.975: E/AndroidRuntime(2176):     at com.qrme.quranmadeeasy.ChapterActivity.onCreate(ChapterActivity.java:64)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.app.Activity.performCreate(Activity.java:5933)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-21 11:22:37.975: E/AndroidRuntime(2176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
01-21 11:22:37.975: E/AndroidRuntime(2176):     ... 10 more

ChapterActivity.java:

public class ChapterActivity extends Activity implements OnItemClickListener {

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_chapter);

        getActionBar().setDisplayHomeAsUpEnabled(true); --->64th line error
        getActionBar().setHomeButtonEnabled(true);

        initialize();
        listChapter.setOnItemClickListener(this);
    }

private void initialize() {

.....
....
ActionBar();
}

public void ActionBar() {
        ActionBar mActionBar = getActionBar();
        mActionBar.setBackgroundDrawable(new ColorDrawable(Color
                .parseColor(white)));
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(this);

        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);

        Typeface type = Typeface.createFromAsset(getAssets(),
                "Roboto-Medium.ttf");

        TextView mTitleTextView = (TextView) mCustomView
                .findViewById(R.id.textviewHeading);
        mTitleTextView.setTypeface(type);

        ImageButton imgbuttonSettings = (ImageButton) mCustomView
                .findViewById(R.id.imagebuttonSettings);
        ImageButton imgbuttonPre = (ImageButton) mCustomView
                .findViewById(R.id.imagebuttonPre);
        imgbuttonPre.setVisibility(View.GONE);
        imgbuttonSettings.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                startActivity(new Intent(ChapterActivity.this,
                        SettingsActivity.class));
            }
        });

        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);
    }
}

清单:

 <application
        android:name="com.qrme.quranmadeeasy.Application"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"

               >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
      ........
   <applicaiton>

任何人都可以帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:0)

从清单文件中删除此行

 android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"

这表示全屏表示没有标题栏或操作栏,因此您正在获得NPE。

修改

同时将此行移至Actionbar()函数

getActionBar().setDisplayHomeAsUpEnabled(true); 
getActionBar().setHomeButtonEnabled(true);

public void ActionBar() {
    ActionBar mActionBar = getActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(true); 
    mActionBar.setHomeButtonEnabled(true);
}