Android模拟器:不幸的是App停止了

时间:2014-06-14 04:02:48

标签: java android eclipse android-emulator

我是Android编程的新手。我按照Android开发者指南开始使用Android。

当试图在模拟器上运行简单的Hello World应用程序时,模拟器显示"不幸的是,我的第一个应用程序已停止。",其中我的第一个应用程序是应用程序的名称。

这是我的AndroidManifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

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

这是我的主要活动文件:

package com.example.myfirstapp;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

这是LogCat:

06-13 23:56:44.148: I/Process(1209): Sending signal. PID: 1209 SIG: 9
06-13 23:56:45.828: W/dalvikvm(1249): VFY: unable to resolve static field 1559 (ActionBarWindow) in Landroid/support/v7/appcompat/R$styleable;
06-13 23:56:45.828: D/dalvikvm(1249): VFY: replacing opcode 0x62 at 0x0004
06-13 23:56:45.848: D/AndroidRuntime(1249): Shutting down VM
06-13 23:56:45.848: W/dalvikvm(1249): threadid=1: thread exiting with uncaught exception (group=0xb2a6fba8)
06-13 23:56:45.878: E/AndroidRuntime(1249): FATAL EXCEPTION: main
06-13 23:56:45.878: E/AndroidRuntime(1249): Process: com.example.myfirstapp, PID: 1249
06-13 23:56:45.878: E/AndroidRuntime(1249): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:107)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:18)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.app.Activity.performCreate(Activity.java:5231)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.os.Looper.loop(Looper.java:136)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at java.lang.reflect.Method.invokeNative(Native Method)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at java.lang.reflect.Method.invoke(Method.java:515)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-13 23:56:45.878: E/AndroidRuntime(1249):     at dalvik.system.NativeStart.main(Native Method)
06-13 23:56:53.678: I/Process(1249): Sending signal. PID: 1249 SIG: 9

这是activity_main文件:

<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.example.myfirstapp.MainActivity"
    tools:ignore="MergeRootFrame" />

3 个答案:

答案 0 :(得分:3)

将apptheme更改为manifest.xml中的T​​heme.AppCompat

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

这最初起作用,但后来在将主题重新回到AppTheme之后也工作了

答案 1 :(得分:1)

似乎在this SO查询中讨论过相同的问题。基本上,您需要将v7应用程序兼容性JAR文件添加到类路径中。

答案 2 :(得分:0)

这适用于我的摩托罗拉设备。 最初我在我的设备上得到了“不幸的应用程序已经停止”,现在将主题更改为android:theme="@style/Theme.AppCompat"之后,它已经停止了。