0

我使用片段,并且在活动中我想在触摸编辑文本时显示/隐藏一个相对布局(最后一个:xK1):

<?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,提前。

日志猫:

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
4

1 回答 1

3

您应该只为顶视图定义以下内容: xmlns:android="http://schemas.android.com/apk/res/android" 我认为这就是为什么您会收到 nullpointerexception 的原因,因为 layoutinflater 找不到正确的视图通过层次结构。

于 2013-02-18T15:58:45.740 回答