0

当我调用 setOnClickListener() 时,我的 Android 应用程序崩溃了。基本上,我正在进行的活动是在一个 XML 文件中使用 ListView,并在另一个由三个 ImageButtons 组成的 XML 文件中使用充气器。我的图像显示正常,但是当我调用 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

执行项目 -> 清理会导致相同的问题,并且 R.java 定义了 facebookButton。

我假设如果我可以让第一个按钮工作,我可以让我的其他两个按钮工作。

4

1 回答 1

1

您需要findViewById()从包含视图中调用。尝试类似:

facebookButton = (ImageButton) footerView.findViewById(R.id.facebookButton);

如果您按照您的方式调用它,它会改为查看 Activity 的视图层次结构。

于 2013-02-26T18:48:13.373 回答