2015-06-14 138 views
0

我遇到此錯誤,當我啓動我的應用程序,我該如何解決這個問題?無法實例化活動ComponentInfo啓動我的應用程序

12112-12112/com.javacodegeeks.androidqrcodeexample E/AndroidRuntime: 致命異常:主 了java.lang.RuntimeException:無法實例活動ComponentInfo {com.javacodegeeks.androidqrcodeexample/com.javacodegeeks.androidqrcodeexample.Login }: 顯示java.lang.NullPointerException

這是我的.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.javacodegeeks.androidqrcodeexample" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="19" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name="com.javacodegeeks.androidqrcodeexample.AndroidBarcodeQrExample" 
      android:label="@string/app_name" > 
     </activity> 
     <activity 
      android:name="com.javacodegeeks.androidqrcodeexample.Login" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

,在這裏我的活動:

package com.javacodegeeks.androidqrcodeexample; 

/** 
* Created by Andre on 14/06/15. 
*/ 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.ActivityNotFoundException; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 


public class Login extends Activity { 

    EditText username = (EditText)findViewById(R.id.editText); 
    EditText password = (EditText)findViewById(R.id.editText2); 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
    } 

    public void login(View view) { 
     if (username.getText().toString().equals("admin") && password.getText().toString().equals("admin")) { 
      Toast toast = Toast.makeText(this, "CORRECT PW", Toast.LENGTH_LONG); 
      toast.show(); 
      //correcct password 
      Intent i = new Intent(getApplicationContext(), AndroidBarcodeQrExample.class); 
      i.putExtra("Disco", username.getText().toString()); 
      startActivity(i); 
     } else { 
      Toast toast = Toast.makeText(this, "UNCORRECT PW", Toast.LENGTH_LONG); 
      toast.show(); 
      //wrong password 
     } 
    } 

} 
+0

在未來,張貼整個堆棧跟蹤。 – CommonsWare

回答

1

移動線:

EditText username = (EditText)findViewById(R.id.editText); 
EditText password = (EditText)findViewById(R.id.editText2); 

onCreate內部

EditText username; 
EditText password; 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    username = (EditText)findViewById(R.id.editText); 
    password = (EditText)findViewById(R.id.editText2); 
} 
相關問題