我想創建一個登錄頁面。我的研究給了我下面的代碼,但我一直在第29行得到一個java.lang.NullPointerException。我試圖自己研究答案,但結果很混亂。有人可以幫我理解我錯過了什麼嗎?Android登錄頁面拋出NullPointerException
LOGINTESTERACTIVITY類
package com.b00512756.Logintester;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LogintesterActivity extends Activity {
EditText txtUserName;
EditText txtPassword;
Button btnLogin;
Button btnCancel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtUserName=(EditText)this.findViewById(R.id.txtUname);
txtPassword=(EditText)this.findViewById(R.id.txtPwd);
btnLogin=(Button)this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
Toast.makeText(LogintesterActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
} else{
Toast.makeText(LogintesterActivity.this, "Invalid Login",Toast.LENGTH_LONG).show();
}
}
});
}
}
main.xml中
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="User Name: "
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:text=""
android:id="@+id/txtUname"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
</TableRow>
<TableRow>
<TextView
android:text="Password: "
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:text=""
android:id="@+id/txtPwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true">
</EditText>
</TableRow>
<TableRow>
<Button
android:text="Cancel"
android:id="@+id/btnCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Login"
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</TableRow>
</TableLayout>
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.b00512756.Logintester"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LogintesterActivity"
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>
錯誤日誌:
04-29 15:36:41.570: WARN/dalvikvm(690): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): FATAL EXCEPTION: main
04-29 15:36:41.590: ERROR/AndroidRuntime(690): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.b00512756.Logintester/com.b00512756.Logintester.LogintesterActivity}: java.lang.NullPointerException
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.os.Looper.loop(Looper.java:123)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at java.lang.reflect.Method.invoke(Method.java:521)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at dalvik.system.NativeStart.main(Native Method)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): Caused by: java.lang.NullPointerException
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at com.b00512756.Logintester.LogintesterActivity.onCreate(LogintesterActivity.java:29)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
非常感謝, Richy
什麼是在第29行中的LogintesterActivity類的聲明? – C0deAttack
我不知道如何在Eclipse中打開行號。 29是btnLogin.setOnClickListener(新的OnClickListener(){ - Richy 16秒前編輯 – Richy