1

I am trying to start an Activity from my Custom Adapter for ListView in Android. I am declaring context but Logcat shows that Context not found. I am uploading my code here

    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;
    //context = this;

    if (v == null) {
        holder = new ViewHolder();
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
        holder.tvName = (TextView) v.findViewById(R.id.tvName);
        holder.tvDescription = (TextView) v.findViewById(R.id.tvDescriptionn);
        holder.tvDOB = (TextView) v.findViewById(R.id.tvDateOfBirth);
        holder.tvCountry = (TextView) v.findViewById(R.id.tvCountry);
        holder.tvHeight = (TextView) v.findViewById(R.id.tvHeight);
        holder.tvSpouse = (TextView) v.findViewById(R.id.tvSpouse);
        holder.tvChildren = (TextView) v.findViewById(R.id.tvChildren);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    holder.imageview.setImageResource(R.mipmap.ic_launcher);
    new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
    holder.tvName.setText(actorList.get(position).getName());
    holder.tvDescription.setText(actorList.get(position).getDescription());
    holder.tvDOB.setText("B'day: " + actorList.get(position).getDob());
    holder.tvCountry.setText(actorList.get(position).getCountry());
    holder.tvHeight.setText("Height: " + actorList.get(position).getHeight());
    holder.tvSpouse.setText("Spouse: " + actorList.get(position).getSpouse());
    holder.tvChildren.setText("Children: " + actorList.get(position).getChildren());

    holder.tvName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(), actorList.get(position).getName(), Toast.LENGTH_SHORT).show();
            Context context = getContext();
            Intent i = new Intent(context, Details.class);
            context.startActivity(i);
        }
    });
    return v;

}

Here I want to start a new Activity against a Button's click event.

Here is my LogCat

06-25 14:25:15.237    3179-3179/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.scroll.owner.jsonparsing, PID: 3179
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
        at android.app.ContextImpl.startActivity(ContextImpl.java:1421)
        at android.app.ContextImpl.startActivity(ContextImpl.java:1408)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:324)
        at com.scroll.owner.jsonparsing.ActorAdapter$1.onClick(ActorAdapter.java:76)
        at android.view.View.performClick(View.java:4881)
        at android.view.View$PerformClick.run(View.java:19592)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5756)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)

Can anyone suggest me a solution. Previously I have implemented this type of Starting Activity from a Java Class. But here the situation seems to me different. I am looking for your suggestion.

Vishwajit Palankar
  • 3,033
  • 3
  • 28
  • 48

5 Answers5

7

This problem is because you are starting an activity from CustomArrayAdapter Class, so for doing this you have to add a line between

Intent i = new Intent(context, Details.class);

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //add this line

context.startActivity(i);

And your code will work fine

Adi Tiwari
  • 761
  • 1
  • 5
  • 17
  • yes I also did the same and it works for me, But I first saw Neal Ahluvalia answer, so I accepted that answer. As your answer is helpful one I have voted up it. Thank you –  Jun 25 '15 at 08:53
1

Use

Intent intent=new Intent(...).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(i);
Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
  • 1
    No! it doesn't work for me. Logcat shows android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? –  Jun 25 '15 at 08:42
  • YEah provide it with FLAG_ACTIVITY_NEW_TASK – Neal Ahluvalia Jun 25 '15 at 08:44
  • `Intent intent=new Intent(...).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);` – Neal Ahluvalia Jun 25 '15 at 08:46
0

either use parent.getContext() or set context param in constructor of adapter while calling and use that refrence

Amarjit
  • 4,327
  • 2
  • 34
  • 51
0

There are several types of contexts. The adapter is not launched with context from Activity (which is good), but only an activity`s context can lunch an other activity ! So in this case, you have to pass your activity to your adapter , and use it as a context to lunch a new one.

Please mind the link: What is 'Context' on Android?

Community
  • 1
  • 1
narancs
  • 5,234
  • 4
  • 41
  • 60
0

try something like this....

pass context reference from activty to the CustomAdapter class..

    public class CustomAdapter extends Adapter {
     private Context context;

     public CustomAdapter (Context context) {
          this.context = context;     
     }   

}

now you can use this context to launch the activty.

RajSharma
  • 1,941
  • 3
  • 21
  • 34