Android应用程序在通过意图启动新活动时崩溃

问题描述:

目前,我正在开发Android Studio代码中的第一个Android应用程序。该应用程序由多个活动组成。它在我的虚拟设备上运行的很好,但是当我在第二个活动中开始第三个活动时,该应用在我的设备上崩溃。因此,有可能从主要活动转到第二个没有问题。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 
+0

[logcat会告诉你什么原因你的应用程序停止](http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this)。请用它编辑你的问题。 –

+1

另外,从来没有'吐司'例外。总是'Log.e'他们 –

+0

我们可以看到它实际上中断的日志吗? – EJoshuaS

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