Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Intent Cannot resolve constructor

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

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

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

The error is :

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.

like image 828
Pull Avatar asked Nov 27 '13 11:11

Pull


3 Answers

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.

like image 152
ρяσѕρєя K Avatar answered Oct 16 '22 05:10

ρяσѕρєя K


Or you can simply start the activity as shown below;

startActivity( new Intent(currentactivity.this, Tostartactivity.class));
like image 8
wanjiku Avatar answered Oct 16 '22 06:10

wanjiku


You may use this:

Intent intent = new Intent(getApplicationContext(), ClassName.class);
like image 5
Aditya Mhamunkar Avatar answered Oct 16 '22 05:10

Aditya Mhamunkar