添加onClickListener时,我遇到了崩溃app的问题, 我不知道我的问题在哪里。 我的代码是:
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
答案 0 :(得分:1)
您在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;
}
希望这有帮助。