不幸的是,'app_name'已停止

时间:2015-01-12 08:10:05

标签: android

我是android开发人员的新手。我正在执行一个应用程序,但它给了我错误:不幸的是,'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)

3 个答案:

答案 0 :(得分:0)

在styles.xml文件中,使用Appcompat主题:

<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>

答案 1 :(得分:0)

扩展ActionBarActivity,需要Theme.AppCompat

所以请extends ActionBarActivity

替换extends Activity

喜欢这个

android:theme="@style/Theme.AppCompat" >

了解更多信息:

http://developer.android.com/guide/topics/ui/actionbar.html

答案 2 :(得分:0)

开发时如果遇到任何异常,请先查看此部分&#34;引起:&#34;在堆栈跟踪中。它显示了失败的确切原因。

在这种情况下,它清楚地显示

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

然后,您可以轻松调试自己或更有意义地提出问题。只是一个建议,因为你是一个新的开发人员