我正在运行以下代码,它在行iv4.setVisibility(View.INVISIBLE)中给出了NullPointerException 请建议。谢谢 代码:
ImageView iv4;
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
iv4= (ImageView) findViewById(R.id.imageView4);
iv4.setVisibility(View.INVISIBLE);
}
<RelativeLayout
xmlns:android="schemas.android.com/apk/res/android";
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/imageView4"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/hg" />
</RelativeLayout>
堆栈跟踪如下:
03-05 03:49:25.477: E/AndroidRuntime(1070): FATAL EXCEPTION: main
03-05 03:49:25.477: E/AndroidRuntime(1070): java.lang.RuntimeException: Unable to start activity ComponentInfo{bitcream.candyhive.candy/bitcream.candyhive.candy.ScoreActivity}: java.lang.NullPointerException
03-05 03:49:25.477: E/AndroidRuntime(1070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-05 03:49:25.477: E/AndroidRuntime(1070): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-05 03:49:25.477: E/AndroidRuntime(1070): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-05 03:49:25.477: E/AndroidRuntime(1070): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-05 03:49:25.477: E/AndroidRuntime(1070): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 03:49:25.477: E/AndroidRuntime(1070): at android.os.Looper.loop(Looper.java:123)
03-05 03:49:25.477: E/AndroidRuntime(1070): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-05 03:49:25.477: E/AndroidRuntime(1070): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 03:49:25.477: E/AndroidRuntime(1070): at java.lang.reflect.Method.invoke(Method.java:507)
03-05 03:49:25.477: E/AndroidRuntime(1070): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-05 03:49:25.477: E/AndroidRuntime(1070): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-05 03:49:25.477: E/AndroidRuntime(1070): at dalvik.system.NativeStart.main(Native Method)
03-05 03:49:25.477: E/AndroidRuntime(1070): Caused by: java.lang.NullPointerException
03-05 03:49:25.477: E/AndroidRuntime(1070): at bitcream.candyhive.candy.ScoreActivity.onCreate(ScoreActivity.java:42)
03-05 03:49:25.477: E/AndroidRuntime(1070): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-05 03:49:25.477: E/AndroidRuntime(1070): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-05 03:49:25.477: E/AndroidRuntime(1070): ... 11 more
03-05 03:49:28.297: I/Process(1070): Sending signal. PID: 1070 SIG: 9
答案 0 :(得分:1)
行iv4.setVisibility(View.INVISIBLE)中的NullPointerException
ImageView
中可能没有imageView4
代码activity_score.xml
。
您引用了错误的布局,或者您的布局不包含带有ID的图像视图。
编辑:
更改
<RelativeLayout
xmlns:android="schemas.android.com/apk/res/android"
到
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
如果NullPointerException
找不到当前虚增布局中提到的ID的视图,您将获得findViewById
。