Android应用程序在botton点击时崩溃

时间:2017-07-20 14:18:00

标签: android android-intent android-activity

我的活动有三个按钮,每个按钮分别导致三个活动。前两个按钮工作正常,但点击第三个按钮,应用程序崩溃。 gradle完美同步。清单已更新。有人可以指出如何排序这个问题。

活动

package com.kiginfotech.choiceofemperor;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Created by kishore on 01-Jun-17.
 */

public class Choice03 extends MainActivity {


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

        Button button10 = (Button) findViewById(R.id.bt10);
        Button button11 = (Button) findViewById(R.id.bt11);
        Button button12 = (Button) findViewById(R.id.bt12);

        button10.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent10 = new Intent(Choice03.this,choice1103.class);
                startActivity(intent10);
                finish();

            }
        });

        button11.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent11 = new Intent(Choice03.this,Choice1203.class);
                startActivity(intent11);
                finish();

            }
        });


        button12.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent12 = new Intent(Choice03.this,Choice1303.class);
                startActivity(intent12);
                finish();

            }
        });

    }

}

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="vertical"
                  android:paddingBottom="@dimen/activity_vertical_margin"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:scrollbars="vertical"
            tools:ignore="UselessParent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginEnd="20dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp"
                    android:text="@string/choice_03_content"
                    android:textSize="20sp"
                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@android:color/black" />

                <LinearLayout

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"

                    >

                    <Button
                        android:id="@+id/bt10"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@android:color/background_light"
                        android:text="@string/choice_1_1"
                        android:textSize="20sp"/>
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@android:color/black" />

                    <Button
                        android:id="@+id/bt11"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@android:color/background_light"
                        android:text="@string/choice_1_2"
                        android:textSize="20sp"
                        />
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@android:color/black" />

                    <Button
                        android:id="@+id/bt12"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@android:color/background_light"
                        android:text="@string/choice_1_3"
                        android:textSize="20sp"
                        />
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@android:color/black" />

                </LinearLayout>
            </LinearLayout>
        </ScrollView>




    </LinearLayout>

目标活动

package com.kiginfotech.choiceofemperor;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Created by kishore on 07-Jun-17.
 */

public class Choice1303 extends MainActivity{

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

        Button button19 = (Button) findViewById(R.id.bt19);
        Button button20 = (Button) findViewById(R.id.bt20);
        Button button21 = (Button) findViewById(R.id.bt15);

        button19.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent13 = new Intent(Choice1303.this,Choicedeffr1303.class);
                startActivity(intent13);
                finish();

            }
        });

        button20.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent20 = new Intent(Choice1303.this,Choiceecofr1303.class);
                startActivity(intent20);
                finish();

            }
        });

        button21.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent21 = new Intent(Choice1303.this,Choiceadmfr1303.class);
                startActivity(intent21);
                finish();

            }
        });
    }
}

清单

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

    <!-- Include required permissions for Google Mobile Ads to run. -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="com.android.vending.BILLING"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher1"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:largeHeap="true"
        android:fullBackupContent="@xml/backup_descriptor"
        tools:ignore="GoogleAppIndexingWarning">






        <activity
            android:name="com.kiginfotech.choiceofemperor.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".Choice01"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        <activity
            android:name=".Choiceadmfr1202"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".Choice02"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".Choice03"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".choice11"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".Choice12"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        <activity
            android:name=".Choice13"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        <activity
            android:name=".Choice1303"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        <activity
            android:name=".choice1102"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        <activity
            android:name=".choice1103"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.DEFAULT"/>

            </intent-filter>
        </activity>




    </application>

</manifest>

这是logcat。

07-26 18:30:41.652 13842-13842/com.kiginfotech.choiceofemperor D/OpenGLRenderer: drawRect multiDraw AppCompatButton @42251360 paint 0x61f2b268 id 5 style 0 AA 0 color 0xffffffff
07-26 18:30:41.653 13842-13842/com.kiginfotech.choiceofemperor D/OpenGLRenderer: finish <0x617463a8>
07-26 18:30:44.508 13842-13842/com.kiginfotech.choiceofemperor I/View: Touch down dispatch to android.support.v7.widget.AppCompatButton{422526a0 VFED..C. ........ 0,196-656,344 #7f0c0062 app:id/bt12}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=326.50208, y[0]=58.171753, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16574142, downTime=16574142, deviceId=2, source=0x1002 }
07-26 18:30:44.662 13842-13842/com.kiginfotech.choiceofemperor I/View: Touch up dispatch to android.support.v7.widget.AppCompatButton{422526a0 VFED..C. ...p.... 0,196-656,344 #7f0c0062 app:id/bt12}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=326.50208, y[0]=58.171753, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16574293, downTime=16574142, deviceId=2, source=0x1002 }
07-26 18:30:44.663 13842-13842/com.kiginfotech.choiceofemperor V/Provider/Settings:  from settings cache , name = sound_effects_enabled , value = 0
07-26 18:30:44.681 13842-13842/com.kiginfotech.choiceofemperor D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{4223c838 token=android.os.BinderProxy@4223be98 {com.kiginfotech.choiceofemperor/com.kiginfotech.choiceofemperor.Choice03}}
07-26 18:30:44.687 13842-13842/com.kiginfotech.choiceofemperor D/ActivityThread: ACT-PAUSE_ACTIVITY handled : 1 / android.os.BinderProxy@4223be98
07-26 18:30:44.931 13842-13842/com.kiginfotech.choiceofemperor D/AndroidRuntime: Shutting down VM
07-26 18:30:44.931 13842-13842/com.kiginfotech.choiceofemperor W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4193fcf8)
07-26 18:30:44.931 13842-13842/com.kiginfotech.choiceofemperor W/dalvikvm: threadid=1: uncaught exception occurred
07-26 18:30:44.931 13842-13842/com.kiginfotech.choiceofemperor W/System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kiginfotech.choiceofemperor/com.kiginfotech.choiceofemperor.Choice1303}: java.lang.NullPointerException
07-26 18:30:44.932 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)
07-26 18:30:44.932 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
07-26 18:30:44.933 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
07-26 18:30:44.933 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
07-26 18:30:44.933 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:110)
07-26 18:30:44.933 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.os.Looper.loop(Looper.java:193)
07-26 18:30:44.933 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5323)
07-26 18:30:44.933 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 18:30:44.933 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
07-26 18:30:44.933 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
07-26 18:30:44.934 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
07-26 18:30:44.934 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at dalvik.system.NativeStart.main(Native Method)
07-26 18:30:44.934 13842-13842/com.kiginfotech.choiceofemperor W/System.err: Caused by: java.lang.NullPointerException
07-26 18:30:45.039 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at com.kiginfotech.choiceofemperor.Choice1303.onCreate(Choice1303.java:43)
07-26 18:30:45.040 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.app.Activity.performCreate(Activity.java:5343)
07-26 18:30:45.040 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
07-26 18:30:45.040 13842-13842/com.kiginfotech.choiceofemperor W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
07-26 18:30:45.040 13842-13842/com.kiginfotech.choiceofemperor W/System.err:    ... 11 more
07-26 18:30:45.040 13842-13842/com.kiginfotech.choiceofemperor W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
07-26 18:30:45.055 13842-13842/com.kiginfotech.choiceofemperor E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.kiginfotech.choiceofemperor, PID: 13842
                                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kiginfotech.choiceofemperor/com.kiginfotech.choiceofemperor.Choice1303}: java.lang.NullPointerException
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                     at android.os.Looper.loop(Looper.java:193)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5323)
                                                                                     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:828)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                                  Caused by: java.lang.NullPointerException
                                                                                     at com.kiginfotech.choiceofemperor.Choice1303.onCreate(Choice1303.java:43)
                                                                                     at android.app.Activity.performCreate(Activity.java:5343)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419) 
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                     at android.os.Looper.loop(Looper.java:193) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5323) 
                                                                                     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:828) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
                                                                                     at dalvik.system.NativeStart.main(Native Method)  

3 个答案:

答案 0 :(得分:0)

将ID添加到此按钮15 ....您正在引用Button21的按钮R.id.bt15

<Button
          android:id="@+id/bt15"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@android:color/background_light"
          android:text="@string/improve_administration"
          android:textSize="20sp"
                            />

答案 1 :(得分:0)

目标XML中不存在ID为15的按钮。

你应该给bt20下面的按钮id bt21:

<Button
    android:id="@+id/bt21"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:text="@string/improve_administration"
    android:textSize="20sp"/>

并替换以下

Button button21 = (Button) findViewById(R.id.bt15);

Button button21 = (Button) findViewById(R.id.bt21);

答案 2 :(得分:0)

您正在引用两种不同的xml布局

MainActivity:

setContentView(R.layout.choice03);

Choice1303:

setContentView(R.layout.choice1303);

您需要确保使用相同的xml文件或在两个xml文件中使用相同的元素ID。