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

  }
}

我尝试运行但出错。

这是我的日志。

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 元素从 移动doInBackgroundonPostExecute以更新 UidoInBackground执行完成

于 2013-01-02T09:55:16.833 回答
0

问题在于以下几行:

loginErrorMsg.setText("");

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

您不能从非 UI 线程更新 UI。而且由于doInBackground()在非 UI 线程中执行,因此您无法从该方法更新 UI..

使用Handler从非 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 回答
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 回答