2014-09-06 74 views
0

我從我想通過如姓名,地址我在OnpostExecute了另一個電子郵件活動字符串數據singlePlaceActivity類。我的singlePlace活動成功地啓動了電子郵件活動,但我沒有在使用getextra方法的電子郵件活動中收到putExtra string data,並且我通過logcat驗證了我的字符串數據名稱,地址和電話沒有空值。使用意向接收使用putExtra和getExtra方法次活動空數據

SinglePlaceActivity:

package info.androidhive.slidingmenu; 

    import android.app.Activity;  
    import android.app.ProgressDialog; 
    import android.content.Intent; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.text.Html; 
    import android.util.Log; 
    import android.widget.TextView; 

    public class SinglePlaceActivity extends Activity { 
    // flag for Internet connection status 
    Boolean isInternetPresent = false; 

    // Connection detector class 
    ConnectionDetector cd; 

    // Alert Dialog Manager 
    AlertDialogManager alert = new AlertDialogManager(); 

    // Google Places 
    GooglePlaces googlePlaces; 

    // Place Details 
    PlaceDetails placeDetails; 

    // Progress dialog 
    ProgressDialog pDialog; 

    // KEY Strings 
      // public static String KEY_REFERENCE = "reference"; // id of the place 
      public static String reference_value = "reference"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.single_place); 

     Intent i = getIntent(); 

     // Place referece id 
     String reference = i.getStringExtra(reference_value); 

     // Calling a Async Background thread 
     new LoadSinglePlaceDetails().execute(reference); 
    } 


    /** 
    * Background Async Task to Load Google places 
    * */ 
    class LoadSinglePlaceDetails extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(SinglePlaceActivity.this); 
      pDialog.setMessage("Passing Restaurent details to Email ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * getting Profile JSON 
     * */ 
     protected String doInBackground(String... args) { 
      String reference = args[0]; 

      // creating Places class object 
      googlePlaces = new GooglePlaces(); 

      // Check if used is connected to Internet 
      try { 
       placeDetails = googlePlaces.getPlaceDetails(reference); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting all products 
      pDialog.dismiss(); 
      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        /** 
        * Updating parsed Places into LISTVIEW 
        * */ 
        if(placeDetails != null){ 
         String status = placeDetails.status; 

         // check place deatils status 
         // Check for all possible status 
         if(status.equals("OK")){ 
          if (placeDetails.result != null) { 
           String name = placeDetails.result.name; 
           String address = placeDetails.result.formatted_address; 
           String phone = placeDetails.result.formatted_phone_number; 
           String latitude = Double.toString(placeDetails.result.geometry.location.lat); 
           String longitude = Double.toString(placeDetails.result.geometry.location.lng); 

           Intent in = new Intent(getApplicationContext(),Email.class); 
           in.putExtra("nameValue",name); 
           in.putExtra("addValue",address); 
           in.putExtra("urlValue",phone); 

           startActivity(in); 


           Log.d("Place ", name + address + phone + latitude + longitude); 


          } 
         } 
         else if(status.equals("ZERO_RESULTS")){ 
          alert.showAlertDialog(SinglePlaceActivity.this, "Near Places", 
            "Sorry no place found.", 
            false); 
         } 
         else if(status.equals("UNKNOWN_ERROR")) 
         { 
          alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
            "Sorry unknown error occured.", 
            false); 
         } 
         else if(status.equals("OVER_QUERY_LIMIT")) 
         { 
          alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
            "Sorry query limit to google places is reached", 
            false); 
         } 
         else if(status.equals("REQUEST_DENIED")) 
         { 
          alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
            "Sorry error occured. Request is denied", 
            false); 
         } 
         else if(status.equals("INVALID_REQUEST")) 
         { 
          alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
            "Sorry error occured. Invalid Request", 
            false); 
         } 
         else 
         { 
          alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
            "Sorry error occured.", 
            false); 
         } 
        }else{ 
         alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
           "Sorry error occured.", 
           false); 
        } 


       } 
      }); 

     } 

    } 

} 

這是我第二次接收活動是電子郵件活動 Email.java

package info.androidhive.slidingmenu; 
    import org.json.JSONObject; 
    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 

    public class Email extends Activity implements View.OnClickListener { 

    EditText personsEmail, rname, radd, rurl, yourmsg; 

    String emailAdd, beginning, stupidAction, hatefulAct, address; 
    Button sendEmail; 






    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.email); 

     //String beginning = getIntent().getStringExtra("extraname") ;//returns null if nothing 
     // String address = getIntent().getStringExtra("extraname2"); 
     //String stupidAction = getIntent().getStringExtra("extraname2"); 

     beginning = getIntent().getStringExtra("nameValue"); 
     address = getIntent().getStringExtra("addValue"); 
     stupidAction= getIntent().getStringExtra("urlValue"); 
     // beginning = intent.getStringExtra("nameValue"); 
     //address = intent.getStringExtra("addValue"); 
     // stupidAction = intent.getStringExtra("urlValue"); 



     initializeVars(); 

     sendEmail.setOnClickListener(this); 
    } 

    private void initializeVars() { 
     // TODO Auto-generated method stub 
     personsEmail = (EditText) findViewById(R.id.etEmails); 
     rname = (EditText) findViewById(R.id.etIntro); 
     radd = (EditText) findViewById(R.id.etName); 
     rurl = (EditText) findViewById(R.id.etThings); 
     yourmsg = (EditText) findViewById(R.id.etAction); 

     sendEmail = (Button) findViewById(R.id.bSentEmail); 
    } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 



     convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated(); 
     String emailaddress[] = { emailAdd }; 
     String message = "Restaurent Name:"+ beginning 

       + " Restaurent Address:"+ address 

       + "Click for more details:" + stupidAction 

       + "Message:" + hatefulAct; 

       Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
       emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress); 
       emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT," Restaurent Recommendation"); 
       emailIntent.setType("plain/text"); 
       emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); 
       startActivity(emailIntent); 


    } 

    private void convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated() { 
     // TODO Auto-generated method stub 
     emailAdd = personsEmail.getText().toString(); 
     beginning = rname.getText().toString(); 
     address = radd.getText().toString(); 
     stupidAction = rurl.getText().toString(); 
     hatefulAct = yourmsg.getText().toString(); 

    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     finish(); 
    } 

} 

回答

0

如果使用AsyncTask沒有必要任何background thread內onPostExecute(...)的。因此,請刪除onPostExecute(...)中的線程,如果您在onPostExecute(..)中發送的值不爲空,那麼您的問題將得到解決

AsyncTask支持正確和方便地使用UI線程。該類 允許執行後臺操作並在UI線程上發佈結果,而無需操縱線程和/或處理程序。

試試這個代碼爲onPostExecute(String response),而不是您

protected void onPostExecute(String response) { 
     // dismiss the dialog after getting all products 
     pDialog.dismiss(); 
     /** 
     * Updating parsed Places into LISTVIEW 
     * */ 
     if (placeDetails != null) { 
      String status = placeDetails.status; 

      // check place deatils status 
      // Check for all possible status 
      if (status.equals("OK")) { 
       if (placeDetails.result != null) { 
        String name = placeDetails.result.name; 
        String address = placeDetails.result.formatted_address; 
        String phone = placeDetails.result.formatted_phone_number; 
        String latitude = Double 
          .toString(placeDetails.result.geometry.location.lat); 
        String longitude = Double 
          .toString(placeDetails.result.geometry.location.lng); 

        Intent in = new Intent(getApplicationContext(), Email.class); 
        in.putExtra("nameValue", name); 
        in.putExtra("addValue", address); 
        in.putExtra("urlValue", phone); 

        startActivity(in); 

        Log.d("Place ", name + address + phone + latitude 
          + longitude); 

       } 
      } else if (status.equals("ZERO_RESULTS")) { 
       alert.showAlertDialog(SinglePlaceActivity.this, "Near Places", 
         "Sorry no place found.", false); 
      } else if (status.equals("UNKNOWN_ERROR")) { 
       alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
         "Sorry unknown error occured.", false); 
      } else if (status.equals("OVER_QUERY_LIMIT")) { 
       alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
         "Sorry query limit to google places is reached", false); 
      } else if (status.equals("REQUEST_DENIED")) { 
       alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
         "Sorry error occured. Request is denied", false); 
      } else if (status.equals("INVALID_REQUEST")) { 
       alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
         "Sorry error occured. Invalid Request", false); 
      } else { 
       alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
         "Sorry error occured.", false); 
      } 
     } else { 
      alert.showAlertDialog(SinglePlaceActivity.this, "Places Error", 
        "Sorry error occured.", false); 
     } 

    } 
+0

Heyy kaushik它不工作,我的字符串值的名稱,地址,電話不爲空我從logcat驗證。即使我沒有在第二個活動,即電子郵件活動geting沒什麼。 – user3930098 2014-09-06 06:27:49

+0

「緯度」和「經度」如何? – kId 2014-09-06 06:49:17

+0

截至目前,我不需要經緯度。 – user3930098 2014-09-06 09:08:18

1

嘗試包傳遞和接收數據,

Intent intent = new Intent(first.this, second.class); 

Bundle bundle = new Bundle(); 
bundle.putInt("index", index);  
intent.putExtras(bundle); 
startActivity(intent); 

接收數據:

Bundle b = getIntent().getExtras(); 
int index = b.getInt("index"); 
+0

heyy我想剛纔,它不工作。 – user3930098 2014-09-06 06:04:11

+0

當您從您的活動發送數據時,什麼是日誌輸出?如果你發送的數據是空的,你也會得到空值。 – Shvet 2014-09-06 06:12:53

+0

heyy dhaval這些值不爲null。我從logcat驗證。 – user3930098 2014-09-06 06:28:55

0

我花了幾個星期在這個問題上。看起來您應該注意「發件人」活動中存儲的值的數據類型。由於我有不同的活動開始我的「接收器」活動,我添加了這個if語句來處理兩種數據類型(考慮someId是一個長數據類型)。

someId = getIntent().getLongExtra(EXTRA_ID,0); 
    if (someId < 1){ 
     someId = Long.parseLong(getIntent().getStringExtra(EXTRA_ID)); 
    } 

不幸的是,我無法解釋爲什麼,也許有些大師可以啓發我們普通老百姓......