1

我不知道为什么 Android/Java 会这样做,这很难想,因为我非常疲倦,目前无法独立思考。

我正在创建静态函数并以这种方式输入和执行代码,我认为这不是原因,因为它甚至还没有通过 if 语句;大家怎么看?我敢打赌这是一个简单的解决方案,我只是无法为我的生活弄明白。

代码:

public class LogIn extends Activity {
    Button login;
    EditText user;
    EditText pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        TextView top = (TextView) findViewById(R.id.textView2);
        EditText user = (EditText) findViewById(R.id.etUser);
        EditText pass = (EditText) findViewById(R.id.etPass);
        CheckBox stay = (CheckBox) findViewById(R.id.cBStay);
        Button login = (Button) findViewById(R.id.btLogin);
        Process();

   }

    private void Process() {
        // TODO Auto-generated method stub
    login.setOnClickListener( new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String user1 = user.getText().toString();
             String pass1 = pass.getText().toString();
            if(user1 !=null &user1.length()>=1 & pass1 !=null &pass1.length()>=1) {
                ComHelper.SendLogin(user1, pass1);
            }
        }
    });


    }



}

日志猫:

06-03 22:34:01.178: E/AndroidRuntime(2599): FATAL EXCEPTION: main
06-03 22:34:01.178: E/AndroidRuntime(2599): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gta5news.qwuik/com.gta5news.qwuik.LogIn}: java.lang.NullPointerException
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.os.Looper.loop(Looper.java:137)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at java.lang.reflect.Method.invoke(Method.java:511)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at dalvik.system.NativeStart.main(Native Method)
06-03 22:34:01.178: E/AndroidRuntime(2599): Caused by: java.lang.NullPointerException
06-03 22:34:01.178: E/AndroidRuntime(2599):     at com.gta5news.qwuik.LogIn.Process(LogIn.java:32)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at com.gta5news.qwuik.LogIn.onCreate(LogIn.java:26)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.app.Activity.performCreate(Activity.java:4465)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-03 22:34:01.178: E/AndroidRuntime(2599):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
4

1 回答 1

6

我知道这是一个事实,但这只是因为方法中有一行Process()

您的实例成员login从未被初始化,因此它是,因此,当您调用该方法时null它会抛出 a 。NullPointerExceptionProcess()

如果您查看您的onCreate()方法,您不会将Button“登录”保存到实例成员,而是保存到本地成员。

login = (Button) findViewById(R.id.btLogin);

这应该可以解决一个问题,但您的其他实例成员也会遇到同样的问题(至少userpass)。

user = (EditText) findViewById(R.id.etUser);
pass = (EditText) findViewById(R.id.etPass);
于 2012-06-04T00:36:13.710 回答