0

每次我尝试为我的应用程序(android 运行时)运行注册活动时,我都会不断接近。以下是我得到的错误:

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 com.android.internal.policy.impl.PhoneWindow$DecorView@4058b4c8 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 com.android.internal.policy.impl.PhoneWindow$DecorView@4058b4c8 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

2 回答 2

1

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

您还应该将新的移动IntentonPostExecute.

于 2012-08-17T14:30:58.010 回答
0

您不能从 AsyncTask doInBackground()(或除 UI 线程之外的任何线程)访问 UI 组件。

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

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

于 2012-08-17T14:24:38.657 回答