简体   繁体   中英

Emulator stopped working

I'm very new to the android programming

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

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

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

 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

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

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.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): 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. 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. 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. the auto-generated project in newest Android Studio has it like so:
AndroidManifest.xml

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

styles.xml

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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