简体   繁体   中英

Crash/NPE upon setOnClickListener() after inflating

My Android app is crashing when I call setOnClickListener(). Basically, what I have going on is an activity that is using ListView in one XML file, and an inflater in another XML file that consists of three ImageButtons. My images display fine, but when I call setOnClickListener, I get a NullPointerException and the app crashes. I can't figure out why, either.

Here's the gist of the activity:

// 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);

Here's the main activity's 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>

Here's the footer's 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>

And here's the stack trace:

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

Doing a Project -> Clean results in the same issue, and R.java has facebookButton defined.

I'm assuming if I can get this first button working, I can get my other two buttons working.

You need to call findViewById() from the containing view. Try something like:

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

If you call it the way you were doing it, it looks in the Activity's view hierarchy instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM