不幸的是我的应用程序停止了错

时间:2015-08-04 03:09:45

标签: android

我是Android的一个相对较新的编码器,在编写了这一小段代码之后,它只是要求用户输入并发送消息并在之后显示它。在输入消息后单击“发送”按钮后,显示“不幸的是,应用程序已停止”。

    package com.example.dm4.myapplication;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;

    public class MainActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";

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

    public void sendMessage(View view) {
        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
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
               // openSearch();
                return true;
            case R.id.action_settings:
                //openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

单击“发送”按钮后,这是日志文件中的错误。

  Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
            at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
            at com.example.dm4.myapplication.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:6010)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)

2 个答案:

答案 0 :(得分:0)

style.xml

中创建新主题
 <style name="ActionbarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">

        <!-- API 14 theme customizations can go here. -->
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/ColorPrimary</item>
        <item name="android:titleTextStyle">@style/Theme.MyAppTheme.ActionBar.TitleTextStyle</item>
        <item name="android:icon">@android:color/transparent</item>
    </style>

现在将主题设置为您的活动

 <activity
            android:name="MainActivity"
            android:theme="@style/ActionbarTheme" >
 </activity>

答案 1 :(得分:0)

你可以这样做:

您的Manifest应用程序标签应如下所示:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >

希望这会对你有所帮助。