无法从json全局声明字符串

时间:2015-07-28 09:35:33

标签: android json string

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

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;
}


}

1 个答案:

答案 0 :(得分:0)

AsyncTask是异步调用,因此在onCreateOptionsMenu(Menu menu)调用之前调用onPostExecute(Void unused)

因此您需要通过调用手动调用onCreateOptionsMenu(菜单菜单) invalidateOptionsMenu()方法结束时的onPostExecute(Void unused)