Domanda

i try to create a change password function. i think php may not have a problem. therefore, i have a problem on android code. now,i don't know why android have a problem on AsyncTask problem. it is my activity code. i hope someone can teach me or help me.

 public class changpassword extends Activity{

Button btnChange;
EditText inputPassword;
EditText inputnewPassword;
EditText inputrenewPassword;
TextView ChangeErrorMsg;
String username;

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_NAME = "name";

// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();

// url to get all orders list
private static String url_change_password = "http://10.0.2.2/android_login_api4/include/changepassword.php";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.changepassword);

    //get id
    Bundle bundle = this.getIntent().getExtras();       
    username = bundle.getString(KEY_NAME);

    // Importing all assets like buttons, text fields
    inputPassword = (EditText) findViewById(R.id.current_password);
    inputnewPassword = (EditText) findViewById(R.id.new_password);
    inputrenewPassword = (EditText) findViewById(R.id.renew_password);
    btnChange = (Button) findViewById(R.id.btnChange);
    ChangeErrorMsg = (TextView) findViewById(R.id.change_error);

    // Login button Click Event
    btnChange.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (inputnewPassword.getText().toString().equals(inputrenewPassword.getText().toString())){

                 // starting background task to change password
                  new Change_Password().execute();
            }else {
                ChangeErrorMsg.setText("Passwords do not match");
            }
        }

    });


}

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

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

    /**
     * getting All orders from url
     * */



    protected String doInBackground(String... args) {

        // getting password from EditTexts
        String password = inputPassword.getText().toString();
        String newpassword = inputnewPassword.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username", username ));
        params.add(new BasicNameValuePair("oldpassword", password));
        params.add(new BasicNameValuePair("newpassword", newpassword));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_change_password, "POST", params);



        try {

            // Checking for SUCCESS TAG
            int success = json.getInt(KEY_SUCCESS);

            if (success == 1) {

                    ChangeErrorMsg.setText("Successsful change ");
                }else{
                    // Error in login
                    ChangeErrorMsg.setText("Your current password was incorrect.");
                }
        } 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 product uupdated
        pDialog.dismiss();
    }
}
}

that is my logcat

 03-19 14:27:41.188: E/AndroidRuntime(659): FATAL EXCEPTION: AsyncTask #1
 03-19 14:27:41.188: E/AndroidRuntime(659): java.lang.RuntimeException: An error      occured while executing doInBackground()
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at java.lang.Thread.run(Thread.java:1096)
 03-19 14:27:41.188: E/AndroidRuntime(659): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.view.View.invalidate(View.java:5139)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.widget.TextView.checkForRelayout(TextView.java:5364)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.widget.TextView.setText(TextView.java:2688)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.widget.TextView.setText(TextView.java:2556)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.widget.TextView.setText(TextView.java:2531)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at com.example.androidhive.changpassword$Change_Password.doInBackground(changpassword.java:132)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at com.example.androidhive.changpassword$Change_Password.doInBackground(changpassword.java:1)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
 03-19 14:27:41.188: E/AndroidRuntime(659):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 03-19 14:27:41.188: E/AndroidRuntime(659):     ... 4 more
 03-19 14:27:41.889: E/WindowManager(659): Activity com.example.androidhive.changpassword has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43e617f8 that was originally added here
 03-19 14:27:41.889: E/WindowManager(659): android.view.WindowLeaked: Activity com.example.androidhive.changpassword has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43e617f8 that was originally added here
 03-19 14:27:41.889: E/WindowManager(659):  at android.view.ViewRoot.<init>(ViewRoot.java:247)
 03-19 14:27:41.889: E/WindowManager(659):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
 03-19 14:27:41.889: E/WindowManager(659):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
 03-19 14:27:41.889: E/WindowManager(659):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
 03-19 14:27:41.889: E/WindowManager(659):  at android.app.Dialog.show(Dialog.java:241)
 03-19 14:27:41.889: E/WindowManager(659):  at com.example.androidhive.changpassword$Change_Password.onPreExecute(changpassword.java:94)
 03-19 14:27:41.889: E/WindowManager(659):  at android.os.AsyncTask.execute(AsyncTask.java:391)
 03-19 14:27:41.889: E/WindowManager(659):  at com.example.androidhive.changpassword$1.onClick(changpassword.java:71)
 03-19 14:27:41.889: E/WindowManager(659):  at android.view.View.performClick(View.java:2408)
 03-19 14:27:41.889: E/WindowManager(659):  at android.view.View$PerformClick.run(View.java:8816)
 03-19 14:27:41.889: E/WindowManager(659):  at      android.os.Handler.handleCallback(Handler.java:587)
 03-19 14:27:41.889: E/WindowManager(659):  at android.os.Handler.dispatchMessage(Handler.java:92)
 03-19 14:27:41.889: E/WindowManager(659):  at android.os.Looper.loop(Looper.java:123)
 03-19 14:27:41.889: E/WindowManager(659):  at android.app.ActivityThread.main(ActivityThread.java:4627)
 03-19 14:27:41.889: E/WindowManager(659):  at java.lang.reflect.Method.invokeNative(Native Method)
 03-19 14:27:41.889: E/WindowManager(659):  at java.lang.reflect.Method.invoke(Method.java:521)
 03-19 14:27:41.889: E/WindowManager(659):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 03-19 14:27:41.889: E/WindowManager(659):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 03-19 14:27:41.889: E/WindowManager(659):  at dalvik.system.NativeStart.main(Native Method)
È stato utile?

Soluzione

You can't perform ui related operations in the background thread, so ChangeErrorMsg.setText must be called in onPostExecute

protected String doInBackground(String... args) {
    // getting password from EditTexts
    String password = inputPassword.getText().toString();
    String newpassword = inputnewPassword.getText().toString();

    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("username", username ));
    params.add(new BasicNameValuePair("oldpassword", password));
    params.add(new BasicNameValuePair("newpassword", newpassword));
    // getting JSON string from URL
    JSONObject json = jParser.makeHttpRequest(url_change_password, "POST", params);         

    try {
        // Checking for SUCCESS TAG
        int success = json.getInt(KEY_SUCCESS);

        if (success == 1) {
            return "Successsful change";
        }

        return "Your current password was incorrect.";
    } 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 product uupdated
    ChangeErrorMsg.setText(file_url);
    pDialog.dismiss();
}

Altri suggerimenti

Your problem is that you are trying to reach your Views, specifically your EditText from a background thread in your AsyncTask. You should probably send them as parameters for your AsyncTask.

You are touching the views in several places. For instance:

protected String doInBackground(String... args) {

    // getting password from EditTexts
    String password = inputPassword.getText().toString();
    String newpassword = inputnewPassword.getText().toString();

Which is a big no no.

protected String doInBackground(String... args) {

    // getting password from args
    String password = args[0];
    String newpassword = args[1];

For instance new YourAsyncTask().execute(password,newpassword);

Edit: Clarification, don't do any UI/view handling in the background thread, aka don't touch views or send Toasts. Either send parameters or do that pre- or postExecute.

You are trying to access UI components (View) from a background thread in your case inside the doInBackground() method.

You can use onPostExecute which runs on the UI thread.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top