Android应用程序因多个按钮重定向到其他活动而不断崩溃

时间:2018-03-15 16:46:39

标签: android android-studio

我老实说现在很丢失。我有这个简单的急救移动应用程序,因此我有多个按钮,重定向到包含急救信息的多个活动。在我的主要活动中,我有5个按钮,所有按钮都重定向到不同的活动。学习按钮,准备按钮,紧急按钮,地图按钮,关于我们按钮。所有这些按钮都可以完美地工作,并重定向到他们应该去的活动。 (这是代码)

package com.example.aaron.myfirstaid;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

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

        Button learnbutton = (Button) findViewById(R.id.learnbutton);
        learnbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, LearnActivity.class);
                startActivity(intent);
            }
        });

        Button prepbutton = (Button) findViewById(R.id.preparebutton);
        prepbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(MainActivity.this, PrepareActivity.class);
                startActivity(intent1);
            }
        });

        Button emerbutton = (Button) findViewById(R.id.emergencybutton);
        emerbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent(MainActivity.this, EmergencyActivity.class);
                startActivity(intent2);
            }
        });

        Button aboutbutton = (Button) findViewById(R.id.aboutus);
        aboutbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent3 = new Intent(MainActivity.this, AboutUsActivity.class);
                startActivity(intent3);
            }
        });

        ImageButton myButton = (ImageButton) findViewById(R.id.mapbutton);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent4 = new Intent(MainActivity.this, MapsActivity.class);
                startActivity(intent4);
            }
        });
    }
}

问题开始时,我按下“学习按钮”,其中包含多个按钮,其中包含多个按钮,应用程序不断崩溃。我还发现,如果代码只有一个函数可以工作,但如果有多个代码不起作用。

(学习班级有效) (此函数设置为更改为“bleedactivity”,这是我的其他活动之一)

package com.example.aaron.myfirstaid;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class LearnActivity extends AppCompatActivity {

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

        Button bleedbutton = (Button) findViewById(R.id.bleedbutton);
        bleedbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LearnActivity.this, bleedactivity.class);
                startActivity(intent);
            }
        });
    }
}

(学习不起作用的课程) (当我尝试将所有按钮重定向到各自的活动时)

package com.example.aaron.myfirstaid;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class LearnActivity extends AppCompatActivity {

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

        Button bleedbutton = (Button) findViewById(R.id.bleedbutton);
        bleedbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LearnActivity.this, bleedactivity.class);
                startActivity(intent);
            }
        });

        Button burnbutton = (Button) findViewById(R.id.burnbotton);
        burnbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent(LearnActivity.this, burnactivity.class);
                startActivity(intent2);
            }
        });

        Button chokebutton = (Button) findViewById(R.id.chokebutton);
        chokebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent3 = new Intent(LearnActivity.this, chokeactivity.class);
                startActivity(intent3);
            }
        });

        Button poisonbutton = (Button) findViewById(R.id.poisonbutton);
        poisonbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent10 = new Intent(LearnActivity.this, poisonactivity.class);
                startActivity(intent10);
            }
        });

        Button allergybutton = (Button) findViewById(R.id.allergybutton);
        allergybutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent4 = new Intent(LearnActivity.this, allergiesactivity.class);
                startActivity(intent4);
            }
        });

        Button asthmabutton = (Button) findViewById(R.id.asthmabutton);
        asthmabutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent5 = new Intent(LearnActivity.this, asthmaactivity.class);
                startActivity(intent5);
            }
        });

        Button bonesbutton = (Button) findViewById(R.id.bonesbutton);
        bonesbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent6 = new Intent(LearnActivity.this, bleedactivity.class);
                startActivity(intent6);
            }
        });

        Button diabetesbutton = (Button) findViewById(R.id.diabetesbutton);
        diabetesbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent7 = new Intent(LearnActivity.this, diabetesactivity.class);
                startActivity(intent7);
            }
        });
        Button distressbutton = (Button) findViewById(R.id.distressbutton);
        distressbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent8 = new Intent(LearnActivity.this, distressactivity.class);
                startActivity(intent8);
            }
        });
        Button heatbutton = (Button) findViewById(R.id.heatbutton);
        heatbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent9 = new Intent(LearnActivity.this, heatstrokeactivity.class);
                startActivity(intent9);
            }
        });
    }
}

如果我的问题太长,我道歉,如果有人能帮助我,我会感谢你。

(这是XML文件)

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/bleedbutton"
            style="@style/Widget.AppCompat.Button"
            android:layout_width="135dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:drawableTop="@drawable/drop"
            android:text="Bleeding" />

        <Button
            android:id="@+id/burnbutton"
            style="@style/Widget.AppCompat.Button"
            android:layout_width="135dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableTop="@drawable/rsz_fire"
            android:text="Burns" />

        <Button
            android:id="@+id/chokebutton"
            android:layout_width="135dp"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/chokingicon"
            android:layout_weight="1"
            android:text="Choking" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/poisonbutton"
            style="@style/Widget.AppCompat.Button"
            android:layout_width="135dp"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/poisonicon"
            android:layout_weight="1"
            android:text="Poisonus Substances" />

        <Button
            android:id="@+id/allergybutton"
            android:layout_width="135dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:drawableTop="@drawable/pea"
            android:text="Allergies" />

        <Button
            android:id="@+id/asthmabutton"
            android:layout_width="135dp"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/asthmaicon"
            android:layout_weight="1"
            android:text="Asthma Attack" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/bonesbutton"
            android:layout_width="135dp"
            android:layout_height="100dp"
            android:drawableTop="@drawable/brokenboneicon"
            android:layout_weight="1"
            android:text="Broken Bones" />

        <Button
            android:id="@+id/diabetesbutton"
            android:layout_width="135dp"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/diabetesicon"
            android:layout_weight="1"
            android:text="Diabetes" />

        <Button
            android:id="@+id/distressbutton"
            android:layout_width="135dp"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/sadicon"
            android:layout_weight="1"
            android:text="Distress" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/heartbutton"
            android:layout_width="135dp"
            android:layout_height="100dp"
            android:drawableTop="@drawable/heartattackicon"
            android:layout_weight="1"
            android:text="Heart Attack" />

        <Button
            android:id="@+id/heatbutton"
            android:layout_width="135dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Heat Stroke" />

        <Button
            android:id="@+id/headbutton"
            android:layout_width="136dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Head Injury" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <Button
            android:id="@+id/hypobutton"
            android:layout_width="135dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:text="Hypothermia" />

        <Button
            android:id="@+id/seizurebutton"
            android:layout_width="135dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:text="Seizures" />

        <Button
            android:id="@+id/shockbutton"
            android:layout_width="135dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:text="Shock" />
    </LinearLayout>
</LinearLayout>

</ScrollView>

(LOGCAT ERROR)

03-16 01:38:22.716 25930-25930/com.example.aaron.myfirstaid E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.aaron.myfirstaid, PID: 25930
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aaron.myfirstaid/com.example.aaron.myfirstaid.LearnActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
          at android.app.ActivityThread.-wrap12(ActivityThread.java)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:159)
          at android.app.ActivityThread.main(ActivityThread.java:6139)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
          at com.example.aaron.myfirstaid.LearnActivity.onCreate(LearnActivity.java:34)
          at android.app.Activity.performCreate(Activity.java:6760)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:159) 
          at android.app.ActivityThread.main(ActivityThread.java:6139) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

1 个答案:

答案 0 :(得分:1)

我找到了你的问题,你有一个简单的拼写错误。更换:
[根据您的错误日志,这应该是您的代码的第34行]

Button burnbutton = (Button) findViewById(R.id.burnbotton);  

Button burnbutton = (Button) findViewById(R.id.burnbutton);  

你没有带有id&#39; burnbotton&#39;(导致nullpointer异常)的按钮,因此如果你将id改为&#39; burnbutton&#39;那么你就无法编译你的代码。 (它存在于你的xml中)一切都应该按预期工作。