应用程序一直停止 - 无法启动

时间:2017-06-14 12:32:18

标签: android

Uber noob编程,但已开始尝试使用教程制作一个非常基本的应用程序(https://developer.android.com/training/basics/firstapp/starting-activity.html) 但我遇到了几个路障。我已经尝试过两次这个应用程序并且失败了。一个问题是viewGroup行一旦输入就会一直从代码中消失。第二个问题是应用程序一启动就崩溃了。我有第一个活动最初工作,但点击按钮时应用程序会崩溃。我已经三合会清洁/重建但它根本没有帮助。我也尝试了其他基本信息,我可以编辑或删除代码的某些部分,但他们也没有帮助。所以在这个阶段,任何和所有的帮助将不胜感激!提前谢谢!

Logcat错误信息

 06-14 20:56:40.335 25791-25791/com.example.jacqueline.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.example.jacqueline.myfirstapp, PID: 25791
                                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jacqueline.myfirstapp/com.example.jacqueline.myfirstapp.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2711)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
                                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                           at android.os.Looper.loop(Looper.java:241)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6217)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                                        Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
                                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                                           at com.example.jacqueline.myfirstapp.MainActivity.onCreate(MainActivity.java:15)
                                                                                           at android.app.Activity.performCreate(Activity.java:6705)
                                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2664)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772) 
                                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515) 
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                           at android.os.Looper.loop(Looper.java:241) 
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6217) 
                                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  

主要活动 -

package com.example.jacqueline.myfirstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.jacqueline.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /**called when user taps the send button*/
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.editText);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
    }

    public class EXTRA_MESSAGE {
    }
}  

2 个答案:

答案 0 :(得分:1)

堆栈跟踪清楚地表明

  

您需要将Theme.AppCompat主题(或后代)与此活动一起使用

在您的清单文件中,将Theme.AppCompat.NoActionBar声明为您的主题

<activity android:name=".MainActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>`

答案 1 :(得分:0)

您需要在此活动中提及Theme.AppCompat主题(或后代)。

  1. 首先从项目资源管理器窗口打开您的清单文件。
  2. 为您的活动添加主题。

    <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       android:theme="@style/AppTheme">
                <activity android:name=".MainActivity"
               android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
                        <intent-filter>
                                    <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>