0

我正在尝试创建一个登录页面。我的研究为我提供了以下代码,但我在第 29 行不断收到 java.lang.NullPointerException。我试图自己研究答案,但结果非常困惑。有人可以帮我理解我所缺少的吗?

LOGINTERACTIVITY 类

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();
       }

}
});       
    }
} 

主要的.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>

清单.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)

非常感谢,里奇

4

3 回答 3

0

在调试器中运行它。在第 29 行设置断点,并在该点检查变量的值。找出不应该为 null 的那个,然后回顾它的设置位置,并尝试找出它可能被设置为 null 的原因。

另外,尝试注释掉第 29 行,看看会发生什么。

我怀疑你的布局由于某种原因没有正确加载,但我不确定那个原因是什么。可能对您了解它的外观有所帮助。

于 2012-04-29T16:37:29.323 回答
0

可能那btnLogin是NULL。

也许您可以assertNotNull(bthLogin);在代码中的这一行之后添加类似的内容:btnLogin = (Button)this.findViewById(R.id.btnLogin);

不过,如果您在代码中发布第 29 行的内容会更有帮助。

于 2012-04-29T16:43:16.350 回答
0

您是否尝试将 2 个btnLogin名称中的一个更改为另一个名称?

您有 2 个具有相同名称的不同对象:btnLogin

于 2012-04-29T16:54:58.197 回答