2013-02-18 110 views
0

我usging片段,並在活動中我想顯示/隱藏一個RelativeLayout的(最後:XK1)當我觸摸的EditText:Android的碎片不能setvisibility RelativeLayout的

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/xMLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/linearborder" > 


    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/xsubLayout" 
     android:keepScreenOn="true" 
     android:orientation="vertical" 
     android:layout_below="@+id/RL_boutton" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
     <EditText 
      android:id="@+id/editText_code_d" 
      android:layout_width="304dp" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/RL_boutton" 
      android:layout_centerHorizontal="true" 
      android:background="@drawable/plaquehaut" 
      android:ems="4" 
      android:gravity="center_vertical|center_horizontal" 
      android:inputType="number" 
      android:maxLength="4" 
      android:text="123" 
      android:textSize="80sp" 
      android:focusableInTouchMode="true" 
      /> 

     <EditText 
      android:id="@+id/editText_code_m" 
      android:layout_width="304dp" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/editText_code_d" 
      android:layout_below="@+id/editText_code_d" 
      android:background="@drawable/plaquebas" 
      android:ems="4" 
      android:gravity="center_vertical|center_horizontal" 
      android:inputType="number" 
      android:maxLength="4" 
      android:text="1337" 
      android:textSize="80sp" 
      /> 
    </RelativeLayout> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
       android:id="@+id/xK1" 
     android:layout_height="wrap_content" 
       android:orientation="vertical" 
     android:visibility="gone"> 
     <include android:id="@+id/xKeyBoard" 
        layout="@layout/keyboard"> 
       </include> 
    </RelativeLayout> 
</RelativeLayout> 

下面的代碼:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    { 
     View view = inflater.inflate(R.layout.fragment_rsslist_overview,container, false); 
       try { 
        mLayout = (RelativeLayout) view.findViewById(R.id.xK1); 
        mKLayout = (RelativeLayout) view.findViewById(R.id.xKeyBoard); 

       } catch (Exception e) { 
        Log.w(getClass().getName(), e.toString()); 
       } 
     mEt = (EditText) view.findViewById(R.id.editText_code_d); 
     mEt.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       edittext_onClick(v); 
      } 
     });  
     mEt1 = (EditText) view.findViewById(R.id.editText_code_m); 
     mEt1.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       edittext_onClick(v); 
      } 
     }); 
     mEt.setOnTouchListener(this); 
     return view; 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
      enableKeyboard(); 
      return true; 
    } 

    private void enableKeyboard() { 
     mLayout.setVisibility(RelativeLayout.VISIBLE); 
     mKLayout.setVisibility(RelativeLayout.VISIBLE); 

    } 

程序在此處運行時停止活動: mLayout.setVisibility(RelativeLayout.VISIBLE);

我該如何解決這個問題?

thx,提前。

的logcat:

02-18 12:06:27.364: I/dalvikvm(1044): Wrote stack traces to '/data/anr/traces.txt' 
02-18 12:06:28.963: D/AndroidRuntime(1044): Shutting down VM 
02-18 12:06:28.963: W/dalvikvm(1044): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
02-18 12:06:29.104: E/AndroidRuntime(1044): FATAL EXCEPTION: main 
02-18 12:06:29.104: E/AndroidRuntime(1044): java.lang.NullPointerException 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at fr.app.tutorielfragment.MyListFragment.enableKeyboard(MyListFragment.java:363) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at fr.app.tutorielfragment.MyListFragment.onTouch(MyListFragment.java:240) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.View.dispatchTouchEvent(View.java:5536) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.app.Activity.dispatchTouchEvent(Activity.java:2364) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.View.dispatchPointerEvent(View.java:5721) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.os.Looper.loop(Looper.java:137) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-18 12:06:29.104: E/AndroidRuntime(1044):  at dalvik.system.NativeStart.main(Native Method) 
02-18 12:06:29.693: I/dalvikvm(1044): threadid=3: reacting to signal 3 
02-18 12:06:29.726: I/dalvikvm(1044): Wrote stack traces to '/data/anr/traces.txt' 
02-18 12:06:34.654: I/Process(1044): Sending signal. PID: 1044 SIG: 9 
+0

發佈您的logcat。 – 2013-02-18 13:22:10

+0

爲什麼你在findViewById的周圍嘗試一下,你不應該在那裏遇到問題? – serenskye 2013-02-18 13:27:16

回答

3

你應該只定義爲之後的頂視圖領域:xmlns:安卓= 「http://schemas.android.com/apk/res/android」 我想這就是爲什麼你由於layoutinflater無法通過層次結構找到正確的視圖,因此會導致nullpointerexception。

+0

是的!正是這樣,Thx Warpzit, – morbak 2013-02-20 09:32:52

+0

@morbak不要在評論中放置代碼。編輯你目前的問題,並在那裏添加額外的部分。 – Warpzit 2013-02-20 09:51:37

+0

@morbak嘗試使用android:clickable =「true」 – Warpzit 2013-02-20 09:52:53