-2

I'm honestly so lost right now. I have this simple First aid mobile application thus I have multiple buttons which redirects to multiple activities containing first aid information. In my main activity, i have 5 buttons which all redirect to different activities. Learn Button, Prepare Button, Emergency Button, Maps Button, About us Button. All these buttons work perfectly and redirect to the activities they're supposed to go. (Here is the code)

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);
            }
        });
    }
}

The problem starts when I press the "Learn Button" which its class contains multiple buttons to another set of multiple activities the app keeps crashing. What I also discovered is if there is only one function the code works but if there are multiple the code does not work.

(Learn Class that works) (This Function is set to change to the "bleedactivity" which is one of my other activities)

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);
            }
        });
    }
}

(Learn Class that does not work) (When I try to make all the buttons redirect to their respective activities)

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);
            }
        });
    }
}

I apologize if my question is too long, and thank you if anyone is able to help me.

(Here is the XML File)

<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) 
Komal12
  • 3,340
  • 4
  • 16
  • 25
DXD
  • 3
  • 2
  • Post your log. What is the error you're getting ? – vincrichaud Mar 15 '18 at 16:48
  • The app crashes when i press the button – DXD Mar 15 '18 at 16:50
  • When your app crashes there is an error message prompt in the logcat in Android Studio. Could you post this error ? – vincrichaud Mar 15 '18 at 16:55
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – DXD Mar 15 '18 at 17:02
  • Is this what you're looking for? I dont know if it is. If you want i could screenshot it and post a link here – DXD Mar 15 '18 at 17:02
  • Can you please post your xml? – Abdul Aleem Mar 15 '18 at 17:03
  • That exactly what we are looking for. Could you ad the whole trace to your post ? – vincrichaud Mar 15 '18 at 17:04
  • https://imgur.com/a/0DqqP A screenshot of the whole logcat – DXD Mar 15 '18 at 17:04
  • 1
    Your problem is one of your button is null. Whe you do `findViewById()` your button is not find so its null. Then you call `setListener()` on this button, so the app crashes. – vincrichaud Mar 15 '18 at 17:05
  • This may be a dumb question but, how do i fix it? How do i make the button not null? I'm confused because it seems to work for my main activity – DXD Mar 15 '18 at 17:10
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Christopher Mar 15 '18 at 17:47
  • I'm sorry i read the Thread but I'm still at a loss on what i should do next. I just cant seem to understand why it would work on my Main Activity but no on my Learn Activity, i used the same method to make it exactly. – DXD Mar 15 '18 at 19:21

1 Answers1

1

I have found your issue, you have a simple spelling mistake. replace:
[this should be line 34 of your code according to your error logs]

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

with

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

You have no button with the id 'burnbotton'(which causes the nullpointer exception), thus you were not able to compile your code, if you change the id to 'burnbutton' (which exists in your xml) everything should work as expected.

Karan Shishoo
  • 2,402
  • 2
  • 17
  • 32