我正在编写一个应用程序,我想在其中写入 Checkbox 的状态,但是每当我单击“警报对话框”按钮获取Null Pointer Exception时,请参阅下面的Logcat报告和代码:
空指针异常:
addListenerOnButton();
params.add(new BasicNameValuePair("sOption1", tv1.getText().toString()));
上传活动.java:
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SaveData();
addListenerOnButton();
}
public void addListenerOnButton() {
tv1 = (TextView) alertDialog.findViewById(R.id.textView1);
CheckBox chkIos = (CheckBox) alertDialog.findViewById(R.id.chkOption1);
chkIos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked())
tv1.setText("True");
else
tv1.setText("False");
}
});
}
private boolean SaveData() {
final AlertDialog.Builder ad = new AlertDialog.Builder(UploadActivity.this);
ad.setTitle("Error!");
ad.setIcon(android.R.drawable.btn_star_big_on);
ad.setPositiveButton("Close", null);
String url = "http://10.0.2.2/res/uploadImage.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sFilesName", file_name.getText().toString()));
params.add(new BasicNameValuePair("sImageName", image_name.getText().toString()));
params.add(new BasicNameValuePair("sPersonName", person_name.getText().toString()));
params.add(new BasicNameValuePair("sPersonEmail", person_email.getText().toString()));
params.add(new BasicNameValuePair("sOption1", tv1.getText().toString()));
String resultServer = getHttpPost(url,params);
Log.d("Entire string::", " " + resultServer);
/*** Default Value ***/
String strStatusID = "0";
String strError = "Cannot upload Image";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strError = c.getString("Message");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
日志猫:
07-15 06:45:01.094: W/dalvikvm(789): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-15 06:45:01.115: E/AndroidRuntime(789): FATAL EXCEPTION: main
07-15 06:45:01.115: E/AndroidRuntime(789): java.lang.NullPointerException
07-15 06:45:01.115: E/AndroidRuntime(789): at com.example.UploadActivity$2.SaveData(UploadActivity.java:303)
07-15 06:45:01.115: E/AndroidRuntime(789): at com.example.UploadActivity$2.onClick(UploadActivity.java:267)
07-15 06:45:01.115: E/AndroidRuntime(789): at android.view.View.performClick(View.java:4204)
07-15 06:45:01.115: E/AndroidRuntime(789): at android.view.View$PerformClick.run(View.java:17355)
07-15 06:45:01.115: E/AndroidRuntime(789): at android.os.Handler.handleCallback(Handler.java:725)
07-15 06:45:01.115: E/AndroidRuntime(789): at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 06:45:01.115: E/AndroidRuntime(789): at android.os.Looper.loop(Looper.java:137)
07-15 06:45:01.115: E/AndroidRuntime(789): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 06:45:01.115: E/AndroidRuntime(789): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 06:45:01.115: E/AndroidRuntime(789): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 06:45:01.115: E/AndroidRuntime(789): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 06:45:01.115: E/AndroidRuntime(789): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 06:45:01.115: E/AndroidRuntime(789): at dalvik.system.NativeStart.main(Native Method)