如何使用Android中的onclick使编辑文本可见

时间:2014-07-11 17:29:23

标签: android android-edittext onclicklistener

当我点击我用来使其显示的按钮时出现编辑文本框时出现问题。我已将编辑文本设置为     机器人:能见度= '不见了'

并在我的活动中使用代码使用onclick方法使其可见。 我运行应用程序时没有编译错误,当我单击应用程序中的按钮时,我只收到运行时错误。

这是代码。

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_orders,
                container, false);

        final TextView t=(TextView)rootView.findViewById(R.id.textView1);
        Button b = (Button) rootView.findViewById(R.id.button1); 

        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View V) {

                t.setVisibility(View.VISIBLE);
               }

        });
        return rootView;
    }
 }
}

logcat的

07-11 13:54:49.874: E/AndroidRuntime(1116): FATAL EXCEPTION: main
07-11 13:54:49.874: E/AndroidRuntime(1116): Process: com.example.thelodown, PID: 1116
07-11 13:54:49.874: E/AndroidRuntime(1116): java.lang.NullPointerException
07-11 13:54:49.874: E/AndroidRuntime(1116):     at com.example.thelodown.Orders$PlaceholderFragment$1.onClick(Orders.java:69)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at android.view.View.performClick(View.java:4438)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at android.view.View$PerformClick.run(View.java:18422)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at android.os.Handler.handleCallback(Handler.java:733)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at android.os.Looper.loop(Looper.java:136)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at java.lang.reflect.Method.invoke(Method.java:515)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-11 13:54:49.874: E/AndroidRuntime(1116):     at dalvik.system.NativeStart.main(Native Method)

布局

<Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TASK " />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="Enter your orders here"
        android:visibility="gone" >

        <requestFocus />
    </EditText>

1 个答案:

答案 0 :(得分:0)

在您的布局中找不到标识为TextView的{​​{1}}

我觉得你错了..

所以请转换

textView1

final TextView t=(TextView)rootView.findViewById(R.id.textView1);