1

このチュートリアルを試してみます

PHP、MySQL、SQLiteでログインすると機能します。

しかし、ログインの進行中に進行状況ダイアログを追加したいと思います。

これは、asynctaskを追加する前の私のログインアクティビティであり、機能します

package com.app.DatabaseSample;


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

import android.app.Activity;
import android.content.Intent;
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.app.library.DatabaseHandler;
import com.app.library.UserFunctions;

public class LoginActivity extends Activity {
    Button btnLogin;
    Button btnLinkToRegister;
    EditText inputUsername;
    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
        inputUsername = (EditText) findViewById(R.id.loginUsername);
        inputPassword = (EditText) findViewById(R.id.loginPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        loginErrorMsg = (TextView) findViewById(R.id.login_error);

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

            public void onClick(View view) {
                String email = inputUsername.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));                        

                            // Launch Dashboard Screen
                            Intent dashboard = new Intent(getApplicationContext(), DatabaseSample.class);

                            // Close all views before launching Dashboard
                            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard);

                            // Close Login Screen
                            finish();
                        }else{
                            // Error in login
                            loginErrorMsg.setText("Incorrect username/password");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

}

そして私がasyntaskで編集した後

私のログインアクティビティがあります

package com.app.DatabaseSample;


import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
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.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.app.library.DatabaseHandler;
import com.app.library.JSONParser;
import com.app.library.UserFunctions;



public class LoginActivity extends Activity {
    Button btnLogin;
    Button btnLinkToRegister;
    EditText inputUsername;
    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
        inputUsername = (EditText) findViewById(R.id.loginUsername);
        inputPassword = (EditText) findViewById(R.id.loginPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        loginErrorMsg = (TextView) findViewById(R.id.login_error);

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

            public void onClick(View view) {
                readLogin(view);
            }
        });

    }

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

        private ProgressDialog dialog;
        private ProgressDialog pDialog;
        protected Context applicationContext;


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(LoginActivity.this);
            pDialog.setMessage("Loading User ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            inputUsername = (EditText) findViewById(R.id.loginUsername);
            inputPassword = (EditText) findViewById(R.id.loginPassword);
            String email = inputUsername.getText().toString();
            String password = inputPassword.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.loginUser(email, password);

            Log.d("Button", "Login");

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

                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(getApplicationContext(), DatabaseSample.class);

                        // Close all views before launching Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);

                        // Close Login Screen
                        finish();
                    }else{
                        // Error in login
                        loginErrorMsg.setText("Incorrect username/password");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    Intent dashboard = new Intent(getApplicationContext(), DatabaseSample.class);
                    dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(dashboard);

                    // Close Login Screen
                    finish();
                }
            });

        }
      }

    public void readLogin(View view) {
        new ProcessLogin().execute();

    // check for login response

  }
}

実行しようとしましたが、エラーが発生しました。

これは私のlogcatです。

01-02 12:10:42.445: ERROR/JSON(4021): {"tag":"login","success":0,"error":1,"error_msg":"Incorrect email or password!"}
01-02 12:10:42.453: DEBUG/Button(4021): Login
01-02 12:10:42.453: WARN/dalvikvm(4021): threadid=9: thread exiting with uncaught exception (group=0x40015560)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021): FATAL EXCEPTION: AsyncTask #1
01-02 12:10:42.460: ERROR/AndroidRuntime(4021): java.lang.RuntimeException: An error occured while executing doInBackground()
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at java.lang.Thread.run(Thread.java:1019)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.view.View.invalidate(View.java:5279)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.widget.TextView.checkForRelayout(TextView.java:5528)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.widget.TextView.setText(TextView.java:2730)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.widget.TextView.setText(TextView.java:2598)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.widget.TextView.setText(TextView.java:2573)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at com.app.DatabaseSample.LoginActivity$ProcessLogin.doInBackground(LoginActivity.java:99)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at com.app.DatabaseSample.LoginActivity$ProcessLogin.doInBackground(LoginActivity.java:1)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-02 12:10:42.460: ERROR/AndroidRuntime(4021):     ... 4 more
01-02 12:10:42.468: WARN/ActivityManager(1308):   Force finishing activity com.app.DatabaseSample/.LoginActivity
01-02 12:10:42.585: WARN/IInputConnectionWrapper(4021): showStatusIcon on inactive InputConnection
01-02 12:10:42.585: WARN/InputManagerService(1308): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40683810 (uid=10040 pid=4021)
01-02 12:10:43.320: ERROR/WindowManager(4021): Activity com.app.DatabaseSample.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40529e48 that was originally added here
01-02 12:10:43.320: ERROR/WindowManager(4021): android.view.WindowLeaked: Activity com.app.DatabaseSample.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40529e48 that was originally added here
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.view.ViewRoot.<init>(ViewRoot.java:258)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.app.Dialog.show(Dialog.java:241)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at com.app.DatabaseSample.LoginActivity$ProcessLogin.onPreExecute(LoginActivity.java:83)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.os.AsyncTask.execute(AsyncTask.java:391)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at com.app.DatabaseSample.LoginActivity.readLogin(LoginActivity.java:156)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at com.app.DatabaseSample.LoginActivity$1.onClick(LoginActivity.java:63)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.view.View.performClick(View.java:2485)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.view.View$PerformClick.run(View.java:9080)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.os.Handler.handleCallback(Handler.java:587)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.os.Looper.loop(Looper.java:130)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at java.lang.reflect.Method.invoke(Method.java:507)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-02 12:10:43.320: ERROR/WindowManager(4021):     at dalvik.system.NativeStart.main(Native Method)

ログインアクティビティに進行状況ダイアログを追加するにはどうすればよいですか?

BR

アレックス

4

4 に答える 4

3

CalledFromWrongThreadException:ビュー階層を作成した元のスレッドのみがそのビューにアクセスできます。

現在、ProcessLoginのdoInBackgroundからUI要素にアクセスしようとしているためです AsyncTask

すべてのUi要素をからdoInBackgroundに移動するだけonPostExecuteで、UiのdoInBackground実行が完了します。

于 2013-01-02T09:55:16.833 に答える
0

次のようなAsyncTaskメソッドを使用してください。

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

    private ProgressDialog dialog;
    private ProgressDialog pDialog;
    protected Context applicationContext;


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Loading User ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        inputUsername = (EditText) findViewById(R.id.loginUsername);
        inputPassword = (EditText) findViewById(R.id.loginPassword);
        String email = inputUsername.getText().toString();
        String password = inputPassword.getText().toString();
        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.loginUser(email, password);

        Log.d("Button", "Login");

 runOnUiThread(new Runnable() {
            public void run() {


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

                    // Launch Dashboard Screen
                    Intent dashboard = new Intent(getApplicationContext(), DatabaseSample.class);

                    // Close all views before launching Dashboard
                    dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(dashboard);

                    // Close Login Screen
                    finish();
                }else{
                    // Error in login
                    loginErrorMsg.setText("Incorrect username/password");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

            }
        });



        return null;

    }

    @Override
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread


    }
  }
于 2013-01-02T10:02:07.523 に答える
0

問題は次の行にあります。

loginErrorMsg.setText("");

loginErrorMsg.setText("Incorrect username/password");

非UIスレッドからUIを更新することはできません。また、doInBackground()UI以外のスレッドで実行されるため、そのメソッドからUIを更新することはできません。

ハンドラーを使用して、UI以外のスレッドからUIを更新します...

于 2013-01-02T09:57:09.983 に答える
0
                     pDialog.dismiss();


                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    Intent dashboard = new Intent(getApplicationContext(),                   
                      DatabaseSample.class);
                    dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(dashboard);

                    // Close Login Screen
                    finish();

このコードは、LoginActivityクラスの関数内にある必要があります。

また、asynctaskを呼び出すときは、LoginActivityのオブジェクトをそのコンストラクターに渡します。

タスクの完了後、オブジェクトを使用します

于 2013-01-02T09:58:48.897 に答える