为什么我不能开始新的活动?

时间:2017-07-19 02:59:06

标签: android android-activity

每当我试图点击按钮时,它就会停止工作 这是日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blogspot.hlowrold.mybooks/com.blogspot.hlowrold.mybooks.Main2Activity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
  at android.app.ActivityThread.access$600(ActivityThread.java:130)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4745)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  at dalvik.system.NativeStart.main(Native Method)
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:356)
  at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:325)
  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:286)
  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
  at com.blogspot.hlowrold.mybooks.Main2Activity.onCreate(Main2Activity.java:13)
  at android.app.Activity.performCreate(Activity.java:5008)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
  at android.os.Handler.dispatchMessage(Handler.java:99) 
  at android.os.Looper.loop(Looper.java:137) 
  at android.app.ActivityThread.main(ActivityThread.java:4745) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:511) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
  at dalvik.system.NativeStart.main(Native Method) 

这是什么问题?请帮帮我。

3 个答案:

答案 0 :(得分:1)

  

引起:java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)

示例 styles.xml:

<style name="ThemeCompatLight" parent="Theme.AppCompat.Light.NoActionBar">
</style>

的AndroidManifest.xml

<activity
        android:name=".your.activity.name"
        android:theme="@style/ThemeCompatLight" />

答案 1 :(得分:0)

制作你的style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

答案 2 :(得分:0)

您需要将appcompat lib添加到build.gradle

compile 'com.android.support:appcompat-v7:25.3.1'

并在styles.xml中添加主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"/>

并在清单文件中,以这种方式执行活动

<activity
    android:name="YOUR_ACTIVITY"
    android:theme="@style/AppTheme" />