我是 android 的新手,我正在开发一个通过 PHP 将数据从 android 传递到 MySQL 的应用程序。我现在正在注册用户部分。我从搜索中获得了一些代码,但是在实现它时,我遇到了运行时错误。这是我的代码。logcat
也是在底部。
public class RegisterActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText registerName;
EditText registerRollno;
EditText inputDesc;
// url to create new product
private static String url_create_voter = "http://10.0.2.2/evoting/create_voter.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register1);
// Edit Text
EditText registerName = (EditText) findViewById(R.id.registerName);
EditText registerRollno = (EditText) findViewById(R.id.registerRollno);
// inputDesc = (EditText) findViewById(R.id.inputDesc);
// Create button
Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
// button click event
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating new product in background thread
new CreateNewVoter().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewVoter extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegisterActivity.this);
pDialog.setMessage("Creating Voter..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = registerName.getText().toString();
String rollno = registerRollno.getText().toString();
//String description = inputDesc.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("rollno", rollno));
// params.add(new BasicNameValuePair("description", description));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_voter,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), CreateVoterSuccess.class);
startActivity(i);
// closing this screen
finish();
} else {
Intent i = new Intent(getApplicationContext(), CreateVoterFail.class);
startActivity(i);
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
这是logcat
:
09-22 05:47:52.343: W/KeyCharacterMap(279): No keyboard for id 0
09-22 05:47:52.343: W/KeyCharacterMap(279): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-22 05:47:54.403: W/dalvikvm(279): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
09-22 05:47:54.414: E/AndroidRuntime(279): FATAL EXCEPTION: AsyncTask #1
09-22 05:47:54.414: E/AndroidRuntime(279): java.lang.RuntimeException: An error occured while executing doInBackground()
09-22 05:47:54.414: E/AndroidRuntime(279): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-22 05:47:54.414: E/AndroidRuntime(279): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-22 05:47:54.414: E/AndroidRuntime(279): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-22 05:47:54.414: E/AndroidRuntime(279): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-22 05:47:54.414: E/AndroidRuntime(279): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-22 05:47:54.414: E/AndroidRuntime(279): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-22 05:47:54.414: E/AndroidRuntime(279): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-22 05:47:54.414: E/AndroidRuntime(279): at java.lang.Thread.run(Thread.java:1096)
09-22 05:47:54.414: E/AndroidRuntime(279): Caused by: java.lang.NullPointerException
09-22 05:47:54.414: E/AndroidRuntime(279): at com.example.evoting.RegisterActivity$CreateNewVoter.doInBackground(RegisterActivity.java:83)
09-22 05:47:54.414: E/AndroidRuntime(279): at com.example.evoting.RegisterActivity$CreateNewVoter.doInBackground(RegisterActivity.java:1)
09-22 05:47:54.414: E/AndroidRuntime(279): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-22 05:47:54.414: E/AndroidRuntime(279): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-22 05:47:54.414: E/AndroidRuntime(279): ... 4 more
09-22 05:47:55.313: E/WindowManager(279): Activity com.example.evoting.RegisterActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f28db0 that was originally added here
09-22 05:47:55.313: E/WindowManager(279): android.view.WindowLeaked: Activity com.example.evoting.RegisterActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f28db0 that was originally added here
09-22 05:47:55.313: E/WindowManager(279): at android.view.ViewRoot.<init>(ViewRoot.java:247)
09-22 05:47:55.313: E/WindowManager(279): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
09-22 05:47:55.313: E/WindowManager(279): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-22 05:47:55.313: E/WindowManager(279): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-22 05:47:55.313: E/WindowManager(279): at android.app.Dialog.show(Dialog.java:241)
09-22 05:47:55.313: E/WindowManager(279): at com.example.evoting.RegisterActivity$CreateNewVoter.onPreExecute(RegisterActivity.java:76)
09-22 05:47:55.313: E/WindowManager(279): at android.os.AsyncTask.execute(AsyncTask.java:391)
09-22 05:47:55.313: E/WindowManager(279): at com.example.evoting.RegisterActivity$1.onClick(RegisterActivity.java:56)
09-22 05:47:55.313: E/WindowManager(279): at android.view.View.performClick(View.java:2408)
09-22 05:47:55.313: E/WindowManager(279): at android.view.View$PerformClick.run(View.java:8816)
09-22 05:47:55.313: E/WindowManager(279): at android.os.Handler.handleCallback(Handler.java:587)
09-22 05:47:55.313: E/WindowManager(279): at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 05:47:55.313: E/WindowManager(279): at android.os.Looper.loop(Looper.java:123)
09-22 05:47:55.313: E/WindowManager(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-22 05:47:55.313: E/WindowManager(279): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 05:47:55.313: E/WindowManager(279): at java.lang.reflect.Method.invoke(Method.java:521)
09-22 05:47:55.313: E/WindowManager(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-22 05:47:55.313: E/WindowManager(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-22 05:47:55.313: E/WindowManager(279): at dalvik.system.NativeStart.main(Native Method)
09-22 05:47:57.133: I/Process(279): Sending signal. PID: 279 SIG: 9
09-22 05:56:34.157: W/KeyCharacterMap(291): No keyboard for id 0
09-22 05:56:34.157: W/KeyCharacterMap(291): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-22 05:56:36.553: W/dalvikvm(291): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
09-22 05:56:36.563: E/AndroidRuntime(291): FATAL EXCEPTION: AsyncTask #1
09-22 05:56:36.563: E/AndroidRuntime(291): java.lang.RuntimeException: An error occured while executing doInBackground()
09-22 05:56:36.563: E/AndroidRuntime(291): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-22 05:56:36.563: E/AndroidRuntime(291): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-22 05:56:36.563: E/AndroidRuntime(291): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-22 05:56:36.563: E/AndroidRuntime(291): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-22 05:56:36.563: E/AndroidRuntime(291): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-22 05:56:36.563: E/AndroidRuntime(291): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-22 05:56:36.563: E/AndroidRuntime(291): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-22 05:56:36.563: E/AndroidRuntime(291): at java.lang.Thread.run(Thread.java:1096)
09-22 05:56:36.563: E/AndroidRuntime(291): Caused by: java.lang.NullPointerException
09-22 05:56:36.563: E/AndroidRuntime(291): at com.example.evoting.RegisterActivity$CreateNewVoter.doInBackground(RegisterActivity.java:83)
09-22 05:56:36.563: E/AndroidRuntime(291): at com.example.evoting.RegisterActivity$CreateNewVoter.doInBackground(RegisterActivity.java:1)
09-22 05:56:36.563: E/AndroidRuntime(291): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-22 05:56:36.563: E/AndroidRuntime(291): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-22 05:56:36.563: E/AndroidRuntime(291): ... 4 more
09-22 05:56:37.203: E/WindowManager(291): Activity com.example.evoting.RegisterActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f21560 that was originally added here
09-22 05:56:37.203: E/WindowManager(291): android.view.WindowLeaked: Activity com.example.evoting.RegisterActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f21560 that was originally added here
09-22 05:56:37.203: E/WindowManager(291): at android.view.ViewRoot.<init>(ViewRoot.java:247)
09-22 05:56:37.203: E/WindowManager(291): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
09-22 05:56:37.203: E/WindowManager(291): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-22 05:56:37.203: E/WindowManager(291): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-22 05:56:37.203: E/WindowManager(291): at android.app.Dialog.show(Dialog.java:241)
09-22 05:56:37.203: E/WindowManager(291): at com.example.evoting.RegisterActivity$CreateNewVoter.onPreExecute(RegisterActivity.java:76)
09-22 05:56:37.203: E/WindowManager(291): at android.os.AsyncTask.execute(AsyncTask.java:391)
09-22 05:56:37.203: E/WindowManager(291): at com.example.evoting.RegisterActivity$1.onClick(RegisterActivity.java:56)
09-22 05:56:37.203: E/WindowManager(291): at android.view.View.performClick(View.java:2408)
09-22 05:56:37.203: E/WindowManager(291): at android.view.View$PerformClick.run(View.java:8816)
09-22 05:56:37.203: E/WindowManager(291): at android.os.Handler.handleCallback(Handler.java:587)
09-22 05:56:37.203: E/WindowManager(291): at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 05:56:37.203: E/WindowManager(291): at android.os.Looper.loop(Looper.java:123)
09-22 05:56:37.203: E/WindowManager(291): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-22 05:56:37.203: E/WindowManager(291): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 05:56:37.203: E/WindowManager(291): at java.lang.reflect.Method.invoke(Method.java:521)
09-22 05:56:37.203: E/WindowManager(291): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-22 05:56:37.203: E/WindowManager(291): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-22 05:56:37.203: E/WindowManager(291): at dalvik.system.NativeStart.main(Native Method)
09-22 05:56:41.412: I/Process(291): Sending signal. PID: 291 SIG: 9