1

私はアンドロイドが初めてで、このコードに問題があります。JSON 文字列を取得し、別のアクティビティを開始して ListView として表示しようとしています。
活動を開始できません。The constructor Intent(RequestJsonString, Class) is undefinedThe constructor Intent(RequestJsonString, Class) is undefinedと書かれています。

ここ: Intent intent = new Intent(RequestJsonString.this,DisplayResults.class); と ここ: RequestJsonString.this.startActivity(intent);

私はstackoverflowでこれに関する多くの投稿を読みactivity、 、contextおよびthis. しかし、まだ私はそれを正しく理解していません。私は何かが欠けているべきだと思います。どんな助けでも大歓迎です。

public class RequestJsonString extends AsyncTask<String, Void, JSONObject> {

@Override
protected JSONObject doInBackground(String... urls) {
    // Code HTTP Get Request and get JSONObject
            return jsonObject;
}

protected void onPostExecute(JSONObject jsonObj){
    try {

        Intent intent = new Intent(RequestJsonString.this,DisplayResults.class);
        intent.putExtra("JSON_Object", jsonObj.toString());
        RequestJsonString.this.startActivity(intent);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Log.v("Json_OutPut","Done");

}

}
4

2 に答える 2

2

AsyncTask からアクティビティを開始します。

Intent intent = new Intent(YourActivityName.this,DisplayResults.class);

または、以下のように同じことを行うことができます。

contextインスタンス変数を宣言し、onCreateメソッドで初期化します。

private Context context;
public void onCreate(Bundle bundle) {
   ............
   context = this;
   ........
}

こんな感じで活動開始。

Intent intent = new Intent(context,DisplayResults.class);
intent.putExtra("JSON_Object", jsonObj.toString());
startActivity(intent);
于 2013-04-21T06:52:38.193 に答える
1

あなたの場合、asynctask クラスのコンテキストを参照しています

Intent intent = new Intent(RequestJsonString.this,DisplayResults.class);

アクティビティ コンテキストを使用する

Intent intent = new Intent(ActivityName.this,DisplayResults.class);

リンクをチェックして、いつ getApplicationContext() を使用するか、いつ Activity Context を使用するかを確認してください

アクティビティ コンテキストまたはアプリケーション コンテキストをいつ呼び出すか?

編集:

Activity コンテキストを asynctask コンストラクターに渡す

 new RequestJsonString(ActivityName.this).execute(params..);

asynctask コンストラクターで

 Context c;
 public  RequestJsonString( Context context)
 {
        c= context;
 }

それで

   Intent intent = new Intent(c,DisplayResults.class);
   startActivity(intent);
于 2013-04-21T06:53:12.600 に答える