Kry*_*NaC 3 android android-intent android-asynctask android-fragments
我已经实现了一个4睫毛的FragmentPagerAdapter,并且在每个中我加载了一个具有不同视图的片段.在其中一个中,按下执行AsyncTask的图像以从服务器获取一系列数据,并通过onPostExecute()方法的意图加载新类.
我在一个活动中有这个功能并且工作得很好.现在要从片段进行调用,我必须使用此类的静态模式进行调用,并且在代码行'startActivity(i)'中出现错误:
//AsyncTask
private static class CargarJSON extends AsyncTask<String, Void, String> {
Context mContext;
public CargarJSON(Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mProgressItem.setVisible(true);
mProgressItem.setActionView(R.layout.actionbar_indeterminate_progress);
mProgressItem.expandActionView();
}
@Override
protected String doInBackground(String... params) {
String url = params[0];
String data = MetodosJSON.getHttpResponse(url);
MetodosJSON.parseaJSON2(data, IniSelCategoria.ac);
return params[1];
}
@Override
protected void onPostExecute(String titulo) {
super.onPostExecute(titulo);
// start new activity
Intent i = new Intent(mContext, PantallaInfo.class);
i.putExtra("title", titulo);
i.putExtra("URLser", urlSer);
**startActivity(i);**
mProgressItem.setVisible(false);
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity
Run Code Online (Sandbox Code Playgroud)
如何让方法调用'startActivity(i)'?
非常感谢你.
将其更改为
mContext.startActivity(i);
Run Code Online (Sandbox Code Playgroud)
context如果不是从a 调用,则需要使用a 来调用该方法Activity.幸运的是,你已经传递Context给构造函数了.
| 归档时间: |
|
| 查看次数: |
2293 次 |
| 最近记录: |