2016-09-26 58 views
-1

error_log中:同時應用Material_Theme錯誤:無法啓動活動ComponentInfo

D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.hetal13.material_design_demo, PID: 2611 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hetal13.material_design_demo/com.example.hetal13.material_design_demo.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343) at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.example.hetal13.material_design_demo.MainActivity.onCreate(MainActivity.java:13) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  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:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  Application terminated.

Style.xml:

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="android:Theme.Material.Light"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

</resources> 

MainActivity.java:

package com.example.hetal13.material_design_demo; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 



public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 
} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 

    android:orientation="vertical" 
    tools:context="com.example.hetal13.material_design_demo.MainActivity"> 

    <Button 
     android:text="@string/button_submit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="46dp" 
     android:id="@+id/button" 
     /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:ems="10" 
     android:layout_marginTop="108dp" 
     android:id="@+id/editText" 

     android:textAlignment="center" 
     tools:ignore="LabelFor" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 
</LinearLayout> 

回答

0

您需要做以下更改您的Style.xml。 (你甚至可以嘗試複製粘貼此代碼段。最有可能它應該工作。)

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

所有你需要的是根據你的logcat Theme.AppCompat主題

希望這有助於

+0

但是做什麼,如果我想補充材料設計的UI – Hetal1311

+0

**這僅僅是材料的主題!**只是向後兼容。我想你只是開始Android開發,所以我建議你看看官方的谷歌文檔,以更好地理解。 **我們應該使用兼容主題,以便預棒棒糖設備也可以獲得一些材質。 ** –

+0

我強烈建議您[閱讀此鏈接](https://developer.android.com/training/material/compatibility.html)。如果你覺得它有用,請考慮upvoting和接受我的答案:) –

相關問題