android,来自自定义的新 Intent class
android, new Intent from custom class
我希望 startActivity(intent)
来自自定义 class。
当我尝试时:
Intent listAllNumbers = new Intent(this, numbers.class);
listAllNumbers.putExtra("Data", jsonResult);
appContext.startActivity(listAllNumbers);
appContext
正在 getApplicationContext()
上下文作为参数传递给自定义 class (AsyncTask
)
通常我有 ..new Intent(MainActivity.this, numbers.class);
但我没有实例化 MainActivity 的意图。我试过输入 class 名称,但我得到 Cannot resolve constructor
。做什么?
ApplicationContext 不允许启动 Activity,您可以阅读有关 here 的更多信息。
您需要使用当前 Activity 中的当前上下文,如下所示:
Intent listAllNumbers = new Intent(this, numbers.class);
在片段中,这样:
Intent listAllNumbers = new Intent(getActivity(), numbers.class);
来自另一个未扩展 Activity 或 Fragment 的 类,您需要通过构造函数或 setter:
设置上下文
public void setContext(Context context) { // called by the activity or fragment
this.context = context;
}
Intent listAllNumbers = new Intent(this.context, numbers.class);
我希望 startActivity(intent)
来自自定义 class。
当我尝试时:
Intent listAllNumbers = new Intent(this, numbers.class);
listAllNumbers.putExtra("Data", jsonResult);
appContext.startActivity(listAllNumbers);
appContext
正在 getApplicationContext()
上下文作为参数传递给自定义 class (AsyncTask
)
通常我有 ..new Intent(MainActivity.this, numbers.class);
但我没有实例化 MainActivity 的意图。我试过输入 class 名称,但我得到 Cannot resolve constructor
。做什么?
ApplicationContext 不允许启动 Activity,您可以阅读有关 here 的更多信息。
您需要使用当前 Activity 中的当前上下文,如下所示:
Intent listAllNumbers = new Intent(this, numbers.class);
在片段中,这样:
Intent listAllNumbers = new Intent(getActivity(), numbers.class);
来自另一个未扩展 Activity 或 Fragment 的 类,您需要通过构造函数或 setter:
设置上下文public void setContext(Context context) { // called by the activity or fragment
this.context = context;
}
Intent listAllNumbers = new Intent(this.context, numbers.class);