2013-07-09 26 views
0

我有一個ViewFlipper並在其中包含2個xml佈局。setOnTouchListener無法在RelativeLayout上工作

<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的通過ID 'search_rel'

,當我想setOnTouchListener爲search_rel我的應用程序是強制關閉:

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> 
+2

顯示完整的追蹤 –

+0

@MichaelButscher:answer edited。 – smoqadam

+1

您的'RelativeLayout'沒有適當的「android:id」屬性 –

回答

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

這種佈局爲空。這意味着你得到佈局錯誤的地方或錯誤的名稱。

你可以試着這樣做:

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"> 
+0

@ Mr-Moqadam更新回答。 –

相關問題