Android

[안드로이드]Attempt to invoke virtual method ‘void android.widget.ImageView.setBackground(android.graphics.drawable.Drawable)’ on a null object reference

이미지뷰에 이미지를 코딩상에서 처리하는 과정에 오류가 발생하였다.

시간차 때문인가? 이거 간헐적이다.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ImageView.setBackground(android.graphics.drawable.Drawable)’ on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2950)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3085)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6853)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ImageView.setBackground(android.graphics.drawable.Drawable)’ on a null object reference
at ddolcat.app.battery.charge.notification.MainActivity.onCreate(:352)
at android.app.Activity.performCreate(Activity.java:7151)
at android.app.Activity.performCreate(Activity.java:7142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2930)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3085)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6853)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)

 

[오류가 발생한 코드 위치]
ImageView fastBtn = mainLayout.findViewById(R.id.start_btn);
fastBtn.setBackground(getResources().getDrawable(R.drawable.baseline_stop_white_48));

 


[해결방법] if 문으로 null 여부를 체크..
if(fastBtn!= null) {
     fastBtn.setBackground(getResources().getDrawable(R.drawable.baseline_stop_white_48));
}

 

Leave a Reply

error: Content is protected !!