1

我在 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);

        }
    });
   }
 }
}

请帮我解决这个错误

4

3 回答 3

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

或者

使用您的本地 IP192.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.

于 2012-09-14T11:23:45.997 回答
0

我们必须提供完整的 url,如192.10.1.000.

您可以使用 localhost 签入 windows。但您必须在上面的行中提供完整的 url。

要知道你的 ipaddress 打开 cmd 提示符。和类型ipconfig

于 2012-09-14T11:27:55.433 回答
0

将调用意图的代码放在Postexecute

去另一个活动从来都不是后台任务

于 2012-09-26T09:49:47.117 回答