我是 android 的新手,我正在开发一个通过 PHP 将数据从 android 传递到 MySQL 的应用程序。现在我正在尝试验证登录 ID 和密码。如果未设置任何一个字段或登录 ID 和密码错误我想显示一个警告对话框,通知用户登录 ID 或密码不正确。为此,我编写了以下代码。
public class HomeActivity extends Activity
{
JSONParser jsonParser = new JSONParser();
private ProgressDialog pDialog;
private static String url_check_voter = "http://10.0.2.2/evoting/check_voter.php";
private EditText getUserId;
private EditText getPassword;
private Button btnLogin;//private Button btnRegister;
private TextView lblResult;
private Button btnRegister;
AlertDialog.Builder alertDialogBuilder;
String TAG_SUCCESS="success" ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
getUserId = (EditText)findViewById(R.id.getLoginIdTxt);
getPassword = (EditText)findViewById(R.id.getPasswordTxt);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
// Set Click Listener
btnLogin.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
new CheckVoter().execute();
// Check Login
/* String userid = getUserId.getText().toString();
String password = getPassword.getText().toString();
if(userid.equals(""))
{
if(password.equals(""))
onClick(view);
else
{
lblResult.setText("Wrong password");
}
} else {
lblResult.setText("Username does not exist. Please register.");
}*/
}
});
}
/* public void onClick(View v)
{
if (v.getId() == R.id.btnLogin)
{
Intent intent = new Intent(this, CreateVoterSuccess.class);
startActivity(intent);
}
else
{
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
}
}*/
class CheckVoter extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(HomeActivity.this);
pDialog.setMessage("Verifying Voter. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args)
{
String loginId = getUserId.getText().toString();
String pwd = getPassword.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("loginId", loginId));
params.add(new BasicNameValuePair("pwd", pwd));
// params.add(new BasicNameValuePair("description", description));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_check_voter,
"POST", params);
Log.d("Create Response", json.toString());
// Building Parameters
//List<NameValuePair> params = new ArrayList<NameValuePair>();
// Building Parameters
// getting JSON string from URL
//JSONObject json = jsonParser.makeHttpRequest(url_check_voter, "GET", params);
// Log.d("All Products: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
}else if(success == 2)
{
//Intent i = new Intent(getApplicationContext(), LoginFail.class);
// startActivity(i);
alertDialogBuilder = new AlertDialog.Builder(
HomeActivity.this);
// set title
alertDialogBuilder.setTitle("Incorrect user name or password");
// set dialog message
alertDialogBuilder
.setMessage("click ohk to enter username or password")
.setCancelable(false)
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
HomeActivity.this.finish();
}
});
AlertDialog alert = alertDialogBuilder.create();
}
else
{
//Intent i = new Intent(getApplicationContext(), CreateVoterFail.class);
// startActivity(i);
alertDialogBuilder = new AlertDialog.Builder(
HomeActivity.this);
// set title
alertDialogBuilder.setTitle("Incorrect user name or password");
// set dialog message
alertDialogBuilder
.setMessage("click ohk to enter username or password")
.setCancelable(false)
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
HomeActivity.this.finish();
}
});
AlertDialog alert = alertDialogBuilder.create();
}
}catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
并在此处插入警报框...
else
{
//Intent i = new Intent(getApplicationContext(), LoginFail.class);
// startActivity(i);
alertDialogBuilder = new AlertDialog.Builder(
HomeActivity.this);
// set title
alertDialogBuilder.setTitle("Incorrect user name or password");
// set dialog message
alertDialogBuilder
.setMessage("click ohk to enter username or password")
.setCancelable(false)
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
HomeActivity.this.finish();
}
});
AlertDialog alert = alertDialogBuilder.create();
}
我得到运行时错误日志 cat 如下
09-29 09:54:56.853: D/Create Response(279): {"message":"Product successfully created.","success":0}
09-29 09:54:56.853: W/dalvikvm(279): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
09-29 09:54:56.983: D/dalvikvm(279): GC_FOR_MALLOC freed 2836 objects / 170040 bytes in 118ms
09-29 09:54:56.983: E/AndroidRuntime(279): FATAL EXCEPTION: AsyncTask #1
09-29 09:54:56.983: E/AndroidRuntime(279): java.lang.RuntimeException: An error occured while executing doInBackground()
09-29 09:54:56.983: E/AndroidRuntime(279): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-29 09:54:56.983: E/AndroidRuntime(279): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-29 09:54:56.983: E/AndroidRuntime(279): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-29 09:54:56.983: E/AndroidRuntime(279): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-29 09:54:56.983: E/AndroidRuntime(279): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-29 09:54:56.983: E/AndroidRuntime(279): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-29 09:54:56.983: E/AndroidRuntime(279): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-29 09:54:56.983: E/AndroidRuntime(279): at java.lang.Thread.run(Thread.java:1096)
09-29 09:54:56.983: E/AndroidRuntime(279): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-29 09:54:56.983: E/AndroidRuntime(279): at android.os.Handler.<init>(Handler.java:121)
09-29 09:54:56.983: E/AndroidRuntime(279): at android.app.Dialog.<init>(Dialog.java:101)
09-29 09:54:56.983: E/AndroidRuntime(279): at android.app.AlertDialog.<init>(AlertDialog.java:63)
09-29 09:54:56.983: E/AndroidRuntime(279): at android.app.AlertDialog.<init>(AlertDialog.java:59)
09-29 09:54:56.983: E/AndroidRuntime(279): at android.app.AlertDialog$Builder.create(AlertDialog.java:786)
09-29 09:54:56.983: E/AndroidRuntime(279): at android.app.AlertDialog$Builder.show(AlertDialog.java:801)
09-29 09:54:56.983: E/AndroidRuntime(279): at com.example.evoting.HomeActivity$CheckVoter.doInBackground(HomeActivity.java:220)
09-29 09:54:56.983: E/AndroidRuntime(279): at com.example.evoting.HomeActivity$CheckVoter.doInBackground(HomeActivity.java:1)
09-29 09:54:56.983: E/AndroidRuntime(279): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-29 09:54:56.983: E/AndroidRuntime(279): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-29 09:54:56.983: E/AndroidRuntime(279): ... 4 more
09-29 09:54:57.793: E/WindowManager(279): Activity com.example.evoting.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f00418 that was originally added here
09-29 09:54:57.793: E/WindowManager(279): android.view.WindowLeaked: Activity com.example.evoting.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f00418 that was originally added here
09-29 09:54:57.793: E/WindowManager(279): at android.view.ViewRoot.<init>(ViewRoot.java:247)
09-29 09:54:57.793: E/WindowManager(279): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
09-29 09:54:57.793: E/WindowManager(279): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-29 09:54:57.793: E/WindowManager(279): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-29 09:54:57.793: E/WindowManager(279): at android.app.Dialog.show(Dialog.java:241)
09-29 09:54:57.793: E/WindowManager(279): at com.example.evoting.HomeActivity$CheckVoter.onPreExecute(HomeActivity.java:134)
09-29 09:54:57.793: E/WindowManager(279): at android.os.AsyncTask.execute(AsyncTask.java:391)
09-29 09:54:57.793: E/WindowManager(279): at com.example.evoting.HomeActivity$2.onClick(HomeActivity.java:74)
09-29 09:54:57.793: E/WindowManager(279): at android.view.View.performClick(View.java:2408)
09-29 09:54:57.793: E/WindowManager(279): at android.view.View$PerformClick.run(View.java:8816)
09-29 09:54:57.793: E/WindowManager(279): at android.os.Handler.handleCallback(Handler.java:587)
09-29 09:54:57.793: E/WindowManager(279): at android.os.Handler.dispatchMessage(Handler.java:92)
09-29 09:54:57.793: E/WindowManager(279): at android.os.Looper.loop(Looper.java:123)
09-29 09:54:57.793: E/WindowManager(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-29 09:54:57.793: E/WindowManager(279): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 09:54:57.793: E/WindowManager(279): at java.lang.reflect.Method.invoke(Method.java:521)
09-29 09:54:57.793: E/WindowManager(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-29 09:54:57.793: E/WindowManager(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-29 09:54:57.793: E/WindowManager(279): at dalvik.system.NativeStart.main(Native Method)
09-29 09:54:59.273: I/Process(279): Sending signal. PID: 279 SIG: 9
09-29 09:56:18.005: D/Create Response(313): {"message":"Product successfully created.","success":0}
09-29 09:56:18.013: W/dalvikvm(313): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
09-29 09:56:18.153: D/dalvikvm(313): GC_FOR_MALLOC freed 2867 objects / 171224 bytes in 123ms
09-29 09:56:18.153: E/AndroidRuntime(313): FATAL EXCEPTION: AsyncTask #1
09-29 09:56:18.153: E/AndroidRuntime(313): java.lang.RuntimeException: An error occured while executing doInBackground()
09-29 09:56:18.153: E/AndroidRuntime(313): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-29 09:56:18.153: E/AndroidRuntime(313): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-29 09:56:18.153: E/AndroidRuntime(313): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-29 09:56:18.153: E/AndroidRuntime(313): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-29 09:56:18.153: E/AndroidRuntime(313): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-29 09:56:18.153: E/AndroidRuntime(313): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-29 09:56:18.153: E/AndroidRuntime(313): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-29 09:56:18.153: E/AndroidRuntime(313): at java.lang.Thread.run(Thread.java:1096)
09-29 09:56:18.153: E/AndroidRuntime(313): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-29 09:56:18.153: E/AndroidRuntime(313): at android.os.Handler.<init>(Handler.java:121)
09-29 09:56:18.153: E/AndroidRuntime(313): at android.app.Dialog.<init>(Dialog.java:101)
09-29 09:56:18.153: E/AndroidRuntime(313): at android.app.AlertDialog.<init>(AlertDialog.java:63)
09-29 09:56:18.153: E/AndroidRuntime(313): at android.app.AlertDialog.<init>(AlertDialog.java:59)
09-29 09:56:18.153: E/AndroidRuntime(313): at android.app.AlertDialog$Builder.create(AlertDialog.java:786)
09-29 09:56:18.153: E/AndroidRuntime(313): at com.example.evoting.HomeActivity$CheckVoter.doInBackground(HomeActivity.java:220)
09-29 09:56:18.153: E/AndroidRuntime(313): at com.example.evoting.HomeActivity$CheckVoter.doInBackground(HomeActivity.java:1)
09-29 09:56:18.153: E/AndroidRuntime(313): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-29 09:56:18.153: E/AndroidRuntime(313): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-29 09:56:18.153: E/AndroidRuntime(313): ... 4 more
09-29 09:56:18.994: E/WindowManager(313): Activity com.example.evoting.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f00510 that was originally added here
09-29 09:56:18.994: E/WindowManager(313): android.view.WindowLeaked: Activity com.example.evoting.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f00510 that was originally added here
09-29 09:56:18.994: E/WindowManager(313): at android.view.ViewRoot.<init>(ViewRoot.java:247)
09-29 09:56:18.994: E/WindowManager(313): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
09-29 09:56:18.994: E/WindowManager(313): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-29 09:56:18.994: E/WindowManager(313): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-29 09:56:18.994: E/WindowManager(313): at android.app.Dialog.show(Dialog.java:241)
09-29 09:56:18.994: E/WindowManager(313): at com.example.evoting.HomeActivity$CheckVoter.onPreExecute(HomeActivity.java:134)
09-29 09:56:18.994: E/WindowManager(313): at android.os.AsyncTask.execute(AsyncTask.java:391)
09-29 09:56:18.994: E/WindowManager(313): at com.example.evoting.HomeActivity$2.onClick(HomeActivity.java:74)
09-29 09:56:18.994: E/WindowManager(313): at android.view.View.performClick(View.java:2408)
09-29 09:56:18.994: E/WindowManager(313): at android.view.View$PerformClick.run(View.java:8816)
09-29 09:56:18.994: E/WindowManager(313): at android.os.Handler.handleCallback(Handler.java:587)
09-29 09:56:18.994: E/WindowManager(313): at android.os.Handler.dispatchMessage(Handler.java:92)
09-29 09:56:18.994: E/WindowManager(313): at android.os.Looper.loop(Looper.java:123)
09-29 09:56:18.994: E/WindowManager(313): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-29 09:56:18.994: E/WindowManager(313): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 09:56:18.994: E/WindowManager(313): at java.lang.reflect.Method.invoke(Method.java:521)
09-29 09:56:18.994: E/WindowManager(313): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-29 09:56:18.994: E/WindowManager(313): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-29 09:56:18.994: E/WindowManager(313): at dalvik.system.NativeStart.main(Native Method)
09-29 09:56:48.843: I/Process(313): Sending signal. PID: 313 SIG: 9
09-29 10:09:07.483: D/Create Response(338): {"message":"Product successfully created.","success":0}
09-29 10:09:07.483: W/dalvikvm(338): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
09-29 10:09:07.523: D/dalvikvm(338): GC_FOR_MALLOC freed 2883 objects / 171944 bytes in 41ms
09-29 10:09:07.533: E/AndroidRuntime(338): FATAL EXCEPTION: AsyncTask #1
09-29 10:09:07.533: E/AndroidRuntime(338): java.lang.RuntimeException: An error occured while executing doInBackground()
09-29 10:09:07.533: E/AndroidRuntime(338): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-29 10:09:07.533: E/AndroidRuntime(338): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-29 10:09:07.533: E/AndroidRuntime(338): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-29 10:09:07.533: E/AndroidRuntime(338): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-29 10:09:07.533: E/AndroidRuntime(338): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-29 10:09:07.533: E/AndroidRuntime(338): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-29 10:09:07.533: E/AndroidRuntime(338): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-29 10:09:07.533: E/AndroidRuntime(338): at java.lang.Thread.run(Thread.java:1096)
09-29 10:09:07.533: E/AndroidRuntime(338): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-29 10:09:07.533: E/AndroidRuntime(338): at android.os.Handler.<init>(Handler.java:121)
09-29 10:09:07.533: E/AndroidRuntime(338): at android.app.Dialog.<init>(Dialog.java:101)
09-29 10:09:07.533: E/AndroidRuntime(338): at android.app.AlertDialog.<init>(AlertDialog.java:63)
09-29 10:09:07.533: E/AndroidRuntime(338): at android.app.AlertDialog.<init>(AlertDialog.java:59)
09-29 10:09:07.533: E/AndroidRuntime(338): at android.app.AlertDialog$Builder.create(AlertDialog.java:786)
09-29 10:09:07.533: E/AndroidRuntime(338): at com.example.evoting.HomeActivity$CheckVoter.doInBackground(HomeActivity.java:220)
09-29 10:09:07.533: E/AndroidRuntime(338): at com.example.evoting.HomeActivity$CheckVoter.doInBackground(HomeActivity.java:1)
09-29 10:09:07.533: E/AndroidRuntime(338): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-29 10:09:07.533: E/AndroidRuntime(338): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-29 10:09:07.533: E/AndroidRuntime(338): ... 4 more
09-29 10:09:08.153: E/WindowManager(338): Activity com.example.evoting.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f003e0 that was originally added here
09-29 10:09:08.153: E/WindowManager(338): android.view.WindowLeaked: Activity com.example.evoting.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f003e0 that was originally added here
09-29 10:09:08.153: E/WindowManager(338): at android.view.ViewRoot.<init>(ViewRoot.java:247)
09-29 10:09:08.153: E/WindowManager(338): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
09-29 10:09:08.153: E/WindowManager(338): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-29 10:09:08.153: E/WindowManager(338): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-29 10:09:08.153: E/WindowManager(338): at android.app.Dialog.show(Dialog.java:241)
09-29 10:09:08.153: E/WindowManager(338): at com.example.evoting.HomeActivity$CheckVoter.onPreExecute(HomeActivity.java:134)
09-29 10:09:08.153: E/WindowManager(338): at android.os.AsyncTask.execute(AsyncTask.java:391)
09-29 10:09:08.153: E/WindowManager(338): at com.example.evoting.HomeActivity$2.onClick(HomeActivity.java:74)
09-29 10:09:08.153: E/WindowManager(338): at android.view.View.performClick(View.java:2408)
09-29 10:09:08.153: E/WindowManager(338): at android.view.View$PerformClick.run(View.java:8816)
09-29 10:09:08.153: E/WindowManager(338): at android.os.Handler.handleCallback(Handler.java:587)
09-29 10:09:08.153: E/WindowManager(338): at android.os.Handler.dispatchMessage(Handler.java:92)
09-29 10:09:08.153: E/WindowManager(338): at android.os.Looper.loop(Looper.java:123)
09-29 10:09:08.153: E/WindowManager(338): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-29 10:09:08.153: E/WindowManager(338): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 10:09:08.153: E/WindowManager(338): at java.lang.reflect.Method.invoke(Method.java:521)
09-29 10:09:08.153: E/WindowManager(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-29 10:09:08.153: E/WindowManager(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-29 10:09:08.153: E/WindowManager(338): at dalvik.system.NativeStart.main(Native Method)