Sergey
Sergey

Reputation: 1

Can't open new Activity in Android Studio

This is my code for first page and when I want to start an Activity I have this error:

Cannot resolve constructor 'Intent(com.example.loginpage.contact,java.lang.Class)'

public class contact extends AsyncTask<String,Void,String>{
Context context;
contact (Context ctx){
    context = ctx;
}
protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        if (result.equals("Wrong email or password.")) {

        } else
        {
            context.startActivity( new Intent(contact.this,mainpage.class));
        }
        alertDialog.show();
    }
}

I want open this activity:

public class mainpage extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainpage);
        String username = getIntent().getStringExtra("Username");
        TextView tv = (TextView)findViewById(R.id.username);
        tv.setText(username);
    }
}

Upvotes: 0

Views: 1133

Answers (2)

Josh Laird
Josh Laird

Reputation: 7224

You are trying to start a new Activity from outside of a Context. When you say contact.this in the line:

startActivity( new Intent(contact.this,mainpage.class));

you are referring to an AsyncTask as that's what contact extends. You should pass in a Context there instead. You can do that by passing Context into your class contact constructor.

Once you have Context passed into the contact you replace contact.this with:

context.startActivity(new Intent(context, mainpage.class));

Upvotes: 1

Mohammad irshad sheikh
Mohammad irshad sheikh

Reputation: 888

public class contact extends AsyncTask<String, Void, String> {
    Context context;

    contact(Context context) {
        this.context = context;
    }
    //yout other staff

}

then use startActivity( new Intent(context,packagename.mainpage.class));

Upvotes: 0

Related Questions