从 Activity 更改为 AppCompatActivity
Change from Activity to 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.access0(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.access0(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>
在您的清单文件中,您需要为您使用 AppCompat 主题 activity。
例如:
android:theme="@style/Theme.AppCompat.Light"
在清单中为您的 activity 添加主题:
<activity
android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light"
android:label="@string/app_name" >
另外,在您的 activity 代码中,您现在应该使用 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"
我从 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.access0(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.access0(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>
在您的清单文件中,您需要为您使用 AppCompat 主题 activity。 例如:
android:theme="@style/Theme.AppCompat.Light"
在清单中为您的 activity 添加主题:
<activity
android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Light"
android:label="@string/app_name" >
另外,在您的 activity 代码中,您现在应该使用 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"