当我调用setOnClickListener()时,我的Android应用程序崩溃了。基本上,我所做的是在一个XML文件中使用ListView的活动,以及由三个ImageButtons组成的另一个XML文件中的inflater。我的图像显示正常,但是当我调用setOnClickListener时,我得到一个NullPointerException并且应用程序崩溃了。我也无法弄明白为什么。
以下是活动的要点:
// Create an inflater to use another xml layout (the Facebook/Twitter/Instagram buttons)
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View footerView = inflater.inflate(R.layout.contacts_footer, null);
facebookButton = (ImageButton) findViewById(R.id.facebookButton);
// Commenting this chunk out has the images display perfectly fine
facebookButton.setOnClickListener(new OnClickListener() { // Crashes here
public void onClick(View v) {
// Open up the Facebook page when clicked
facebookPage = new Intent(android.content.Intent.ACTION_VIEW);
facebookPage.setData(Uri.parse(facebookURL));
startActivity(facebookPage);
}
});
listview.addFooterView(footerView);
listview.setAdapter(adapter);
这是主要活动的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blackBackground" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/button_gradient" >
<!-- Header -->
<Button
android:id="@+id/aboutButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:text="About"
android:textColor="@color/whiteText" />
<TextView
android:id="@+id/contactHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Contact"
android:textColor="@color/whiteText" />
</RelativeLayout>
<!-- Listing -->s
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/contactItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@color/blackBackground"
android:dividerHeight="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
</ListView>
</RelativeLayout>
</LinearLayout>
这是页脚的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blackBackground"
android:id="@+id/buttonIcons"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonIcons"
android:gravity="center_horizontal">
<ImageButton
android:id="@+id/facebookButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:paddingLeft="5dp"
android:src="@drawable/facebook_icon" />
<ImageButton
android:id="@+id/twitterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:paddingLeft="5dp"
android:src="@drawable/twitter_icon" />
<ImageButton
android:id="@+id/instagramButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:paddingLeft="5dp"
android:src="@drawable/instagram_icon" />
</LinearLayout>
</LinearLayout>
这是堆栈跟踪:
02-26 11:27:14.073: E/AndroidRuntime(875): FATAL EXCEPTION: main
02-26 11:27:14.073: E/AndroidRuntime(875): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.radio.app/org.radio.app.Contacts}: java.lang.NullPointerException
02-26 11:27:14.073: E/AndroidRuntime(875): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-26 11:27:14.073: E/AndroidRuntime(875): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-26 11:27:14.073: E/AndroidRuntime(875): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-26 11:27:14.073: E/AndroidRuntime(875): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-26 11:27:14.073: E/AndroidRuntime(875): at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 11:27:14.073: E/AndroidRuntime(875): at android.os.Looper.loop(Looper.java:137)
02-26 11:27:14.073: E/AndroidRuntime(875): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-26 11:27:14.073: E/AndroidRuntime(875): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 11:27:14.073: E/AndroidRuntime(875): at java.lang.reflect.Method.invoke(Method.java:511)
02-26 11:27:14.073: E/AndroidRuntime(875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-26 11:27:14.073: E/AndroidRuntime(875): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-26 11:27:14.073: E/AndroidRuntime(875): at dalvik.system.NativeStart.main(Native Method)
02-26 11:27:14.073: E/AndroidRuntime(875): Caused by: java.lang.NullPointerException
02-26 11:27:14.073: E/AndroidRuntime(875): at org.radio.app.Contacts.onCreate(Contacts.java:127)
02-26 11:27:14.073: E/AndroidRuntime(875): at android.app.Activity.performCreate(Activity.java:5008)
02-26 11:27:14.073: E/AndroidRuntime(875): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-26 11:27:14.073: E/AndroidRuntime(875): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-26 11:27:14.073: E/AndroidRuntime(875): ... 11 more
做项目 - &gt;清除同一问题的结果,并且R.java定义了facebookButton。
我假设我可以让第一个按钮工作,我可以让其他两个按钮工作。
答案 0 :(得分:1)
您需要从包含的视图中调用findViewById()
。尝试类似:
facebookButton = (ImageButton) footerView.findViewById(R.id.facebookButton);
如果按照您的方式调用它,它会在Activity的视图层次结构中查找。