1

パスワード変更機能を作ってみました。php は問題ないと思います。したがって、Androidコードに問題があります。今、私はアンドロイドがAsyncTaskの問題に問題を抱えている理由を知りません。それは私の活動コードです。誰かが私に教えたり、助けてくれることを願っています。

 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();
    }
}
}

それが私の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)
4

3 に答える 3

1

バックグラウンド スレッドでは UI 関連の操作を実行できないため、バックグラウンド スレッドでChangeErrorMsg.setText呼び出す必要があります。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();
}
于 2013-03-19T14:46:57.930 に答える
1

あなたの問題は、ビュー、特にEditTextAsyncTask のバックグラウンド スレッドからアクセスしようとしていることです。おそらく、AsyncTask のパラメーターとしてそれらを送信する必要があります。

いくつかの場所でビューに触れています。例えば:

protected String doInBackground(String... args) {

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

これは大きなノーノーです。

protected String doInBackground(String... args) {

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

例えば new YourAsyncTask().execute(password,newpassword);

編集: 明確化、バックグラウンド スレッドで UI/ビュー処理を行わないでください。つまり、ビューに触れたり、トーストを送信したりしないでください。パラメータを送信するか、実行前または実行後に実行します。

于 2013-03-19T14:49:05.033 に答える
0

doInBackground()メソッド内のケースで、バックグラウンド スレッドから UI コンポーネント (ビュー) にアクセスしようとしています。

onPostExecuteUIスレッドで実行されるものを使用できます。

于 2013-03-19T14:47:51.140 に答える