问题描述
- Android 写事件监听 真机调试出现闪退(我是新手,刚学Android,求大神指教)
-
这是我的程序:
ImageView imageView = (ImageView) findViewById(R.id.login_image);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {} });
这是错误日志:
03-08 18:10:43.557 9816-9816/com.example.administrator.happyread E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.administrator.happyread, PID: 9816
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.happyread/com.example.administrator.happyread.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$1200(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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:967)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.administrator.happyread.MainActivity.onCreate(MainActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6102)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)?
at android.app.ActivityThread.access$1200(ActivityThread.java:165)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)?
at android.os.Handler.dispatchMessage(Handler.java:102)?
at android.os.Looper.loop(Looper.java:135)?
at android.app.ActivityThread.main(ActivityThread.java:5593)?
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:967)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)?
03-08 18:10:43.877 9816-9816/com.example.administrator.happyread I/Process: Sending signal. PID: 9816 SIG: 9这是我调用的xml:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@mipmap/k8"
android:gravity="bottom"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark"><ImageView android:id="@+id/login_image" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginBottom="5dp" android:paddingTop="@dimen/nav_header_vertical_spacing" android:src="@mipmap/p9" /> <Button android:id="@+id/login_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:text="立即登录" android:layout_alignBottom="@+id/login_image" android:layout_toRightOf="@+id/login_image" android:layout_toEndOf="@+id/login_image" />
解决方案
你的imageview是空的,检查一下这个activity调用的xml里面有没有login_image这个id的imageview
解决方案二:
已经解决了,谢谢大神,这里给出我的解决方法
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
ImageView imageView = (ImageView) findViewById(R.id.login_image);
imageView = (ImageView) headerView.findViewById(R.id.login_image);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
});
用的是一楼的方法
解决方案三:
把setContentView方法写在findviewbyid前面。