如何在onCreate方法之前获得getStringExtra消息?

时间:2013-11-26 02:05:01

标签: java android json methods oncreate

我有两个活动,在第一个我发消息,putExtra我将它移到下一个活动。但是,如您所知要在secind活动中收到消息,我需要在getStringExtra方法中使用onCreate。另一方面,我真的需要在onCreate开始之前获得该消息。那我该怎么办呢。

public class Result extends Activity {
String url; // <<< I need the message to put it here

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_result);

    Intent intent = getIntent();
    String mainURL = intent.getStringExtra(SingleSearchPage.EXTRA_MESSAGE); // Here is the message

    url = mainURL;  //Tried to change the value of URL but did not work

    new GetJSONTask().execute(url);

}
class GetJSONTask extends AsyncTask<String, Void, JSONObject> {


        protected JSONObject doInBackground(String... urls) {
            // Creating new JSON Parser
            JSONParser jParser = new JSONParser();

            // Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);

            return json;
        }

LOG:

    11-26 03:56:00.666: D/URL(777): url =http://api.worldbank.org/countries/CA/indicators/SP.POP.TOTL?date=1980:2013&format=json
11-26 03:56:00.796: I/Choreographer(777): Skipped 62 frames!  The application may be doing too much work on its main thread.
11-26 03:56:01.045: I/Choreographer(777): Skipped 57 frames!  The application may be doing too much work on its main thread.
11-26 03:56:01.316: W/dalvikvm(777): threadid=12: thread exiting with uncaught exception (group=0x414c4700)
11-26 03:56:01.536: E/AndroidRuntime(777): FATAL EXCEPTION: AsyncTask #2
11-26 03:56:01.536: E/AndroidRuntime(777): java.lang.RuntimeException: An error occured while executing doInBackground()
11-26 03:56:01.536: E/AndroidRuntime(777):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-26 03:56:01.536: E/AndroidRuntime(777):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)

有没有想过在onCreate之前获得intent消息的价值?

1 个答案:

答案 0 :(得分:0)

尝试以下操作并告诉我Log.d打印出来的内容*

  public class Result extends Activity {
    String url; // <<< I need the message to put it here

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_result);

      Bundle extras = getIntent().getExtras();
       if (extras != null) {
          mainURL= extras.getString(SingleSearchPage.EXTRA_MESSAGE);
       }
       Log.d("URL", "url =" + mainURL);

        url = mainURL;  //Tried to change the value of URL but did not work

        new GetJSONTask().execute(url);

    }