2012-06-04 67 views
1

我不知道爲什麼Android/Java正在這樣做,這很難想象,因爲我非常疲倦,在現在這個時候我完全無法想象自己。關於onClick的Java/Android NullPointerException

我創建靜態函數和插補和執行代碼的方式,我不認爲這是因爲它甚至還沒有拿到過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); 
      } 
     } 
    }); 


    } 



} 

的logcat:

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) 
+0

在onCreate()中分配之前刪除聲明EditText。這使得一個本地的EditText和留下一個空的undeccaed EditText與全局相同的名稱。 – Phobos

回答

6

我知道這是事實,但只是因爲沒有在Process()法一行。

您的實例成員login始終沒有被初始化,因此,它是null,因此,它拋出一個NullPointerException當你調用Process()方法。

如果你看看你的onCreate()方法,你不會將Button'登錄'保存到實例成員,而是保存到本地成員。

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

這應該可以解決一個問題,但你會爲您的實例成員的其餘太(至少userpass)相同的問題。

user = (EditText) findViewById(R.id.etUser); 
pass = (EditText) findViewById(R.id.etPass); 
+0

謝謝,它現在排序:) – TheBlueCat

相關問題