简体   繁体   中英

Android sdk: java.lang.NullPointerException: Attempt to invoke virtual method

I'm getting this error when I try to debug the app on my device and trying to get rid of it in hours now...and cannot figure it out...Would be awesome if someone could find the solution since I want to release a new update for my app as soon as possible..

07-05 22:45:02.550    2821-2821/davvarn.csgo_maps E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: davvarn.csgo_maps, PID: 2821
    java.lang.RuntimeException: Unable to start activity ComponentInfo{davvarn.csgo_maps/davvarn.csgo_maps.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
            at android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            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:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
            at davvarn.csgo_maps.MainActivity.changeContent(MainActivity.java:176)
            at davvarn.csgo_maps.MainActivity.onCreate(MainActivity.java:115)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
            at android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            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:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

I do not really know how to paste the MainActivity file here, cause I'm new to Stackoverflow. Please let me know how and I will make a try.

Make sure you've initialized your image view and are using the correct id defined in your XML. You can initialize your ImageView like this:

ImageView imageView = (ImageView)findViewById(R.id.yourImageViewIDHere);

Make you do this after your onCreate calls setContentView.

try this..

imgview = (ImageView) findViewById(R.id.imageView);
imgview.setBackgroundResource(R.drawable.image2);

Hoping this will help you :)

In your layout.xml inside Imageview, instead of using android:src or app:srcCompat use property :

android:background="@drawable/yourfilename"

Hopefully this would help to someone whenever getting the exception either

1). while setting background resource to Imageview

2). or to update the imageview resource or to perform some action based on current resource by getting the current background resource from Imageview using

YourImageView.getBackground().getConstantState().equals(
                        getResources().getDrawable(R.drawable.YourImageView).getConstantState())

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM