很抱歉问了这个简单的问题,但我很困惑。在我的代码中,我想在我的一种方法完成后启动一个新活动。所以我只是需要有人告诉我最好放在哪里Intent I = new Intent(MainActivity.this, ListView.class); startActivity(I);
我在这里尝试过,但有人告诉我这可能会导致错误
public class MainActivity extends Activity {
EditText et;
Button getanswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button getanswer = (Button) findViewById(R.id.button1);
getanswer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new asynctask().execute();
Intent I = new Intent(MainActivity.this, ListView.class);
startActivity(I);
}
});
}
}
class asynctask extends AsyncTask<String, Void, JSONObject> {
private static final String TAG_RESULTS = "ResultsSet";
private static final String TAG_SUBJECT = "Subject";
private static final String TAG_NUMANSWERS = "NumAnswers";
private static final String TAG_QUESTION = "Question";
private static final String TAG_QUESTION_CONTENT = "Content";
private static final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
private static final String TAG_QUESTION_ANSWERS = "Answers";
public JSONArray json;
@Override
protected JSONObject doInBackground(String... params) {
JSONParsser jParser = new JSONParsser();
String URI = "http://example.com/json";
JSONObject json = jParser.getJSONFromURI(URI);
return json ;
}{ }
@Override
protected void onPostExecute(JSONObject json) {
JSONArray ResultsSet = null; {
try {
ResultsSet = json.getJSONArray(TAG_RESULTS);
} catch (JSONException e) {
e.printStackTrace();
}
return;
}}}
我也尝试过像大多数人建议的那样听到它,但我在“MainActivity.this”下面收到一个错误,上面写着“在范围内没有可以访问 MainActivity 类型的封闭实例”。此外,我在“startActivity(I);”下得到一个错误 它说我必须创建一个名为“startActivity(Intent)”的全新方法
public class MainActivity extends Activity {
EditText et;
Button getanswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button getanswer = (Button) findViewById(R.id.button1);
getanswer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new asynctask().execute();
}
});
}
}
class asynctask extends AsyncTask<String, Void, JSONObject> {
private static final String TAG_RESULTS = "ResultsSet";
private static final String TAG_SUBJECT = "Subject";
private static final String TAG_NUMANSWERS = "NumAnswers";
private static final String TAG_QUESTION = "Question";
private static final String TAG_QUESTION_CONTENT = "Content";
private static final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
private static final String TAG_QUESTION_ANSWERS = "Answers";
public JSONArray json;
@Override
protected JSONObject doInBackground(String... params) {
JSONParsser jParser = new JSONParsser();
String URI = "http://rxample.com/json";
JSONObject json = jParser.getJSONFromURI(URI);
return json ;
}{ }
@Override
protected void onPostExecute(JSONObject json) {
JSONArray ResultsSet = null; {
try {
ResultsSet = json.getJSONArray(TAG_RESULTS);
} catch (JSONException e) {
e.printStackTrace();
}
Intent I = new Intent(MainActivity.this, ListView.class);
startActivity(I);
return;
}}}
有人可以告诉我为什么会弹出这些错误以及放置此代码的正确位置。