3

これを実行すると、力が入ります。事はです。データは私のデータベースに送信されます。それは私のデータベースにありますが、logcatを読んだところ、DinBackgroundに問題があると書かれています。ここに私のコードがあります:

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";

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


    // Importing all assets like buttons, text fields
    inputFullName = (EditText) findViewById(R.id.registerName);
    inputEmail = (EditText) findViewById(R.id.registerEmail);
    inputPassword = (EditText) findViewById(R.id.registerPassword);
    btnRegister = (Button) findViewById(R.id.btnRegister);
    btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
    registerErrorMsg = (TextView) findViewById(R.id.register_error);

    // Register Button Click event
    btnRegister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            new CreateNewProduct().execute();
        }
    });


}

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

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

    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
        String name = inputFullName.getText().toString();
        String email = inputEmail.getText().toString();
        String password = inputPassword.getText().toString();
        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.registerUser(name, email, password);

        // check for login response
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                registerErrorMsg.setText("");
                String res = json.getString(KEY_SUCCESS);
                if(Integer.parseInt(res) == 1){
                    // user successfully registred
                    // Store user details in SQLite Database
                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");

                    // Clear all previous data in database
                    userFunction.logoutUser(getApplicationContext());
                    db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
                    // Launch Dashboard Screen

                    // Close Registration Screen
                    finish();
                }else{
                    // Error in registration
                    registerErrorMsg.setText("Error occured in registration");
                }
            }
        } 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();
        Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
        // Close all views before launching Dashboard
        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(dashboard);
    }

}

これが私のlogcatです:

12-02 15:35:33.761: E/AndroidRuntime(671): FATAL EXCEPTION: AsyncTask #1
12-02 15:35:33.761: E/AndroidRuntime(671): java.lang.RuntimeException: An error occured while executing doInBackground()
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
12-02 15:35:33.761: E/AndroidRuntime(671):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-02 15:35:33.761: E/AndroidRuntime(671):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-02 15:35:33.761: E/AndroidRuntime(671):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-02 15:35:33.761: E/AndroidRuntime(671):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-02 15:35:33.761: E/AndroidRuntime(671):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-02 15:35:33.761: E/AndroidRuntime(671):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-02 15:35:33.761: E/AndroidRuntime(671):  at java.lang.Thread.run(Thread.java:856)
12-02 15:35:33.761: E/AndroidRuntime(671): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:722)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:771)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:4005)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.view.View.invalidate(View.java:8576)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.view.View.invalidate(View.java:8527)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.widget.TextView.checkForRelayout(TextView.java:6760)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.widget.TextView.setText(TextView.java:3306)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.widget.TextView.setText(TextView.java:3162)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.widget.TextView.setText(TextView.java:3137)
12-02 15:35:33.761: E/AndroidRuntime(671):  at com.laurenswuyts.find.it.RegisterActivity$CreateNewProduct.doInBackground(RegisterActivity.java:100)
12-02 15:35:33.761: E/AndroidRuntime(671):  at com.laurenswuyts.find.it.RegisterActivity$CreateNewProduct.doInBackground(RegisterActivity.java:1)
12-02 15:35:33.761: E/AndroidRuntime(671):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-02 15:35:33.761: E/AndroidRuntime(671):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-02 15:35:33.761: E/AndroidRuntime(671):  ... 5 more
4

1 に答える 1

0

doInBackground()内のUI 要素にアクセスしています。名前が示すように、doInBackgroud() はバックグラウンド (非 UI) スレッドであり、UI 要素にはアクセスできません。そのため、そのコードを UI スレッド内に配置する必要があります。onPreExecute の UI 要素からユーザー入力を取得し、それらを文字列に格納して、後で doInBackground() で使用します。onPreExecute()

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

    /**
     * Before starting background thread Show Progress Dialog
     * */
    String name, email, password;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(RegisterActivity.this);
        pDialog.setMessage("Loading");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        name = inputFullName.getText().toString();
        email = inputEmail.getText().toString();
        password = inputPassword.getText().toString();

    }

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

        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.registerUser(name, email, password);

        // check for login response
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                registerErrorMsg.setText("");
                String res = json.getString(KEY_SUCCESS);
                if(Integer.parseInt(res) == 1){
                    // user successfully registred
                    // Store user details in SQLite Database
                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");

                    // Clear all previous data in database
                    userFunction.logoutUser(getApplicationContext());
                    db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
                    // Launch Dashboard Screen

                    // Close Registration Screen
                    finish();
                }else{
                    // Error in registration
                    //show error message to user in onPostExecute
                }
            }
        } 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();
        Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
        // Close all views before launching Dashboard
        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(dashboard);
    }

}

さらに、AsyncTask を呼び出すときにパラメーターが間違っています。

として宣言したのでclass CreateNewProduct extends AsyncTask<String, String, String>、AsyncTask を呼び出すときに String を渡す必要があります。

したがって、new CreateNewProduct().execute("");それを呼び出す正しい方法になります。

于 2012-12-02T15:19:29.607 に答える