我正在编写一个应用程序,我想写入Checkbox的状态,但每当我点击Alert Dialog按钮获取空指针异常,请参阅下面的 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)
答案 0 :(得分:2)
删除以下行。
tv1 = (TextView) alertDialog.findViewById(R.id.textView1);
CheckBox chkIos = (CheckBox) alertDialog.findViewById(R.id.chkOption1);
如果要在该方法之外使用它,则不应引用方法中的任何视图。所以在onCreat
方法中写上两行。
//declaration
CheckBox chkIos;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
tv1 = (TextView) findViewById(R.id.textView1);
chkIos = (CheckBox) findViewById(R.id.chkOption1);
//rest of code.
}
答案 1 :(得分:2)
我认为你没有在TextView中获得CheckBox的状态,请使用下面的代码来获取检查状态,我已经编写了简单的Activity代码,以便在AlertDialog Box中使用它。
public class MyAndroidAppActivity extends Activity {
private CheckBox chkIos ;
TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
tv1 = (TextView) findViewById(R.id.textView1);
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkIos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked())
tv1.setText("True");
else
tv1.setText("False");
}
});
}
}