Android Intent Cannot resolve constructor

AndroidAndroid Intent

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

Or you can simply start the activity as shown below;

startActivity( new Intent(currentactivity.this, Tostartactivity.class));

Solution 3 - Android

You may use this:

Intent intent = new Intent(getApplicationContext(), ClassName.class);

Solution 4 - Android

> Using .getActivity() solves this issue:

For eg.

Intent i= new Intent(MainActivity.this.getActivity(), Next.class);
startActivity(i);

Hope this helps.

Cheers.

Solution 5 - Android

You Can not Use the Intent's Context for Creating Intent. So You need to use your Fragment's Parent Activity Context

Intent intent = new Intent(getActivity(),MyClass.class);

Solution 6 - Android

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.

Fixed using closing Android Studio and moving the repository to other location and opening the the project once again. Fixed the problem.

Seems like Android Studio building problem.

Solution 7 - Android

this work for me

    ncharacters.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent openncharacter = new Intent(getApplicationContext(),ncharacters.class);
            startActivity(openncharacter);
        }
    });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPullView Question on Stackoverflow
Solution 1 - Androidρяσѕρєя KView Answer on Stackoverflow
Solution 2 - AndroidwanjikuView Answer on Stackoverflow
Solution 3 - AndroidAditya MhamunkarView Answer on Stackoverflow
Solution 4 - AndroidCrime_Master_GoGoView Answer on Stackoverflow
Solution 5 - AndroidSubramanian RamsundaramView Answer on Stackoverflow
Solution 6 - AndroidgauravsngargView Answer on Stackoverflow
Solution 7 - AndroidEhsano KhanView Answer on Stackoverflow