2012-04-29 31 views
0

我想創建一個登錄頁面。我的研究給了我下面的代碼,但我一直在第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

+0

什麼是在第29行中的LogintesterActivity類的聲明? – C0deAttack

+0

我不知道如何在Eclipse中打開行號。 29是btnLogin.setOnClickListener(新的OnClickListener(){ - Richy 16秒前編輯 – Richy

回答

0

在調試器中運行它。在第29行設置一個斷點,並檢查當時變量的值。確定哪一個不應該是空的,然後回顧它的設置位置,並嘗試找出它爲什麼可能被設置爲null。

此外,請嘗試註釋第29行,看看會發生什麼。

我懷疑你的佈局由於某種原因無法正確加載,但我不確定那是什麼原因。可能有助於你看到它的樣子。

0

可能是btnLogin是NULL。

也許你可以在此行代碼之後添加類似assertNotNull(bthLogin);:如果你發佈的是第29行的代碼,雖然btnLogin = (Button)this.findViewById(R.id.btnLogin);

會更有幫助。

+0

我不知道如何打開Eclipse中的行數29是btnLogin.setOnClickListener(新的OnClickListener(){ – Richy

0

您是否嘗試將2 btnLogin名稱之一更改爲其他名稱?

你必須使用相同的名稱2個不同的對象:btnLogin

+0

謝謝大家 - 我得到它工作原來這是一個與TableLayout有關的XML問題,我將它更改爲LinearLayout,因爲某些原因,一切似乎都正常工作....非常感謝:) – Richy

+0

'android:layout_column =「1」'也許如果你在每個'Textview'或'Button'上都有這條線可以修復它。 – iCantSeeSharp