简体   繁体   English

模拟器停止工作

[英]Emulator stopped working

I'm very new to the android programming 我对android编程很陌生

I'm trying to execute the app which I was writing from here , 我正在尝试执行从这里编写的应用程序,

but I can't able to run the app. 但我无法运行该应用。 When I'm trying to run the app, the emulator shows 当我尝试运行应用程序时,模拟器会显示

the Abc(app name) suddenly stopped working Abc(应用名称)突然停止工作

activity_main.xml 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"
    />
</LinearLayout>

MainActivity.java MainActivity.java

package com.xyz.abc;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

private static final String EXTRA_MESSAGE = "com.xyz.Abc.MESSAGE";


/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button

    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

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


@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;
}

}

DisplayMessageActivity.java DisplayMessageActivity.java

 package com.xyz.abc;

 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 DisplayMessageActivity extends ActionBarActivity {

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

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

@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_display_message,
                  container, false);
          return rootView;
    }
}
}

fragment_display_message.xml fragment_display_message.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="com.xyz.abc.DisplayMessageActivity$PlaceholderFragment" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/edit_message" />

</LinearLayout>

below is the logcat output for error. 以下是错误的logcat输出。

05-07 06:11:21.004: D/AndroidRuntime(1143): Shutting down VM 05-07 06:11:21.004: W/dalvikvm(1143): threadid=1: thread exiting with uncaught exception (group=0xb2aabba8) 05-07 06:11:21.134: E/AndroidRuntime(1143): FATAL EXCEPTION: main 05-07 06:11:21.134: E/AndroidRuntime(1143): Process: com.xyz.abc, PID: 1143 05-07 06:11:21.134: E/AndroidRuntime(1143): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xyz.abc/com.xyz.abc.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 05-07 06:11:21.004:D / AndroidRuntime(1143):关闭VM 05-07 06:11:21.004:W / dalvikvm(1143):threadid = 1:线程退出且未捕获异常(group = 0xb2aabba8)05 -07 06:11:21.134:E / AndroidRuntime(1143):致命异常:主05-07 06:11:21.134:E / AndroidRuntime(1143):进程:com.xyz.abc,PID:1143 05-07 06 :11:21.134:E / AndroidRuntime(1143):java.lang.RuntimeException:无法启动活动ComponentInfo {com.xyz.abc/com.xyz.abc.MainActivity}:java.lang.IllegalStateException:您需要使用此活动的Theme.AppCompat主题(或后代)。 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.app.ActivityThread.access$800(ActivityThread.java:135) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.os.Handler.dispatchMessage(Handler.java:102) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.os.Looper.loop(Looper.java:136) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.app.ActivityThread.main(ActivityThread.java:5017) 05-07 06:11:21.134: E/AndroidRuntime(1143): at java.lang.reflect.Method.invokeNative(Native Method) 05-07 06:11:21.134: E/AndroidRuntime(1143): at java.lang.reflect.Method.invoke(Method.java:515) 05-07 06:11:21.134: E/AndroidRuntime(1143): 05-07 06:11:21.134:E / AndroidRuntime(1143):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)05-07 06:11:21.134:E / AndroidRuntime(1143):位于android。 app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)05-07 06:11:21.134:E / AndroidRuntime(1143):at android.app.ActivityThread.access $ 800(ActivityThread.java:135)05-07 06:11 :21.134:E / AndroidRuntime(1143):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)05-07 06:11:21.134:E / AndroidRuntime(1143):在android.os.Handler dispatchMessage(Handler.java:102)05-07 06:11:21.134:E / AndroidRuntime(1143):at android.os.Looper.loop(Looper.java:136)05-07 06:11:21.134:E / AndroidRuntime(1143):位于android.app.ActivityThread.main(ActivityThread.java:5017)05-07 06:11:21.134:E / AndroidRuntime(1143):位于java.lang.reflect.Method.invokeNative(本机方法) 05-07 06:11:21.134:E / AndroidRuntime(1143):at java.lang.reflect.Method.invoke(Method.java:515)05-07 06:11:21.134:E / AndroidRuntime(1143): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 05-07 06:11:21.134: E/AndroidRuntime(1143): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-07 06:11:21.134: E/AndroidRuntime(1143): at dalvik.system.NativeStart.main(Native Method) 05-07 06:11:21.134: E/AndroidRuntime(1143): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)05-07 06:11:21.134:E / AndroidRuntime(1143):在com.android.internal.os.ZygoteInit.main( ZygoteInit.java:595)05-07 06:11:21.134:E / AndroidRuntime(1143):at dalvik.system.NativeStart.main(Native Method)05-07 06:11:21.134:E / AndroidRuntime(1143):引起原因:java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98) 05-07 06:11:21.134: E/AndroidRuntime(1143): at com.xyz.abc.MainActivity.onCreate(MainActivity.java:29) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.app.Activity.performCreate(Activity.java:5231) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 05-07 06:11:21.134: E/AndroidRuntime(1143): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 05-07 06:11:21.134: E/AndroidRuntime(1143): ... 11 more 05-07 06:11:29.494: I/Process(1143): Sending signal. 05-07 06:11:21.134:E / AndroidRuntime(1143):位于android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108)05-07 06:11:21.134:E / AndroidRuntime(1143) :位于android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)05-07 06:11:21.134:E / AndroidRuntime(1143):位于android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity .java:98)05-07 06:11:21.134:E / AndroidRuntime(1143):位于com.xyz.abc.MainActivity.onCreate(MainActivity.java:29)05-07 06:11:21.134:E / AndroidRuntime (1143):位于android.app.Activity.performCreate(Activity.java:5231)05-07 06:11:21.134:E / AndroidRuntime(1143):位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 05-07 06:11:21.134:E / AndroidRuntime(1143):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)05-07 06:11:21.134:E / AndroidRuntime(1143):... 11年5月7日06:11:29.494:I / Process(1143):正在发送信号。 PID: 1143 SIG: 9 PID:1143 SIG:9

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

This is the reason that your app crashes. 这就是您的应用程序崩溃的原因。 Look at your manifest, find the theme of your activity or application and ensure that it extends some AppCompat theme, for ex. 查看清单,找到活动或应用程序的主题,并确保它扩展了某些AppCompat主题,例如。 the auto-generated project in newest Android Studio has it like so: 最新的Android Studio中自动生成的项目如下所示:
AndroidManifest.xml AndroidManifest.xml

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

styles.xml styles.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
       ...
 </style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM