无法从json全局声明字符串

问题描述:

我想从远程json获取一个值并将其转换为全局定义的字符串,以便我可以将其用于共享目的。我设法使用该字符串来设置textview,但是当我进一步尝试在onCreateOptionsMenu中使用相同的字符串时,它给了我空值。即使我不能从LoadService中声明的textview中获取字符串以在OncreateOption中使用它。无法从json全局声明字符串

share_string是全局定义的字符串,其值在LoadService中声明,我想在OnCreateOptionMnu中使用它。任何帮助将是最受欢迎的。

private class LoadService extends AsyncTask<String, Void, Void> { 

    private final HttpClient Client = new DefaultHttpClient(); 
    private String Content; 
    private String Error = null; 
    private final String TAG = null; 
    private ProgressDialog Dialog = new ProgressDialog(Advertisment_Page.this); 


    protected void onPreExecute() { 
     // NOTE: You can call UI Element here. 

     // UI Element 
     Dialog.setMessage("Loading service.."); 
     Dialog.show(); 
    } 

    // Call after onPreExecute method 
    public Void doInBackground(String... urls) { 
     try { 

      // NOTE: Don't call UI Element here. 

      HttpGet httpget = new HttpGet(urls[0]); 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      Content = Client.execute(httpget, responseHandler); 

     } catch (ClientProtocolException e) { 
      Error = e.getMessage(); 
      cancel(true); 
     } catch (IOException e) { 
      Error = e.getMessage(); 
      cancel(true); 
     } 

     return null; 
    } 

    public void onPostExecute(Void unused) { 
     // NOTE: You can call UI Element here. 

     // Close progress dialog 
     Dialog.dismiss(); 
     Log.e(TAG, "------------------------------------- Output: " 
       + Content); 
     try { 

      // Load json data and display 
      JSONObject json = new JSONObject(Content); 

      title_tv.setText(json.getString("title")); 

      share_string=(json.getString("rupees")); 
      rupees_tv.setText(share_string); 

      relativeLayout.setVisibility(View.VISIBLE); 




     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_share, menu); 
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider(); 

    /** Setting a share intent */ 
    mShareActionProvider.setShareIntent(getDefaultShareIntent()); 

    return true; 
} 

private Intent getDefaultShareIntent(){ 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("text/plain"); 
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT,share_string); 
    intent.putExtra(android.content.Intent.EXTRA_TEXT, share_string); 
    return intent; 
} 


} 
+0

如果你打电话给你的asynctask,它可能会发生,因为oncreateoptionmenu在asynctask完成之前调用,因此你的共享字符串包含空值 – USKMobility

的AsyncTask是异步调用,这样onCreateOptionsMenu(Menu menu)onPostExecute(Void unused)电话叫。

因此,您需要通过在onPostExecute(Void unused)方法的末尾调用 invalidateOptionsMenu()来手动调用onCreateOptionsMenu(菜单菜单)。

+0

你是摇滚人。非常感谢。 –

+0

欢迎Pratik Mhatre。请注意它 –