gpt4 book ai didi

java - 膨胀后 setOnClickListener() 崩溃/NPE

转载 作者:行者123 更新时间:2023-12-01 14:48:52 26 4
gpt4 key购买 nike

当我调用 setOnClickListener() 时,我的 Android 应用程序崩溃了。基本上,我正在进行的 Activity 是在一个 XML 文件中使用 ListView,并在另一个由三个 ImageButton 组成的 XML 文件中使用 inflater。我的图像显示正常,但当我调用 setOnClickListener 时,我收到 NullPointerException 并且应用程序崩溃。我也不明白为什么。

以下是 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);

这是主要 Activity 的 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

做一个项目 -> Clean 会导致同样的问题,并且 R.java 定义了 facebookButton。

我假设如果我能让第一个按钮正常工作,我就能让其他两个按钮正常工作。

最佳答案

您需要从包含 View 中调用findViewById()。尝试这样的事情:

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

如果您按照原来的方式调用它,它会在 Activity 的 View 层次结构中查找。

关于java - 膨胀后 setOnClickListener() 崩溃/NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15096877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com