Android意图无法解析构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20241857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Intent Cannot resolve constructor
提问by Pull
I have a first class extending Fragment, and a second class extending Activity.
我有一个扩展 Fragment 的第一类,和一个扩展 Activity 的第二类。
My Fragment is working fine, and my code for the Intent in the Fragment is :
我的 Fragment 工作正常,我在 Fragment 中的 Intent 代码是:
ImageButton button= (ImageButton) getView().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MyFragment.this, MyClass.class);
MyFragment.this.startActivity(myIntent); }
});
My class MyClass code is :
我的课程 MyClass 代码是:
public class MyClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
}
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.MyClass);
}
}
The error is :
错误是:
Gradle: cannot find symbol constructor Intent(com.xxxx.xxxx.MyFragment,java.lang.Class<com.xxxx.xxxx.MyClass>)
Gradle: cannot find symbol constructor Intent(com.xxxx.xxxx.MyFragment,java.lang.Class<com.xxxx.xxxx.MyClass>)
I don't know where I went wrong.
我不知道我哪里错了。
回答by ρяσ?ρ?я K
Use
用
Intent myIntent = new Intent(v.getContext(), MyClass.class);
or
或者
Intent myIntent = new Intent(MyFragment.this.getActivity(), MyClass.class);
to start a new Activity. This is because you will need to pass Application or component context as a first parameter to the Intent Constructor when you are creating an Intent for a specific component of your application.
开始一个新的活动。这是因为当您为应用程序的特定组件创建意图时,您需要将应用程序或组件上下文作为第一个参数传递给意图构造函数。
回答by wanjiku
Or you can simply start the activity as shown below;
或者您可以简单地开始活动,如下所示;
startActivity( new Intent(currentactivity.this, Tostartactivity.class));
回答by Aditya Mhamunkar
You may use this:
你可以使用这个:
Intent intent = new Intent(getApplicationContext(), ClassName.class);
回答by Crime_Master_GoGo
Using
.getActivity()
solves this issue:
使用
.getActivity()
解决了这个问题:
For eg.
例如。
Intent i= new Intent(MainActivity.this.getActivity(), Next.class);
startActivity(i);
Hope this helps.
希望这可以帮助。
Cheers.
干杯。
回答by gauravsngarg
Same Error was coming with my code in Activity but not in Fragment. Showing constructor error for different line like new Intent( From.this, To.class) and new ArrayList<> etc.
我的代码在 Activity 中出现了同样的错误,但在 Fragment 中没有。显示不同行的构造函数错误,如 new Intent(From.this, To.class) 和 new ArrayList<> 等。
Fixed using closing Android Studio and moving the repository to other location and opening the the project once again. Fixed the problem.
修复了使用关闭 Android Studio 并将存储库移动到其他位置并再次打开项目的问题。修复了问题。
Seems like Android Studio building problem.
似乎是 Android Studio 构建问题。
回答by Subramanian Ramsundaram
You Can not Use the Intent's Context
for Creating Intent. So You need to use your Fragment's
Parent Activity
Context
您不能将Intent's Context
用于创建意图。所以你需要使用你的Fragment's
Parent Activity
Context
Intent intent = new Intent(getActivity(),MyClass.class);