When learning the activity life cycle, I hope to test the difference between onPause() and onStop() through the Dialog theme. When clicking the button to jump to the Activity, an error is reported:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.activitylifecycletest, PID: 2920 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activitylifecycletest/com.example.activitylifecycletest.DialogActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:347) at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:316) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:281) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.example.activitylifecycletest.DialogActivity.onCreate(DialogActivity.java:11) at android.app.Activity.performCreate(Activity.java:6664) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Find the most useful sentence, indicating that the theme used does not match the current Activity:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
The activity configuration in AndroidManifest.xml is as follows:
1 <activity 2 android:name=".DialogActivity" 3 android:theme="@android:style/Theme.Dialog"> 4 </activity>
Change the theme to AppCompat according to the error message:
1 <activity 2 android:name=".DialogActivity" 3 android:theme="@style/Theme.AppCompat.Dialog"> 4 </activity>
After checking the java code, I found that my Activity inherits the AppCompatActivity class. When creating an Activity in Android Studio, if you check the Backwards Compatibility (AppCompat) option, the created Activity will inherit AppCompatActivity instead of Activity, and you need to use the corresponding AppCompat theme.
java code:
1 import android.support.v7.app.AppCompatActivity; 2 import android.os.Bundle; 3 4 public class DialogActivity extends AppCompatActivity { 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_dialog); 10 } 11 12 }
Read More:
- How to Solve Error: Android java.lang.IllegalStateException: Could not execute method of the activity
- How to Solve Error inflating class android.support.design.widget.FloatingActionButton
- [Solved] Fragment not attached to an activity
- [Solved] Error in inherits(x, “theme”): argument “e2” is missing, with no default
- Template Render Error Solution (Hexo blog theme, GIT upload)
- [Solved] Could not identify launch activity: Default Activity not found Error while Launching activity
- Git: How to Solve Error: Please commit your changes or stash them before you merge.
- [Solved] Could not identify launch activity: Default Activity not found Error while Launching activity
- Could not identify launch activity: Default Activity not foundError while Launching activity
- Solve the VUE startup problem (You may use special comments to disable some warnings)
- [Solved] Android DataBinding Error: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 fragment Changes to Fragm
- How to Solve Jetpack room use error
- How to Solve VMware Workstation Error: This virtual machine appears to be in use.
- [Solved] JNI Error: Fatal signal 11 (SIGSEGV)
- How to Solve canal & MYSQL or “Kafka cannot consume data” Error
- [Solved] Android9.0 App Install Android4.4 Error: InflateException-You must supply a layout_height attribute.
- How to Solve AOP error in Spring
- How to Solve mount error(6): No such device or address
- [Solved] Manifest merger failed: android:exported needs to be explicitly specified for element <activity#com
- How to Solve Android Error: gps requires ACCESS_FINE_LOCATION