1

嗨,我正在开发一个新的 android 应用程序,其中有一个登录页面,当用户输入用户名时,用户名会与表中存在的用户名进行检查...如果存在,则必须出现一条消息,如“用户名”存在请使用另一个用户名' ...我为此创建了一个php文件并在活动中添加了代码...如果未注册用户名显示消息,我在android中添加了一条toast消息..但是当我添加该消息时,应用程序是crashibng '不幸的是应用程序关闭'如果我删除它.. 应用程序不会崩溃但会留在页面上.. 我正在添加我的 php 代码和活动代码请检查.. 如果有人帮助...

活动

/**
     * Background Async Task to Create new product
     * */
    class CreateNewProduct extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(SignUpActivity.this);
            pDialog.setMessage("Creating a new account..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {





            String name = editTextUserName.getText().toString();
            String password = editTextPassword.getText().toString();
            String mobile = editMobileNumber.getText().toString();
            String sms = Integer.toString(number);

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            params.add(new BasicNameValuePair("password", password));
            params.add(new BasicNameValuePair("mobile", mobile));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_data,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product




                    Intent i = new Intent(SignUpActivity.this, RegisterActivity.class);

                    i.putExtra("number", sms + "");
                    startActivity(i);

                    //closing this screen
                    //finish();
                } else {
                    // failed to create product
                    return "false";



                }

                protected void onPostExecute(String result)

                {
                    // TODO Auto-generated method stub
                    super.onPostExecute(result);
                                if (result == "false")
                    Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();

                }


            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }





}

PHP 代码

<?php


// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name']) && isset($_POST['password']) && isset($_POST['mobile'])) {

    $name = $_POST['name'];
    $password = $_POST['password'];
    $mobile = $_POST['mobile'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();


     $result1 = mysql_query("SELECT name FROM numerol WHERE name = '$name'");




     if (!mysql_num_rows($result1)) 


     {

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO numerol(name, password, mobile) VALUES('$name', '$password', 

'$mobile')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}


}

else

{

echo 'User Name already exists, Please choose another username';

}
?>

日志猫

11-09 06:12:48.081: I/Choreographer(1792): Skipped 33 frames!  The application may be doing too much work on its main thread.
11-09 06:12:49.091: I/Choreographer(1792): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-09 06:12:49.561: I/Choreographer(1792): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-09 06:12:50.512: I/Choreographer(1792): Skipped 49 frames!  The application may be doing too much work on its main thread.
11-09 06:12:50.660: I/Choreographer(1792): Skipped 34 frames!  The application may be doing too much work on its main thread.
11-09 06:12:51.220: I/Choreographer(1792): Skipped 50 frames!  The application may be doing too much work on its main thread.
11-09 06:12:51.841: I/Choreographer(1792): Skipped 45 frames!  The application may be doing too much work on its main thread.
11-09 06:12:52.561: I/Choreographer(1792): Skipped 78 frames!  The application may be doing too much work on its main thread.
11-09 06:12:52.731: I/Choreographer(1792): Skipped 44 frames!  The application may be doing too much work on its main thread.
11-09 06:12:54.050: I/Choreographer(1792): Skipped 340 frames!  The application may be doing too much work on its main thread.
11-09 06:12:54.230: I/Choreographer(1792): Skipped 47 frames!  The application may be doing too much work on its main thread.
11-09 06:12:54.570: I/Choreographer(1792): Skipped 76 frames!  The application may be doing too much work on its main thread.
11-09 06:12:56.263: I/Choreographer(1792): Skipped 438 frames!  The application may be doing too much work on its main thread.
11-09 06:12:56.570: I/Choreographer(1792): Skipped 70 frames!  The application may be doing too much work on its main thread.
11-09 06:12:57.830: E/WindowManager(1792): Activity com.example.numero.SignUpActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40e2e678 V.E..... R.....ID 0,0-456,144} that was originally added here
11-09 06:12:57.830: E/WindowManager(1792): android.view.WindowLeaked: Activity com.example.numero.SignUpActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40e2e678 V.E..... R.....ID 0,0-456,144} that was originally added here
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-09 06:12:57.830: E/WindowManager(1792):  at android.app.Dialog.show(Dialog.java:281)
11-09 06:12:57.830: E/WindowManager(1792):  at com.example.numero.SignUpActivity$CreateNewProduct.onPreExecute(SignUpActivity.java:295)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.AsyncTask.execute(AsyncTask.java:534)
11-09 06:12:57.830: E/WindowManager(1792):  at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:163)
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.View.performClick(View.java:4204)
11-09 06:12:57.830: E/WindowManager(1792):  at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:57.830: E/WindowManager(1792):  at android.os.Looper.loop(Looper.java:137)
11-09 06:12:57.830: E/WindowManager(1792):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:57.830: E/WindowManager(1792):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:57.830: E/WindowManager(1792):  at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:57.830: E/WindowManager(1792):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:57.830: E/WindowManager(1792):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:57.830: E/WindowManager(1792):  at dalvik.system.NativeStart.main(Native Method)
11-09 06:12:57.861: I/Choreographer(1792): Skipped 92 frames!  The application may be doing too much work on its main thread.
11-09 06:12:58.062: E/ActivityThread(1792): Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$2@40d4eed0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.062: E/ActivityThread(1792): android.app.IntentReceiverLeaked: Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$2@40d4eed0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.sendSMS(SignUpActivity.java:205)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.access$3(SignUpActivity.java:193)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:117)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.view.View.performClick(View.java:4204)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.os.Looper.loop(Looper.java:137)
11-09 06:12:58.062: E/ActivityThread(1792):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:58.062: E/ActivityThread(1792):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:58.062: E/ActivityThread(1792):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:58.062: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:58.062: E/ActivityThread(1792):     at dalvik.system.NativeStart.main(Native Method)
11-09 06:12:58.140: E/ActivityThread(1792): Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$3@40e35ab0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.140: E/ActivityThread(1792): android.app.IntentReceiverLeaked: Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$3@40e35ab0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.sendSMS(SignUpActivity.java:235)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.access$3(SignUpActivity.java:193)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:117)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.view.View.performClick(View.java:4204)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.os.Looper.loop(Looper.java:137)
11-09 06:12:58.140: E/ActivityThread(1792):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:58.140: E/ActivityThread(1792):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:58.140: E/ActivityThread(1792):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:58.140: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:58.140: E/ActivityThread(1792):     at dalvik.system.NativeStart.main(Native Method)
11-09 06:12:58.291: E/ActivityThread(1792): Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$2@40e357c0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.291: E/ActivityThread(1792): android.app.IntentReceiverLeaked: Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$2@40e357c0 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.sendSMS(SignUpActivity.java:205)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.access$3(SignUpActivity.java:193)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:117)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.view.View.performClick(View.java:4204)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.os.Looper.loop(Looper.java:137)
11-09 06:12:58.291: E/ActivityThread(1792):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:58.291: E/ActivityThread(1792):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:58.291: E/ActivityThread(1792):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:58.291: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:58.291: E/ActivityThread(1792):     at dalvik.system.NativeStart.main(Native Method)
11-09 06:12:58.350: D/dalvikvm(1792): GC_CONCURRENT freed 480K, 14% free 3822K/4412K, paused 77ms+114ms, total 413ms
11-09 06:12:58.400: E/ActivityThread(1792): Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$3@40d4f558 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.400: E/ActivityThread(1792): android.app.IntentReceiverLeaked: Activity com.example.numero.SignUpActivity has leaked IntentReceiver com.example.numero.SignUpActivity$3@40d4f558 that was originally registered here. Are you missing a call to unregisterReceiver()?
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.sendSMS(SignUpActivity.java:235)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.example.numero.SignUpActivity.access$3(SignUpActivity.java:193)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.example.numero.SignUpActivity$1.onClick(SignUpActivity.java:117)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.view.View.performClick(View.java:4204)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.view.View$PerformClick.run(View.java:17355)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.os.Handler.handleCallback(Handler.java:725)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.os.Looper.loop(Looper.java:137)
11-09 06:12:58.400: E/ActivityThread(1792):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-09 06:12:58.400: E/ActivityThread(1792):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 06:12:58.400: E/ActivityThread(1792):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-09 06:12:58.400: E/ActivityThread(1792):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-09 06:12:58.400: E/ActivityThread(1792):     at dalvik.system.NativeStart.main(Native Method)
11-09 06:13:08.551: I/Process(1792): Sending signal. PID: 1792 SIG: 9
4

3 回答 3

1

这不是答案,我发现了一些问题,所以我重写了你的 php 文件。除最后一条消息外,所有作为 json 对象返回的响应可能是您的 android 应用程序关闭问题的原因。

                // array for JSON response
            $response = array();

            // check for required fields
                    if (isset($_POST['name']) && isset($_POST['password']) && isset($_POST['mobile'])) {

                            $name = $_POST['name'];
                            $password = $_POST['password'];
                            $mobile = $_POST['mobile'];

                            // include db connect class
                            require_once __DIR__ . '/db_connect.php';

                            // connecting to db
                            $db = new DB_CONNECT();


                             $result1 = mysql_query("SELECT name FROM numerol WHERE name = '$name'");                                   

                             if (mysql_num_rows($result1)==0) {

                                    // mysql inserting a new row
                                    $result = mysql_query("INSERT INTO numerol(name, password, mobile) VALUES('$name', '$password', '$mobile')");

                                    // check if row inserted or not
                                    if ($result) {
                                        // successfully inserted into database
                                        $response["success"] = 1;
                                        $response["message"] = "Product successfully created.";                                 

                                    } else {
                                        // failed to insert row
                                        $response["success"] = 0;
                                        $response["message"] = "Oops! An error occurred.";                                  

                                    }
                                } else {
                                    // required field is missing
                                    $response["success"] = 0;
                                    $response["message"] = "User Name already exists, Please choose another username";


                                }


                        }else{          
                            $response["success"] = 0;
                            $response["message"] = "Required field(s) is missing";      


                        }

                        echo json_encode($response);

注意使用 mysqli_* 函数而不是 mysql_* 函数(已弃用)

于 2013-11-09T06:20:22.383 回答
1

在 asyncTask 中,您不能在 doInBackground 函数中调用 Toast ...您可以在 onPostExecute(String result) 中显示您的 toast,因此您的代码应该是这样的。

if (success == 1) {
                // successfully created product




                Intent i = new Intent(SignUpActivity.this, RegisterActivity.class);

                i.putExtra("number", sms + "");
                startActivity(i);

                //closing this screen
                //finish();
            } else {
                // failed to create product


                return "false";
            }
            protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
                    pDialog.dismiss();
                    if (result == "false")
        Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();

    }

在 doInBackgound 中,您只处理数据,但不能更改活动的 UI。如果您想更改 UI,您可以在onPreExecute()onPostExecute(String result)

编辑!!

ahhhhhhhhh请删除这个块

protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }

并用我的部分代码替换它

这是

protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
       pDialog.dismiss();
                if (result == "false")
    Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();

这应该在 doInBackground 方法之外并在 asynctask 内

所以这一切看起来像这样:

class CreateNewProduct extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SignUpActivity.this);
        pDialog.setMessage("Creating a new account..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {





        String name = editTextUserName.getText().toString();
        String password = editTextPassword.getText().toString();
        String mobile = editMobileNumber.getText().toString();
        String sms = Integer.toString(number);

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("mobile", mobile));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_data,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product




                Intent i = new Intent(SignUpActivity.this, RegisterActivity.class);

                i.putExtra("number", sms + "");
                startActivity(i);

                //closing this screen
                //finish();
            } else {
                // failed to create product
                return "false";

           }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
   pDialog.dismiss();
            if (result == "false")
Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();

}

}

于 2013-11-09T06:12:19.510 回答
0

尝试这个:

/**
 * Background Async Task to Create new product
 * */
class CreateNewProduct extends AsyncTask<String, String, String> {
    int success = 0;
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SignUpActivity.this);
        pDialog.setMessage("Creating a new account..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {





        String name = editTextUserName.getText().toString();
        String password = editTextPassword.getText().toString();
        String mobile = editMobileNumber.getText().toString();
        String sms = Integer.toString(number);

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("mobile", mobile));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_data,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
             success = json.getInt(TAG_SUCCESS);               


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        if (success == 1) {
            // successfully created product




            Intent i = new Intent(SignUpActivity.this, RegisterActivity.class);

            i.putExtra("number", sms + "");
            startActivity(i);

            //closing this screen
            //finish();
        } else {
            // failed to create product
            Toast.makeText(SignUpActivity.this, "User Name already exists. Please choose another user name ", Toast.LENGTH_LONG).show();


        }
        // dismiss the dialog once done         
        pDialog.dismiss();
    }

}
于 2013-11-09T06:49:31.473 回答