我寫這封信,我想複選框的寫狀態的應用程序,但每當我在警告對話框按鈕得到空指針異常做點擊,請參閱下面的logcat的報告代碼:空指針異常點擊按鈕
空指針異常:
addListenerOnButton();
params.add(new BasicNameValuePair("sOption1", tv1.getText().toString()));
UploadActivity.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();
}
logcat的:
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)
這是行數303在UploadActivity.java文件 –
這裏是行號。 303 - params.add(new BasicNameValuePair(「sOption1」,tv1.getText()。toString())); – Sun
什麼是'alertDialog',你使用自定義對話框? –