当我尝试在我的设备上调试应用程序并尝试在几个小时内摆脱它时,我收到此错误...并且无法弄清楚...如果有人能找到解决方案,那会很棒我想尽快为我的应用发布一个新的更新。
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)
我真的不知道如何粘贴MainActivity
文件,因为我是Stackoverflow的新手。请让我知道如何尝试。
答案 0 :(得分:6)
确保您已初始化图像视图并使用XML中定义的正确ID。您可以像这样初始化ImageView:
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewIDHere);
让你在onCreate调用setContentView后执行此操作。
答案 1 :(得分:0)
试试这个..
imgview = (ImageView) findViewById(R.id.imageView);
imgview.setBackgroundResource(R.drawable.image2);
希望这会对你有所帮助:)。
答案 2 :(得分:0)
在Imageview中的layout.xml中,而不是使用 android:src 或 app:srcCompat use属性:
android:background="@drawable/yourfilename"
希望这可以在有人获得异常的情况下对某人有所帮助
1)。同时将背景资源设置为Imageview
2)。或通过使用以下方法从Imageview获取当前背景资源来更新imageview资源或基于当前资源执行某些操作:
YourImageView.getBackground().getConstantState().equals(
getResources().getDrawable(R.drawable.YourImageView).getConstantState())