krzyhub
krzyhub

Reputation: 6540

Android studio - can not launch activity from other class

From MainActivity I easily launch my PictureList activity.

public void buttonOnClick(View v) {
    Context context = getApplicationContext();
    Button b = (Button)v;
    String btnTxt = b.getText().toString();

    Intent intent = new Intent(MainActivity.this, PicturesList.class);
    Bundle bundle = new Bundle();
    bundle.putString("category", btnTxt);
    intent.putExtras(bundle);
    startActivity(intent);
    finish();
}

In my PictureList class I successfully creating a listView of pictures

 adapter = new LazyImageLoadAdapter(PicturesList.this, strings, names, ratings, ids);

 list.setAdapter(adapter);

But in LazyImageLoadAdapter I have an onClickListener for every picture from the list and I can't succesfully activate ``PictureView` activity from it.

private class OnItemClickListener implements OnClickListener {

    private int position;

    OnItemClickListener(int pos) {
        position = pos;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(LazyImageLoadAdapter.this, PictureView.class);
        Bundle bundle = new Bundle();
        bundle.putString("id", ids[position]);
        intent.putExtras(bundle);
    }
}

Error is like so:

Error:(116, 29) error: no suitable constructor found for Intent(LazyImageLoadAdapter,Class<PictureView>)
constructor Intent.Intent(String,Uri,Context,Class<?>) is not applicable
(actual and formal argument lists differ in length)
constructor Intent.Intent(Context,Class<?>) is not applicable
(actual argument LazyImageLoadAdapter cannot be converted to Context by method invocation conversion)
constructor Intent.Intent(String,Uri) is not applicable
(actual argument LazyImageLoadAdapter cannot be converted to String by method invocation conversion)
constructor Intent.Intent(String) is not applicable
(actual and formal argument lists differ in length)
constructor Intent.Intent(Intent) is not applicable
(actual and formal argument lists differ in length)
constructor Intent.Intent() is not applicable
(actual and formal argument lists differ in length)

Could anyone give me some tips or solution for that, please?

Upvotes: 0

Views: 1556

Answers (2)

siriuseteor77
siriuseteor77

Reputation: 1126

Because LazyImageLoadAdapter.this does not return a context.

Try this:

 @Override
    public void onClick(View v) {
        Intent intent = new Intent(v.getContext(), PictureView.class);
        Bundle bundle = new Bundle();
        bundle.putString("id", ids[position]);
        intent.putExtras(bundle);
    }

Upvotes: 2

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20646

Replace this

Intent intent = new Intent(LazyImageLoadAdapter.this, PictureView.class);
Bundle bundle = new Bundle();
bundle.putString("id", ids[position]);
intent.putExtras(bundle);

To this :

Intent intent = new Intent(v.getContext(), PictureView.class);
Bundle bundle = new Bundle();
bundle.putString("id", ids[position]);
intent.putExtras(bundle);

Upvotes: 3

Related Questions