setInputType()导致在App中强制关闭

时间:2011-08-27 02:30:46

标签: java android textinput

尝试使用ToggleButton在密码( * )和纯文本EditText之间循环。使用Everything按下切换按钮并且setInputType()运行应用程序强制关闭代码,任何帮助都会很好,因为它看起来是正确的。

    setContentView(R.layout.text);

    Button chkCMD = (Button) findViewById(R.id.bResults);
    final ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
    final EditText input = (EditText) findViewById(R.id.etCommand);
    final TextView display = (TextView) findViewById(R.id.tvResults);
    passTog.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (passTog.isChecked()){
                input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

            }else{
                input.setInputType(InputType.TYPE_CLASS_TEXT);

                }
            }       
        });
    };

logcat的

08-27 02:33:57.673: WARN/System.err(539): java.lang.NullPointerException
08-27 02:33:57.673: WARN/System.err(539):     at com.don.hatland.TextPlay$1.onClick(TextPlay.java:38)
08-27 02:33:57.683: WARN/System.err(539):     at android.view.View.performClick(View.java:2485)
08-27 02:33:57.683: WARN/System.err(539):     at android.widget.CompoundButton.performClick(CompoundButton.java:99)
08-27 02:33:57.683: WARN/System.err(539):     at android.view.View$PerformClick.run(View.java:9080)
08-27 02:33:57.683: WARN/System.err(539):     at android.os.Handler.handleCallback(Handler.java:587)
08-27 02:33:57.683: WARN/System.err(539):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 02:33:57.683: WARN/System.err(539):     at android.os.Looper.loop(Looper.java:123)
08-27 02:33:57.683: WARN/System.err(539):     at android.app.ActivityThread.main(ActivityThread.java:3647)
08-27 02:33:57.683: WARN/System.err(539):     at java.lang.reflect.Method.invokeNative(Native Method)
08-27 02:33:57.683: WARN/System.err(539):     at java.lang.reflect.Method.invoke(Method.java:507)
08-27 02:33:57.683: WARN/System.err(539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-27 02:33:57.693: WARN/System.err(539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-27 02:33:57.693: WARN/System.err(539):     at dalvik.system.NativeStart.main(Native Method)

Text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="25dp"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android.id="@+id/etCommand"
        android:hint="Type a Command"
        android:password="true"/>


    <LinearLayout android:weightSum="100"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button android:layout_weight="20"
        android:text="Button"
        android:id="@+id/bResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"> </Button>

        <ToggleButton android:layout_weight="80" 
        android:paddingBottom="10dp"
        android:text="Try Command"
        android:checked="true"
        android:id="@+id/tbPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"> </ToggleButton>

    </LinearLayout>

    <TextView android:text="invaild" android:id="@+id/tvResults"
        android:gravity="center" android:layout_width="fill_parent"
        android:layout_height="wrap_content"> </TextView>


</LinearLayout>

2 个答案:

答案 0 :(得分:1)

你有一个小错误:

android.id="@+id/etCommand"

应该是

android:id="@+id/etCommand"
       ^

它会导致input为空,因为在findViewById中找不到它。

答案 1 :(得分:1)

android.id = “@ + ID / etCommand” 应该 机器人:ID = “@ + ID / etCommand”