2015-01-12 97 views
0

我是Android開發人員中的新成員。 I'm執行的應用程序,但it's給我的錯誤:可惜的是, 'APP_NAME' 已停止不幸的是,'app_name'已停止

我的代碼:

MainActivity

public class MainActivity extends ActionBarActivity { 
    private ListView lista; 
    //array de datos 
    private String[] datos = {"Item uno","Item dos","Item tres", "Item cuatro","Item cinco", 
    "Item seis","Item siete","Item ocho","Item nueve","Item diez"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //Referenciamos la lista a la actividad 
     lista = (ListView) findViewById(R.id.lista); 
     //creamos el arrayadapter 
     ArrayAdapter< String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datos); 
     //Vinculamos el adaptador a la lista 
     lista.setAdapter(adapter); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

主要佈局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.cursoandroid.miarrayadapter.MainActivity" 
    tools:ignore="MergeRootFrame" 
    > 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/lista" 
     /> 

清單:

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:theme="@style/AppTheme" 
      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> 

    </FrameLayout> 

enter image description here enter image description here 我的字符串XML是:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">MiarrayAdapter</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="action_settings">Settings</string> 

</resources> 

謝謝。誰能說我,有什麼不對?

文件的logcat:

01-12 08:49:40.101 1253-1253/com.cursoandroid.miarrayadapter E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.cursoandroid.miarrayadapter, PID: 1253 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cursoandroid.miarrayadapter/com.cursoandroid.miarrayadapter.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(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.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
      at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151) 
      at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138) 
      at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123) 
      at com.cursoandroid.miarrayadapter.MainActivity.onCreate(MainActivity.java:16) 
      at android.app.Activity.performCreate(Activity.java:5933) 
      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.access$800(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) 
+0

顯示你的logcat –

+0

你能顯示你的strings.xml文件嗎? – hakiko

+0

是的,我編輯了我的文章。你可以現在就把它 –

回答

0

如果在開發過程中遇到任何異常情況,請首先在堆棧跟蹤中查找此部分「由......引起:」。它顯示了失敗的確切原因。

在這種情況下,它清楚地表明

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 

然後可以很容易地調試你的自我或問的問題更有意義。只是一個建議,因爲你是一個新的開發者

0

在你styles.xml文件,使用程序兼容性主題:

<style name="**YourThemeName**" parent="@style/Theme.AppCompat"> 

</style> 

現在使用這個主題在你的AndroidManifest.xml文件:

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/**YourThemeName**"> 

..... 

</application> 
+0

不要在parent =「@ style/Theme.AppCompat 「>,標記了如何紅色的錯誤 –