通过Intent启动新活动时Android应用程序崩溃

时间:2017-03-04 19:31:44

标签: java android android-intent android-activity

目前,我正在开发Android Studio Code中的第一个Android应用程序。该应用程序包含多个活动。它在我的虚拟设备上工作得非常漂亮,但是当我在第二个活动中开始第三个活动时,应用程序在我的设备上崩溃了。因此,可以毫无问题地从主要活动转到第二活动。

下面你可以看到我的课。重要的是要注意,当我运行应用程序时,我可以在if语句(encryptedPassword.equals(storedPassword))之后看到toast消息。之后,应用程序崩溃了。我没有看到任何错误。此外,由于应用程序在此之前崩溃,显然是我无法捕获异常。

public class LoginActivity extends AppCompatActivity {

private String errorLogin;
private String wrongPassword;
private String rightPassword;
private String tryAgain;
private String greeting;
private static final String PACKAGE_NAME = "nl.test.notevault.activities";
private static final String NOTES_ACTIVITY = "NotesActivity";

Button loginButton;
EditText loginPassword;
Context context;

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

    loginButton = (Button)findViewById(R.id.loginButton);
    loginPassword = (EditText)findViewById(R.id.loginPassword);
    context = this;

    errorLogin = getString(R.string.error_login_general);
    wrongPassword = getString(R.string.error_login_wrong_password);
    rightPassword = getString(R.string.success_login_general);
    tryAgain = getString(R.string.action_try_again);
    greeting = getString(R.string.greeting_general);

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                EncryptString e = new EncryptString();
                String encryptedPassword = e.encrypt(loginPassword.getText().toString());
                KeyValueDB k = new KeyValueDB();
                String storedPassword = k.getPassword(context);
                if (encryptedPassword.equals(storedPassword)) {
                    Toast.makeText(getApplicationContext(), rightPassword + ". " + greeting + ", " + k.getUsername(context) + "!", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, NotesActivity.class); 
                    startActivity(intent);
                } else {
                    Toast.makeText(getApplicationContext(), wrongPassword + ". " + tryAgain + ".", Toast.LENGTH_SHORT).show();
                    loginPassword.setText(null);
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), errorLogin + ".", Toast.LENGTH_SHORT).show();
            }
        }
    });

我的清单可能有问题。下面你可以看到它。所有活动都存储在活动包中。

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".activities.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".activities.LoginActivity"
        android:label="@string/title_activity_login" />
    <activity
        android:name=".activities.SetupActivity"
        android:label="@string/title_activity_setup" />
    <activity android:name=".activities.CreateNoteActivity" />
    <activity android:name=".activities.NotesActivity"
        android:label="Notes" />
    <activity android:name=".activities.EditNoteActivity" />
    <activity android:name=".activities.SettingsActivity"></activity>
</application>

虚拟设备的logcat输出。请注意,该应用程序在虚拟设备上运行正常。由于我没有合适的连接器atm,我无法将手机连接到电脑。

W/System: ClassLoader referenced unknown path: /data/app/nl.test.notevault-2/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is nl.test.notevault, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/nl.test.notevault-2/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
E/EGL_emulation: tid 3721: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaefc9e60, error=EGL_BAD_MATCH
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
E/EGL_emulation: tid 3721: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x92ab8ac0, error=EGL_BAD_MATCH
E/EGL_emulation: tid 3721: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaefd0280, error=EGL_BAD_MATCH
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
E/EGL_emulation: tid 3721: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x92ab9680, error=EGL_BAD_MATCH
E/EGL_emulation: tid 3721: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaefc9e40, error=EGL_BAD_MATCH
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection

1 个答案:

答案 0 :(得分:0)

我买了正确的线缆并将手机连接到电脑上。当我通过Android Code Studio直接在调试模式下运行程序时,不会弹出意图错误。我认为我的apk文件有问题,否则我无法确定错误是什么。谢谢你们。