【问题标题】:app crashes after button click once password is entered输入密码后单击按钮后应用程序崩溃
【发布时间】:2017-03-25 20:26:50
【问题描述】:

我创建了一个应用程序,用户可以在其中输入密码,单击按钮,然后将他们带到一个新活动。我做了一个创建密码活动,用户首先输入一个新密码,然后将他们带到主要活动。在主要活动中,用户将键入创建的密码,单击按钮,然后将他们带到下一个活动。但是,在输入密码(正确或错误)后单击按钮后,应用程序崩溃。有人可以建议吗? Android 清单如下:

<?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:尝试在空对象引用上调用虚拟方法“android.text.Editable android.widget.EditText.getText()” 在 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

【问题讨论】:

标签: android passwords crash


【解决方案1】:

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

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

【讨论】:

  • 是的,我已经设法解决了这个问题,这正是你刚才指出的,ID 与 XML 文件中的 ID 不匹配,因此它一直崩溃
  • 当您不输入密码并单击按钮时,我想建议您一件事,您的应用程序将崩溃,因此请像这样更改您的 if 条件... if ( !TextUtils.isempty( text) && text.equals(密码)){
【解决方案2】:

因为你还没有发布你的日志猫,所以在黑暗中刺伤......

editText 如果你想在内部类中访问它,应该声明final。当您尝试在此处访问时抛出异常:

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

【讨论】:

  • 我是 android studio 新手,不知道如何获取 logcat
  • 在Android Studio的底部栏,选择“Android Monitor”。编辑您的问题以包含应用崩溃时出现的大块红色文本
  • 应用程序崩溃后logcat仍为空白
  • 我该如何声明final?
  • 我已经拿到了 logcat,你能帮我看看吗,非常感谢
猜你喜欢
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
相关资源
最近更新 更多