0

我正在编写一个应用程序,我想在其中写入 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)
4

2 回答 2

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.
   }
于 2013-07-15T06:59:41.883 回答
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");
            }
        });
    }
 }
于 2013-07-15T07:08:52.333 回答