我有一个ViewFlipper并在其中包含2个xml布局。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/topmenubg" android:layout_gravity="left">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewFlipper">
<include android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/search_layout" android:id="@+id/include1"/>
<include android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/list_layout" android:id="@+id/include"/>
</ViewFlipper>
在search_layout中的
我有一个RelativeLayout By ID'search_rel'
当我想为search_rel设置setOnTouchListener时,我的应用程序强行关闭:
RelativeLayout rel = (RelativeLayout) findViewById(R.id.search_rel);
rel.setClickable(false);
rel.setOnTouchListener(mGestureListener);
错误:
07-09 13:06:36.776: ERROR/AndroidRuntime(4453): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.joupin.Story/ir.joupin.Story.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ir.joupin.Story.MyActivity.onCreate(MyActivity.java:270)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
修改: 我的search_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#80000000" android:id="@+id/search_rel" android:focusableInTouchMode="true"
android:focusable="true" android:clickable="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="عبارت : "
android:id="@+id/textView" android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:layout_marginTop="20dp"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_toLeftOf="@+id/textView" android:layout_alignBaseline="@+id/textView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="جستجو در : "
android:id="@+id/textView1" android:layout_alignRight="@+id/textView" android:layout_alignParentTop="false"
android:layout_marginTop="40dp" android:layout_below="@+id/textView"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:layout_alignParentLeft="false"
android:layout_alignBaseline="@+id/textView1" android:layout_alignParentRight="false"
android:layout_toLeftOf="@+id/textView1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="23dp"
android:text="متن"
android:id="@+id/checkbox_text"
android:labelFor="@id/checkBox"
android:layout_alignTop="@+id/textView1" android:layout_toLeftOf="@+id/checkBox"/>
..................
.............
...............
.................
</RelativeLayout>
答案 0 :(得分:3)
RelativeLayout rel = (RelativeLayout) findViewById(R.id.search_rel);
此布局为null。这意味着您在错误的位置或错误的名称中获取布局。
您可以尝试这样做:
View includeView = yourViewFlipper.findViewById(R.id.yourInclude);
RelativeLayout rel = (RelativeLayout) includeView.findViewById(R.id.search_rel);
由于Michael Butscher
感到很遗憾,您忘了将R.id.search_rel
添加到您的布局中。试试这个:
<RelativeLayout
android:id="@+id/search_rel"
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#80000000" android:id="@+id/search_rel" android:focusableInTouchMode="true"
android:focusable="true" android:clickable="false">