1

私はアンドロイドの初心者で、ロギング用の小さなアプリケーションを作成しようとしています。ここで良いチュートリアルを見つけましたが、非同期タスクで実行する必要があるため、うまくいきません。いくつかの変更を試みましたが、何も機能しません:(.there is my code:

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.widget.TextView;

import com.example.androidhive.library.DatabaseHandler;
import com.example.androidhive.library.UserFunctions;


public class LoginActivity extends Activity {
Button btnLogin;
Button btnLinkToRegister;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;

// 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.login);

    // Importing all assets like buttons, text fields
    inputEmail = (EditText) findViewById(R.id.loginEmail);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
    loginErrorMsg = (TextView) findViewById(R.id.login_error);

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

        public void onClick(View view) {
            new LogTask().execute();    
        }

    });

    // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),RegisterActivity.class);
            startActivity(i);
            finish();
        }
    });
}

private class LogTask extends AsyncTask<String, String, String> {


    protected String doInBackground(String... args) {
        String email = inputEmail.getText().toString();
        String password = inputPassword.getText().toString();
        UserFunctions userFunction = new UserFunctions();
        Log.d("Button", "Login");
        JSONObject json = userFunction.loginUser(email, password);

        // check for login response
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                loginErrorMsg.setText("");
                String res = json.getString(KEY_SUCCESS); 
                if(Integer.parseInt(res) == 1){
                    // user successfully logged in
                    // 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));                        


                }else{
                    // Error in login
                    return null;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return "done";
    }


    protected void onPostExecute(String file_url) {

        if(file_url==null) {
              loginErrorMsg.setText("Incorrect username/password");
       } else if(file_url.equals("done")) {
              Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
              dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              startActivity(dashboard);
              finish();
       }

    }

}

}

私はこのエラーがありました:

01-20 10:10:01.005: I/dalvikvm(2285): threadid=3: reacting to signal 3
01-20 10:10:01.425: I/dalvikvm(2285): Wrote stack traces to '/data/anr/traces.txt'
01-20 10:10:01.505: I/dalvikvm(2285): threadid=3: reacting to signal 3
01-20 10:10:01.575: I/dalvikvm(2285): Wrote stack traces to '/data/anr/traces.txt'
01-20 10:10:02.325: I/dalvikvm(2285): threadid=3: reacting to signal 3
01-20 10:10:02.465: I/dalvikvm(2285): Wrote stack traces to '/data/anr/traces.txt'
01-20 10:10:02.499: D/gralloc_goldfish(2285): Emulator without GPU emulation detected.
01-20 10:10:02.985: I/dalvikvm(2285): threadid=3: reacting to signal 3
01-20 10:10:03.095: I/dalvikvm(2285): Wrote stack traces to '/data/anr/traces.txt'
01-20 10:10:24.085: D/Button(2285): Login
01-20 10:10:24.895: E/JSON(2285): {"tag":"login","success":1,"error":0,"uid":"50ee5c6c65dd38.71886880","user":{"name":"s","email":"s","created_at":"2013-01-10 07:15:08","updated_at":null}}
01-20 10:10:24.905: W/dalvikvm(2285): threadid=12: thread exiting with uncaught exception (group=0x409c01f8)
01-20 10:10:24.985: E/AndroidRuntime(2285): FATAL EXCEPTION: AsyncTask #1
01-20 10:10:24.985: E/AndroidRuntime(2285): java.lang.RuntimeException: An error occured while executing doInBackground()
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at java.lang.Thread.run(Thread.java:856)
01-20 10:10:24.985: E/AndroidRuntime(2285): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:722)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:771)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4005)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.view.View.invalidate(View.java:8576)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.view.View.invalidate(View.java:8527)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.widget.TextView.checkForRelayout(TextView.java:6760)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.widget.TextView.setText(TextView.java:3306)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.widget.TextView.setText(TextView.java:3162)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.widget.TextView.setText(TextView.java:3137)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at com.example.androidhive.LoginActivity$ChartTask2.doInBackground(LoginActivity.java:95)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at com.example.androidhive.LoginActivity$ChartTask2.doInBackground(LoginActivity.java:1)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-20 10:10:24.985: E/AndroidRuntime(2285):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-20 10:10:24.985: E/AndroidRuntime(2285):     ... 5 more
01-20 10:10:25.495: W/IInputConnectionWrapper(2285): showStatusIcon on inactive InputConnection
01-20 10:10:28.095: I/Process(2285): Sending signal. PID: 2285 SIG: 9
4

2 に答える 2

2

次のように、UI 関連のコードを に移動してみてくださいonPostExecute()

private class LogTask extends AsyncTask<String, String, String> {
    String email, password.

    protected void onPreExecute() {
        email = inputEmail.getText().toString();
        password = inputPassword.getText().toString();
    }
    protected String doInBackground(String... args) {
        UserFunctions userFunction = new UserFunctions();
        Log.d("Button", "Login");
        JSONObject json = userFunction.loginUser(email, password);

        // check for login response
        try {
            if (json.getString(KEY_SUCCESS) != null) {

                String res = json.getString(KEY_SUCCESS); 
                if(Integer.parseInt(res) == 1){
                    // user successfully logged in
                    // 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));                        


                }else{
                    // Error in login
                    return null;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return "done;
    }

次にonPostExecute()

protected void onPostExecute(String file_url) {

       if(file_url==null) {
              loginErrorMsg.setText("Incorrect username/password");
       } else if)file_url.equals("done") {
              Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
              dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              startActivity(dashboard);
              finish();
       }
}
于 2013-01-20T09:45:16.050 に答える