从Activity更改为AppCompatActivity

时间:2016-02-22 14:25:07

标签: java android xml android-appcompat appcompatactivity

我从Activity更改为AppCompatActivity。

将此添加到gradle:

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

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

logcat的:

    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>

1 个答案:

答案 0 :(得分:2)

在您的清单文件中,您需要为您的活动使用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"