我有一个AsyncTask,它使用来自Internet的已解析数据填充自定义列表。如何从Android中的PostExecute正确启动活动?
在PostExecute中填充该列表并准备好将其转移到新的活动中。
我做这种方式:
@Override
protected void onPostExecute(List<VideoDataDescription> result)
{
super.onPostExecute(result);
MainActivity.progressDialog.dismiss();
context.startActivity(new Intent(context, ResultsQueryActivity.class));
}
其中上下文
private Context context;
在logcat中执行此代码后,我收到了显示java.lang.NullPointerException。 这是可能的和正确的开始一个活动,因为我这样做?
UPD 我已经加入
private Context mContext;
public YoutubeAndYahooParser(Context context)
{
super();
this.mContext = context;
}
初始化上下文和调用
YoutubeAndYahooParser youtubeAndYahooParser = new YoutubeAndYahooParser(ResultsQueryActivity.this);
youtubeAndYahooParser.execute("my string to pass in asynctak");
在此之后在PostExecute
Intent intent = new Intent(mContext, ResultsQueryActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
我增添了新的标志,因为我有在LogCat中下一个:
*从Activity上下文外调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?*
对吗?
你是怎么初始化'context'的? – Vladimir 2012-02-02 18:47:33
似乎是,没有。如何初始化上下文? – 2012-02-02 19:04:27
看到dhaag23的回答 – Vladimir 2012-02-02 19:14:49