AsyncTask类中的错误无法发送数据

时间:2012-09-14 11:21:38

标签: android android-asynctask

我在android平台上工作,想要将一些注册详细信息发送到php页面,因为我创建了一个名为'ContinueRegister'的活动,它包含一个AsyncTask类。在运行我的项目时,显示它已停止,并且在logcat中存在一些错误,如

 Activity com.opz.ContinueRegister has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41269db8     
        error opening trace file: No such file or directory
        at android.os.Looper.loop(Looper.java:137)
        at android.os.Handler.handleCallback(Handler.java:615)
        atcom.opz.ContinueRegister$CreateNewUser.onPreExecute(ContinueRegister.java:76)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
        at java.lang.reflect.Method.invokeNative(Native Method)

这是我的班级文件

public class ContinueRegister extends Activity {

    // Progress Dialog
        private ProgressDialog pDialog;

        JSONParser jsonParser = new JSONParser();

    EditText firstname;
    EditText lastname;
    EditText dob;
    EditText gender;

    RegisterActivity ra= new RegisterActivity();

// url to create new product
    private static String url_create_product = "http://localhost/login_api/create_account.php/";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";

   @Override    
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        // Set View to register.xml
       setContentView(R.layout.registerfinsh);

             Button bt=(Button)findViewById(R.id.btnRegister);

       // Listening to Login Screen link
       bt.setOnClickListener(new View.OnClickListener(){

            public void onClick(View arg0) {

                    new CreateNewUser().execute();
                    }
            });

            }


            /**
     * Background Async Task to Create new product
     * */

class CreateNewUser extends AsyncTask<String, String, String> {
/**
 * Before starting background thread Show Progress Dialog
 * */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ContinueRegister.this);
pDialog.setMessage("Creating New User..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

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

     String Username = ra.username.getText().toString();
     String Email = ra.email.getText().toString();
     String Password =ra.password.getText().toString();
     firstname = (EditText)findViewById(R.id.first_name);
     lastname =(EditText)findViewById(R.id.last_name);
     dob =(EditText)findViewById(R.id.date_of_birth);
     gender = (EditText)findViewById(R.id.gender);
     String Firstname = firstname.getText().toString();
     String Lastname = lastname.getText().toString();
     String Dob =dob.getText().toString();
     String Gender =gender.getText().toString();

            // Building Parameters

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("Username", Username));
            params.add(new BasicNameValuePair("Email", Email));
            params.add(new BasicNameValuePair("Password", Password));
            params.add(new BasicNameValuePair("Firstname", Firstname));
            params.add(new BasicNameValuePair("Lastname", Lastname));
            params.add(new BasicNameValuePair("Dob", Dob));
            params.add(new BasicNameValuePair("Gender", Gender));

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

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

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

                if (success == 1) {
                    // successfully created user
                    Intent i = new Intent(getApplicationContext(), FinishSignupActivity.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create user
                }
            } 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();


       TextView loginScreen = (TextView) findViewById(R.id.link_to_login2);

       loginScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent m = new Intent(getApplicationContext(),LoginActivity.class);
            startActivity(m);

        }
    });
   }
 }
}

请帮我修复此错误

3 个答案:

答案 0 :(得分:1)

String url_create_product = "http://10.0.2.2/login_api/create_account.php/";

使用您的本地IP,例如192.168.0.1

String url_create_product = "http://192.168.0.1/login_api/create_account.php/";

另请检查login_api文件夹中的文件名是create_account.php

还有一件事是Never Access Any view inside doinBackGround(...)

因为doinBackGround method is non-UI thread.

答案 1 :(得分:0)

我们必须提供完整的网址,例如192.10.1.000

您可以使用localhost检查窗口。但是您必须在上面的行中提供完整的URL。

要知道你的ipaddress打开cmd提示符。并输入ipconfig

答案 2 :(得分:0)

将用于调用intent的代码放在onPostexecute

转到另一个活动永远不是后台任务