Android Intent无法解析构造函数 [英] Android Intent Cannot resolve constructor

查看:268
本文介绍了Android Intent无法解析构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类扩展了Fragment,第二类扩展了Activity.

I have a first class extending Fragment, and a second class extending Activity.

我的片段运行正常,并且片段中的Intent代码是:

My Fragment is working fine, and my code for the Intent in the Fragment is :

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);            }
    });

我的班级MyClass代码是:

My class MyClass code is :

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);
    } 
}

错误是:

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.

推荐答案

使用

Intent myIntent = new Intent(v.getContext(), MyClass.class);

 Intent myIntent = new Intent(MyFragment.this.getActivity(), MyClass.class);

开始一个新的活动.这是因为在为应用程序的特定组件创建Intent时,需要将Application或组件上下文作为第一个参数传递给Intent构造函数.

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.

这篇关于Android Intent无法解析构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆