0

我有一个 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 设置 OnTouchListener 时,我的应用程序被强制关闭:

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>
4

1 回答 1

3
RelativeLayout rel = (RelativeLayout) findViewById(R.id.search_rel);

此布局为空。这意味着您将 Layout 放在错误的位置或使用错误的名称。

您可以尝试这样做:

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">
于 2013-07-09T13:28:02.507 回答