2013-07-15 67 views
0

我寫這封信,我想複選框的寫狀態的應用程序,但每當我在警告對話框按鈕得到空指針異常點擊,請參閱下面的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) 
+1

這是行數303在UploadActivity.java文件 –

+0

這裏是行號。 303 - params.add(new BasicNameValuePair(「sOption1」,tv1.getText()。toString())); – Sun

+0

什麼是'alertDialog',你使用自定義對話框? –

回答

2

我覺得你是不是在TextView中得到CheckBox的狀態,請使用以下代碼獲取檢查狀態,我已經寫代碼..

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"); 
      } 
     }); 
    } 
} 
2

刪除以下行。

tv1 = (TextView) alertDialog.findViewById(R.id.textView1); 
CheckBox chkIos = (CheckBox) alertDialog.findViewById(R.id.chkOption1); 

如果您想在該方法之外使用它,則不應該在方法中引用任何視圖。所以在onCreat方法中寫上面兩行。簡單的活動改變它自己的方式在AlertDialog盒使用它

//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. 
    }