0

我从 Activity 更改为 AppCompatActivity。

将此添加到gradle:

dependencies {
    compile "com.android.support:appcompat-v7:23.0.0"
}

但我的应用程序崩溃了。更改的原因是我想编辑我的操作栏。例如:添加徽标等。

日志猫:

    02-22 15:02:22.780 2065-2065/com.example.hansbaum.calculator I/art: Not late-enabling -Xcheck:jni (already on)
02-22 15:02:22.880 2065-2065/com.example.hansbaum.calculator D/AndroidRuntime: Shutting down VM
02-22 15:02:22.880 2065-2065/com.example.hansbaum.calculator E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: com.example.hansbaum.calculator, PID: 2065
                                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hansbaum.calculator/com.example.hansbaum.calculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' 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 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
                                                                                            at com.example.hansbaum.calculator.MainActivity.onCreate(MainActivity.java:53)
                                                                                            at android.app.Activity.performCreate(Activity.java:5937)
                                                                                            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) 
02-22 15:02:53.210 2065-2065/? I/Process: Sending signal. PID: 2065 SIG: 9

显现:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

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

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

</manifest>
4

1 回答 1

3

在您的清单文件中,您需要为您的活动使用 AppCompat 主题。例如:

android:theme="@style/Theme.AppCompat.Light"

在清单中为您的活动添加主题:

<activity
            android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.Light"
            android:label="@string/app_name" >

同样在您的活动代码中,您现在应该使用getSupportActionBar()而不是 getActionBar()

在您的 styles.xml 中替换 <style name="AppTheme" parent="Theme.AppCompat.Light"><style name="AppTheme" parent="@style/Theme.AppCompat.Light">并在您的清单中替换android:theme="@style/Theme.AppCompat.Light"android:theme="@style/AppTheme"

于 2016-02-22T14:42:19.060 回答