【问题标题】:android, new Intent from custom classandroid,来自自定义类的新意图
【发布时间】:2015-04-23 14:39:31
【问题描述】:

我希望从自定义类startActivity(intent)

当我尝试时:

Intent listAllNumbers = new Intent(this, numbers.class);
listAllNumbers.putExtra("Data", jsonResult);
appContext.startActivity(listAllNumbers);

appContextgetApplicationContext() 上下文作为参数传递给自定义类 (AsyncTask)

通常我有 ..new Intent(MainActivity.this, numbers.class); 但我没有从 MainActivity 实例化意图。我试过输入类名,但我得到了Cannot resolve constructor。做什么?

【问题讨论】:

  • 显示 numbers.class 代码。记住要遵循 java 代码风格并使用 FirstLetter UpperCase 创建类
  • 什么时候开始,从另一个活动开始?
  • 来自自定义类。一个扩展 AsyncTask

标签: java android android-intent


【解决方案1】:

ApplicationContext 不允许启动一个Activity,你可以阅读更多关于here的内容。

您需要使用当前 Activity 中的当前上下文,如下所示:

Intent listAllNumbers = new Intent(this, numbers.class);

在片段中,这样:

Intent listAllNumbers = new Intent(getActivity(), numbers.class);

从另一个不扩展Activity或Fragment的类,你需要通过contructor或setter来设置上下文:

public void setContext(Context context) { // called by the activity or fragment
    this.context = context;
}

Intent listAllNumbers = new Intent(this.context, numbers.class);

【讨论】:

  • 我将如何开始意图? this.context.startActivity(listAllNumbers) 不起作用
  • 你有没有工作的错误? number.class 在 AndroidManifest.xml 中声明?
  • 已解决:Intent.FLAG_ACTIVITY_NEW_TASK 感谢您的帮助 :)
猜你喜欢
  • 1970-01-01
  • 2011-05-19
  • 2011-10-17
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
相关资源
最近更新 更多