1

使用したチュートリアル リンク: http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

問題は次のとおりです。アプリケーションのログインおよびサインアップ アクティビティが機能しています。ただし、ユーザーがログイン/サインアップボタンをクリックして情報を表示した後、警告ダイアログを表示したいと考えています。

上記のチュートリアル リンクに従い、AlertDialogManager.java を作成しました。これはログインボタンの私のコードです:

try{                                                
            int success = json.getInt(TAG_SUCCESS);
            //String message = json.getString(TAG_MESSAGE);
            //Log.v("SignUp", "Checkpoint 3");


            if (success == 1) {
                session.createLoginSession(email);

                Intent i = new Intent(getApplicationContext(), Home.class);
                startActivity (i);

                finish();
            }else{
                //failed to login                   
            }
        } catch (JSONException e){
            e.printStackTrace();
        }

アプリケーションがユーザーのログインに失敗したときに、セクションにアラートダイアログコードを追加しようとしました。私のアラートダイアログコード:

alert.showAlertDialog(Login.this,"Login failed", "Bla bla bla", false);

ログインボタンをクリックしようとすると、アプリケーション全体がクラッシュし、以下の logcat が表示されます。

05-15 10:08:55.641: W/dalvikvm(2338): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
05-15 10:08:55.701: E/AndroidRuntime(2338): FATAL EXCEPTION: AsyncTask #1
05-15 10:08:55.701: E/AndroidRuntime(2338): java.lang.RuntimeException: An error occured while executing doInBackground()
05-15 10:08:55.701: E/AndroidRuntime(2338):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-15 10:08:55.701: E/AndroidRuntime(2338): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

私はまだ Android プログラミングに慣れていないので >< 誰か助けてくれませんか? ありがとう!他に必要なことを教えてください。ここに投稿します。

コード全体は次のとおりです。 public class Login extends Activity {

private ProgressDialog pDialog;

//Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();

//Session manager
SessionManager session;

JSONParser jsonParser = new JSONParser();
EditText C_Email;
EditText C_Password;

public static final String DOMAIN = "192.168.0.112"; //Home
//public static final String DOMAIN = "172.18.74.146";
//URL to login
private static String url_login = "http://" + DOMAIN + "/iChop/login.php";

//JSON Node names
private static final String TAG_SUCCESS = "success";
//private static final String TAG_MESSAGE = "message";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    //Session manager
    session = new SessionManager(getApplicationContext());

    //Edit Text
    C_Email = (EditText) findViewById(R.id.C_Email);
    C_Password = (EditText) findViewById(R.id.C_Password);

    Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();

    //Login button
    Button Login = (Button) findViewById(R.id.Login);

    //Button click event
    Login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new customer in background
            new CustomerLogin().execute();
        }
    });

}


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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Login.this);
        pDialog.setMessage("Logging you in!");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... args){
        String email = C_Email.getText().toString();
        String password = C_Password.getText().toString();

        //Building parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));

        /**
        Log.d("C_Email", email);
        Log.d("C_Password", password);
        **/

        //getting JSON object
        JSONObject json = jsonParser.makeHttpRequest(url_login, "POST", params);
        Log.v("SignUp", "Checkpoint 1");

        //check log cat for response
        Log.d("Create Response", json.toString());
        Log.v("SignUp", "Checkpoint 2");

        //check for success tag
        try{                                                
            int success = json.getInt(TAG_SUCCESS);
            //String message = json.getString(TAG_MESSAGE);
            //Log.v("SignUp", "Checkpoint 3");


            if (success == 1) {
                session.createLoginSession(email);

                Intent i = new Intent(getApplicationContext(), Home.class);
                startActivity (i);

                finish();
            }else{
                //failed to login

            }
        } catch (JSONException e){
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url){
        pDialog.dismiss();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

public void SignUp (View view) {
    Intent signup = new Intent (this, SignUp.class);
    startActivity(signup);
}
}
4

3 に答える 3

1

でインテントとアラート ダイアログを呼び出しているようです。doInBackgroundおそらく でインテントまたはアラート ダイアログを呼び出す必要がありますOnPostExceute

OnPostExecute でこれを実行してみてください

if (success == 1) {
                session.createLoginSession(email);

                Intent i = new Intent(getApplicationContext(), Home.class);
                startActivity (i);

                finish();
            }else{
                alert.showAlertDialog(Login.this,"Login failed", "Bla bla bla", false);

            }

それが機能するかどうか教えてください。

于 2013-05-15T10:57:22.147 に答える
0

このコードをbutton.setOnClickListener()

Runnable runnable = new Runnable() {
            @Override
            public void run() {
                handler.post(new Runnable() { // This thread runs in the UI
                    @Override
                    public void run() {
                         // Update the UI, call your asynctask
                    }
                });
            }
        };
        new Thread(runnable).start();
    }
于 2013-05-15T10:44:58.513 に答える