Search code examples
androidandroid-intentandroid-2.2-froyo

Android SDK: Constructor Intent is Undefined Error


I am building an Intent in my android application and I am using the standard format classname.this but I am getting "The constructor Intent(MySampleFragment, Class) is undefined" error

FYR this is the part of my code which runs into the error.

    @Override
    public void onClick(View v) {
           Intent i = new Intent(MySampleFragment.this,Alert.class);
           startActivity(i);
    }

Solution

  • Use getActivity() or v.getContext() instead of MySampleFragment.this as first parameter of Intent constructor as:

    Intent i = new Intent(getActivity(),Alert.class);
    //Intent i = new Intent(v.getContext(),Alert.class);
    startActivity(i);