我遇到的問題是我無法直接從我的MainActivity訪問我的頭文件xml佈局文件中的按鈕。每次運行應用程序都會崩潰。如何從我的MainActivity訪問我的Header.xml佈局中的按鈕?
我不斷收到一個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)
是您的標題佈局命名tool_bar?它只是看起來不像你有正確的連接到我的XML。 –
tool_bar是一個單獨的佈局,我定義了工具欄應該是什麼樣的。我假設工具欄與當前問題無關 – Equivocal
那麼你的問題就在那裏。 MainActivity xml通過include引用tool_bar,但不包含包含頭的XML文件。導入這個人,它應該工作。 –