应用程序立即关闭,但代码中没有显示错误

时间:2017-03-31 14:20:06

标签: java android xml

我是编程方面的新手,可能有一个简单的问题: 我有一个按钮,当它被点击时,一个新的活动将开始。 Android Studio没有发现任何错误,但是当我启动应用程序时,它会立即崩溃... 这是我的代码

头部活动java:

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

    import java.lang.String;

    public class head_screen extends AppCompatActivity {

        String titletext = "Try Your Math!";

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

        }

        public void start (View view){
            Button btnstart = (Button) findViewById(R.id.btnStart); 
            btnstart.setOnClickListener(new View.OnClickListener() { 
                @Override
                public void onClick(View v) {
                    Intent start = new Intent(head_screen.this, Aufgabe1.class); 
                    startActivity(start); 
                }
            });

            }
    }

头部活动xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#8B5A2B"
    tools:context="com.example.samuel.try_your_math.head_screen"
    >

    <Button
        android:id="@+id/btnStart"
        style="@style/Widget.AppCompat.Button"
        android:layout_width="136dp"
        android:layout_height="78dp"
        android:background="#0F0F0F"
        android:onClick="start"
        android:text="Play"
        android:textAllCaps="true"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textColor="@android:color/background_light"
        android:textSize="30sp"
        android:textStyle="bold|italic"
        tools:layout_editor_absoluteX="124dp"
        tools:layout_editor_absoluteY="244dp"
        tools:text="Play" />
</android.support.constraint.ConstraintLayout>

第二个活动java:

import android.app.Activity;
import android.os.Bundle;

public class Aufgabe1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aufgabe1);

    }
}

第二个活动xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Did it"
        android:layout_marginTop="200dp"
        android:textSize="50dp"/>
</LinearLayout>

清单:

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


    <application
        android:allowBackup="true"
        android:theme="@android:style/Theme.NoTitleBar"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true">

        <activity android:name=".head_screen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

启动应用时,控制台中会显示以下错误代码:

03-31 16:09:49.188 11770-11770/? D/dalvikvm: Late-enabling CheckJNI
03-31 16:09:49.288 11770-11770/com.example.samuel.try_your_math I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
03-31 16:09:49.288 11770-11770/com.example.samuel.try_your_math W/dalvikvm: VFY: unable to resolve interface method 16999: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
03-31 16:09:49.288 11770-11770/com.example.samuel.try_your_math D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math W/dalvikvm: VFY: unable to resolve interface method 17001: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math W/dalvikvm: VFY: unable to resolve interface method 17005: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math W/dalvikvm: VFY: unable to resolve virtual method 462: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math W/dalvikvm: VFY: unable to resolve virtual method 484: Landroid/content/res/TypedArray;.getType (I)I
03-31 16:09:49.298 11770-11770/com.example.samuel.try_your_math D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
03-31 16:09:49.318 11770-11770/com.example.samuel.try_your_math D/AndroidRuntime: Shutting down VM
03-31 16:09:49.318 11770-11770/com.example.samuel.try_your_math W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416fad88)
03-31 16:09:49.318 11770-11770/com.example.samuel.try_your_math E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.samuel.try_your_math, PID: 11770
                                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.samuel.try_your_math/com.example.samuel.try_your_math.head_screen}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
                                                                                      at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                      at android.os.Looper.loop(Looper.java:136)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5212)
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
                                                                                      at dalvik.system.NativeStart.main(Native Method)
                                                                                   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.samuel.try_your_math.head_screen.onCreate(head_screen.java:18)
                                                                                      at android.app.Activity.performCreate(Activity.java:5240)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2216)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301) 
                                                                                      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                      at android.os.Looper.loop(Looper.java:136) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5212) 
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                      at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
                                                                                      at dalvik.system.NativeStart.main(Native Method) 
03-31 16:09:51.438 11770-11770/? I/Process: Sending signal. PID: 11770 SIG: 9

非常感谢,我希望你能帮助我, 最好的祝福, SAMU

0 个答案:

没有答案