简体   繁体   中英

java.lang.NullPointerException: Attempt to invoke virtual method FloatingActionButton.setImageResource(int)' on a null object reference

I am trying to change the image of a floating action button when the app detects an in coming phone call. Here is the java class

public class PhoneCallReceiver extends BroadcastReceiver {

Main2Activity main2Activity;

@Override
public void onReceive(Context context, Intent intent) {

    String state= intent.getStringExtra(TelephonyManager.EXTRA_STATE);


    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

        main2Activity=new Main2Activity();
        main2Activity.pauseWorkout();


    }

}

}

Inside the pauseWorkout method it contains a method call to another method that changes the image of the floating action button, and it is where the exception is pointing at . Here is the method:

public void showPlayButton() {

    //CHANGING FLOATING ACTION BUTTON

    startPauseFAB.setImageResource(R.drawable.play_white);
    startPauseFAB.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.floating_action_button_color_play)));
    startPauseFAB.setRippleColor(getResources().getColor(R.color.myGreen));


}

startPauseFAB.setImageResource(R.drawable.play_white); is where the problem is. In the main2Activity the image changes from play to pause icons with no problem but when a call is ringing the app crushes. Here is how it is intitialized on onCreate:

        startPauseFAB = (FloatingActionButton) findViewById(R.id.startAndPauseFAB);

And here is the xml for the floating action button:

 <android.support.design.widget.FloatingActionButton
                    android:id="@+id/startAndPauseFAB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_margin="16dp"
                    android:src="@drawable/play_white"
                    app:backgroundTint="@color/floating_action_button_color_play"
                    app:borderWidth="0dp"
                    app:elevation="8dp"
                    app:fabSize="normal" />

I tried to use the debugging tool at startPauseFAB.setImageResource(R.drawable.play_white); it says there is no value.

here is the error message :

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.leonk.workouttimerv1, PID: 13567
              java.lang.RuntimeException: Unable to start receiver com.example.leonk.workouttimerv1.classes.PhoneCallReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setImageResource(int)' on a null object reference
                  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2630)
                  at android.app.ActivityThread.access$1700(ActivityThread.java:151)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1387)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5268)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setImageResource(int)' on a null object reference
                  at com.example.leonk.workouttimerv1.Main2Activity.setFABButton(Main2Activity.java:1308)
                  at com.example.leonk.workouttimerv1.classes.PhoneCallReceiver.onReceive(PhoneCallReceiver.java:27)
                  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2623)
                  at android.app.ActivityThread.access$1700(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1387) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5268) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 

How can I solve this problem. I searched through similar questions but the solutions didn't help

In the phoneCallReceiver java class in the onReceive method I add the method call context.sendBroadCast with an intent as follows

public class PhoneCallReceiver extends BroadcastReceiver {





@Override
public void onReceive(Context context, Intent intent) {

    String state= intent.getStringExtra(TelephonyManager.EXTRA_STATE);


    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {



    context.sendBroadcast(new Intent("PHONE_RING"));

    }

}

}

Then on the mainActivity create an object of the phoneCallReceiver class and override the onReceive method then call the activity method to change the image like :

 PhoneCallReceiver phoneCallReceiver=new PhoneCallReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {

        pauseWorkout();

    }
};

Then on onCreate register the receiver like so:

        registerReceiver(phoneCallReceiver,new IntentFilter("PHONE_RING"));

Then destroy :

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(phoneCallReceiver);

}

Solution I got it here : Thanks everyone who commented you helped a lot

Blockquote

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question java.lang.NullPointerException: Attempt to invoke virtual method 'ActionBar.setNavigationMode(int)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddingLeft()' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CardView.setVisibility(int)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getBottom()' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View .MainActivity.findViewById(int)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference in oncreate method java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference, while its not null Android Studio: java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference [TextView]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM