输入密码后,按钮单击后应用程序崩溃

时间:2017-03-25 20:26:51

标签: android passwords crash

我创建了一个应用程序,用户输入密码,单击按钮并将其带到新活动。我已经创建了一个创建密码活动,用户首先输入一个新密码然后将它们带到主活动。在主活动中,用户将键入创建的密码,单击按钮,它将使他们进入下一个活动。但是,在输入密码(正确或不正确)后单击按钮时,应用程序崩溃。任何人都可以提出建议吗? Android Manifest如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.junaidandroid.androidsecure">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name="com.junaidandroid.CreatePassword">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.junaidandroid.androidsecure.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.junaidandroid.Activity2"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name="com.junaidandroid.NoteLauncher">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>
</application>

`

MainActivity的Java代码

public class MainActivity extends AppCompatActivity {

EditText editText;
Button button2;

String password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView (R.layout.activity_main);


    //load the password
    SharedPreferences settings = getSharedPreferences("PREFS", 0);
    password = settings.getString("password", "");

    editText = (EditText) findViewById(R.id.editText);
    button2 = (Button) findViewById(R.id.button2);

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = editText.getText().toString();

            if (text.equals(password)){
                //enter the app
                Intent intent = new Intent(getApplicationContext(), Activity2.class);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(MainActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

}

我现在添加了LOGCAT错误:

致命的例外:主要                                                                                过程:com.junaidandroid.androidsecure,PID:2569                                                                                java.lang.NullPointerException:尝试调用虚方法&#39; android.text.Editable android.widget.EditText.getText()&#39;在null对象引用上                                                                                    在com.junaidandroid.androidsecure.MainActivity $ 1.onClick(MainActivity.java:38)                                                                                    在android.view.View.performClick(View.java:5610)                                                                                    在android.view.View $ PerformClick.run(View.java:22265)                                                                                    在android.os.Handler.handleCallback(Handler.java:751)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:154)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

2 个答案:

答案 0 :(得分:0)

因为你没有张贴你的logcat而在黑暗中刺伤......

如果要在内部类中访问它,则应将

editText声明为final。当您尝试在此处访问它时会抛出异常:

String text = editText.getText().toString();

答案 1 :(得分:0)

确保编写了正确的edittext的findviewbyid。这个空指针异常只是因为发生 你试图从edittext获取id不正确的值。这个编辑文本id应该与你声明的xml文件中的相同。

editText = (EditText)findViewById(R.id.your_edittext_id);