开发者

setInputType() causing force close in App

开发者 https://www.devze.com 2023-03-31 16:43 出处:网络
Trying to use a ToggleButton to cycle between a password (*) and plain text EditText. Using Everything the toggle is pressed and the setInputType() runs application force closes code below any help wo

Trying to use a ToggleButton to cycle between a password (*) and plain text EditText. Using Everything the toggle is pressed and the setInputType() runs application force closes code below any help would be great because it looks right.

    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>


You have a small mistake:

android.id="@+id/etCommand"

Should be

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

It causes input to be null, since it is not found in findViewById.


android.id="@+id/etCommand" should be android:id="@+id/etCommand"

0

精彩评论

暂无评论...
验证码 换一张
取 消