報錯:You need to use a Theme.AppCompat theme (or descendant) with this activity.,theme.appcompat報錯

來源:互聯網
上載者:User

報錯:You need to use a Theme.AppCompat theme (or descendant) with this activity.,theme.appcompat報錯

學習 Activity 生命週期時希望通過 Dialog 主題測試 onPause() 和 onStop() 的區別。

點擊按鈕跳轉 Activity 時報錯:

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)

找到最有用的一句話,說明所使用的 theme 和當前 Activity 不匹配。

You need to use a Theme.AppCompat theme (or descendant) with this activity.

AndroidManifest.xml 中 activity 配置如下:

1 <activity2     android:name=".DialogActivity"3     android:theme="@android:style/Theme.Dialog">4 </activity>

根據報錯提示改為 AppCompat 主題即可:

1 <activity2     android:name=".DialogActivity"3     android:theme="@style/Theme.AppCompat.Dialog">4 </activity>

查看 java 代碼後發現我的 Activity 繼承 AppCompatActivity 類。在 Android Studio 中建立 Activity 時,如果勾選了 Backwards Compatibility(AppCompat) 選項,所建立的 Activity 會繼承 AppCompatActivity 而不是 Activity,此時需要使用配套的APPCompat主題。

java 代碼:

 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 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.