gpt4 book ai didi

android - 当onClickListener时,Android App崩溃

转载 作者:行者123 更新时间:2023-12-03 17:45:41 25 4
gpt4 key购买 nike

添加onClickListener时我的应用程序崩溃了,
我不知道我的问题在哪里。
我的代码是:

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room_choose);
addButtonListener(); //that crashes

[...]
}
public void addButtonListener() {
ImageButton ib = (ImageButton) findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RoomChoose.this, Salon.class);
startActivity(intent);
}
});
}

图像按钮的代码为:
    <ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@null"
android:contentDescription="@string/salonDesc"
android:src="@drawable/ic_liv" />

有人可以帮忙吗?

更新:

崩溃日志:
11-10 17:16:36.713: E/Trace(5166): error opening trace file: No such file or directory (2)
11-10 17:16:37.123: E/AndroidRuntime(5166): FATAL EXCEPTION: main
11-10 17:16:37.123: E/AndroidRuntime(5166): java.lang.RuntimeException: Unable to start activity ComponentInfo{ele.mentari.homecenter/ele.mentari.homecenter.RoomChoose}: java.lang.NullPointerException
11-10 17:16:37.123: E/AndroidRuntime(5166): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at android.app.ActivityThread.access$600(ActivityThread.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at android.os.Handler.dispatchMessage(Handler.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at android.os.Looper.loop(Looper.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at android.app.ActivityThread.main(ActivityThread.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 17:16:37.123: E/AndroidRuntime(5166): at java.lang.reflect.Method.invoke(Method.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at dalvik.system.NativeStart.main(Native Method)
11-10 17:16:37.123: E/AndroidRuntime(5166): Caused by: java.lang.NullPointerException
11-10 17:16:37.123: E/AndroidRuntime(5166): at ele.mentari.homecenter.RoomChoose.addButtonListener(RoomChoose.java:48)
11-10 17:16:37.123: E/AndroidRuntime(5166): at ele.mentari.homecenter.RoomChoose.onCreate(RoomChoose.java:36)
11-10 17:16:37.123: E/AndroidRuntime(5166): at android.app.Activity.performCreate(Activity.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
11-10 17:16:37.123: E/AndroidRuntime(5166): at com.lbe.security.service.core.client.b.x.callActivityOnCreate(Unknown Source)
11-10 17:16:37.123: E/AndroidRuntime(5166): ... 12 more

最佳答案

您正在NullPointerException上获得ImageButton ib,因为它属于Fragment xml-fragment_room_choose.xml。您正在使用findViewById引用activity_room_choose.xml

您将需要在片段的onCreateView中这样做:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_room_choose, container, false);
ImageButton ib = (ImageButton) view.findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your code
}
});
return view;
}

希望这可以帮助。

关于android - 当onClickListener时,Android App崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26848014/

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