2012-08-17 167 views
0

我每次尝试运行我的应用程序(android运行时)的注册活动时都会收到强制关闭。下面是我得到的错误:AsyncTask致命异常#!

08-16 23:58:41.146: W/dalvikvm(4017): threadid=10: thread exiting with uncaught exception (group=0x4001d5a0) 
08-16 23:58:41.156: E/AndroidRuntime(4017): FATAL EXCEPTION: AsyncTask #1 
08-16 23:58:41.156: E/AndroidRuntime(4017): java.lang.RuntimeException: An error occured while executing doInBackground() 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at java.lang.Thread.run(Thread.java:1027) 
08-16 23:58:41.156: E/AndroidRuntime(4017): Caused by: java.lang.NullPointerException 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at com.thryfting.www.RegisterActivity$register.doInBackground(RegisterActivity.java:124) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at com.thryfting.www.RegisterActivity$register.doInBackground(RegisterActivity.java:1) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
08-16 23:58:41.156: E/AndroidRuntime(4017):  ... 4 more 
08-16 23:58:41.416: E/WindowManager(4017): Activity com.thryfting.www.RegisterActivity has leaked window [email protected] that was originally added here 
08-16 23:58:41.416: E/WindowManager(4017): android.view.WindowLeaked: Activity com.thryfting.www.RegisterActivity has leaked window [email protected] that was originally added here 
08-16 23:58:41.416: E/WindowManager(4017): at android.view.ViewRoot.<init>(ViewRoot.java:277) 
08-16 23:58:41.416: E/WindowManager(4017): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 
08-16 23:58:41.416: E/WindowManager(4017): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
08-16 23:58:41.416: E/WindowManager(4017): at android.view.Window$LocalWindowManager.addView(Window.java:433) 
08-16 23:58:41.416: E/WindowManager(4017): at android.app.Dialog.show(Dialog.java:265) 
08-16 23:58:41.416: E/WindowManager(4017): at com.thryfting.www.RegisterActivity$register.onPreExecute(RegisterActivity.java:106) 
08-16 23:58:41.416: E/WindowManager(4017): at android.os.AsyncTask.execute(AsyncTask.java:391) 
08-16 23:58:41.416: E/WindowManager(4017): at com.thryfting.www.RegisterActivity.onClick(RegisterActivity.java:78) 
08-16 23:58:41.416: E/WindowManager(4017): at android.view.View.performClick(View.java:2532) 
08-16 23:58:41.416: E/WindowManager(4017): at android.view.View$PerformClick.run(View.java:9293) 
08-16 23:58:41.416: E/WindowManager(4017): at android.os.Handler.handleCallback(Handler.java:587) 
08-16 23:58:41.416: E/WindowManager(4017): at android.os.Handler.dispatchMessage(Handler.java:92) 
08-16 23:58:41.416: E/WindowManager(4017): at android.os.Looper.loop(Looper.java:150) 
08-16 23:58:41.416: E/WindowManager(4017): at android.app.ActivityThread.main(ActivityThread.java:4263) 
08-16 23:58:41.416: E/WindowManager(4017): at java.lang.reflect.Method.invokeNative(Native Method) 
08-16 23:58:41.416: E/WindowManager(4017): at java.lang.reflect.Method.invoke(Method.java:507) 
08-16 23:58:41.416: E/WindowManager(4017): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-16 23:58:41.416: E/WindowManager(4017): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-16 23:58:41.416: E/WindowManager(4017): at dalvik.system.NativeStart.main(Native Method) 

这是我的AsyncTask代码你能告诉我,如果我做错了什么?

public void onClick(View v) { 
     // TODO Auto-generated method stub 
    switch(v.getId()){ 
    case R.id.btnRegister:  

     String userEmail = inputEmail.getText().toString(); 
     String userPassword = inputPassword.getText().toString(); 
     String userFullName = inputPassword.getText().toString(); 

     //Check if all fields have been completed. 
     if (userEmail.equals("") || userPassword.equals("") || userFullName.equals("")){ 
      Toast.makeText(getApplicationContext(), 
        "You must fill in all of the fields.", Toast.LENGTH_SHORT).show(); 
      return; 

     } 

     new register().execute(); 

     break; 

    case R.id.link_to_login: 
     Intent i = new Intent(getApplicationContext(), LoginActivity.class); 
     startActivity(i); 

     //Close Registration View 
     finish(); 

     break; 

     } 

    } 

    public class register extends AsyncTask<String, String, String>{ 


     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(RegisterActivity.this); 
      pDialog.setMessage("Joining Thryfting..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 


      // Register Button Click event 
        String name = inputFullName.getText().toString(); 
        String email = inputEmail.getText().toString(); 
        String password = inputPassword.getText().toString(); 
        UserFunctions userFunction = new UserFunctions(); 
        JSONObject json = userFunction.registerUser(name, email, password); 

        // check for login response 
        try { 
         if (json.getString(KEY_SUCCESS) != null) { 

          registerErrorMsg.setText(""); 

          String res = json.getString(KEY_SUCCESS); 

          if(Integer.parseInt(res) == 1){ 
           // user successfully registred 
           // Store user details in SQLite Database 
           DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 
           JSONObject json_user = json.getJSONObject("user"); 

           // Clear all previous data in database 
           userFunction.logoutUser(getApplicationContext()); 
           db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT)); 

           // Launch Dashboard Screen 

           Intent dashboard = new Intent(getApplicationContext(), Timeline.class); 

           // Close all views before launching Dashboard 
           dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           startActivity(dashboard); 

           // Close Registration Screen 
           finish(); 
          }else{ 
           // Error in registration 
           //registerErrorMsg.setText("Error occured in registration"); 
           Toast.makeText(getApplicationContext(), KEY_ERROR_MSG, Toast.LENGTH_SHORT).show(); 
          } 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      pDialog.dismiss(); 
     } 

    } 
+4

你在doInBackground获得nullpointerexception(RegisterActivity.java:124) – nandeesh 2012-08-17 14:19:52

+0

有没有一种方法可以解决它? – 2012-08-17 14:24:24

+0

指向文件中的第124行 – nandeesh 2012-08-17 14:25:56

回答

0

不允许你从一个的AsyncTask的doInBackground()(或大于UI线程以外的任何线程)访问的UI组件。

例如:Toast.makeText(...).show();

相反,您应该在onProgressUpdate()onPostExecute()中使用它。

+0

你忘了写这个,为了调用'onProgressUpdate()''你必须从'doInBackground()' – 2012-08-17 14:28:12

+0

@Benito调用'publishProgress()'现在我要编辑 – 2012-08-17 14:46:17

1

在您的doInBackground方法中,您需要返回String而不是null

您还应该将新的Intent移动到onPostExecute

相关问题