私が抱えている問題は、ヘッダー xml レイアウト ファイル内にあるボタンに MainActivity から直接アクセスできないことです。実行するたびにアプリがクラッシュします。
NullPointerException ポイントを取得し続けます:
MainActivity.onCreate(MainActivity.java:125)
このコード行はどれですか:
mButtonHeader.setOnClickListener(new View.OnClickListener() {
ボタンにアクセスし、onClick() を設定してクリック可能にするにはどうすればよいですか?
私のヘッダーのレイアウト:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#ffffff"
android:orientation="vertical"
android:id="@+id/myHeader">
<!--android:background="@drawable/background_material_original"-->
<Button
android:id="@+id/buttonHeader"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:alpha="0.0"
android:clickable="true"
android:enabled="true"
/>
</RelativeLayout>
私の MainActivity XML レイアウト ファイル:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar" />
<ImageView
android:id="@+id/imageViewHomeBackgroundPic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_marginTop="0dp"
android:background="@drawable/homebp"
android:contentDescription="@string/home_bp"/>
<TextView
android:id="@+id/mId"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header"
app:menu="@menu/menu_drawer_home"
/>
</android.support.v4.widget.DrawerLayout>
私の MainActivity クラス:
public class MainActivity extends AppCompatActivity{
protected Toolbar toolbar;
protected NavigationView navigationView;
private DrawerLayout drawerLayout;
protected Button mButtonHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Calling method of my navigation drawer
setupNavViewDrawer();
mButtonHeader = (Button) findViewById(R.id.buttonHeader);
mButtonHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("http://www.google.com/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
私のlogcatエラー:
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: FATAL EXCEPTION: main
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: Process: com.pi.cas.ndtc, PID: 14553
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pi.cas.ndtc/com.pi.cas.ndtc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5257)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at com.pi.cas.ndtc.MainActivity.onCreate(MainActivity.java:125)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5257)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-31 20:48:05.342 14553-14553/com.pi.cas.ndtc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)