Mohamed Atef
Mohamed Atef

Reputation: 69

java.lang.reflect.InvocationTargetException appears while starting Activity

This is a block of code inside my ListView Customized Adapter :

@Override
public View getView(int i, View convertView, ViewGroup parent) {
    final MovieEntity feedItem=feedTrailersList.get(i);

    //LinearLayout Trailers_Linear=(LinearLayout) convertView.findViewById(R.id.Trailers_Linear);

    View view=convertView;
    if (view==null){
        view=LayoutInflater.from(getContext()).inflate(R.layout.trailer_list_item, parent,false);
    }
    TextView trailerName=(TextView)view.findViewById(R.id.trailer_name);
    trailerName.setText(feedItem.getTRAILER_NAME_STRING());
    trailerName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent(String.valueOf(Intent.FLAG_ACTIVITY_NEW_TASK));
            intent.setData(Uri.parse(feedItem.getTRAILER_KEY_STRING()));
            mContext.startActivity(intent);

        }
    });
 return view;
}

Here, i am trying to startActivity via my Context, as i am in a cutomized adapter class not in an activity, but it gives me this error titles :

ex: java.lang.reflect.InvocationTargetException
cause : android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

i was using Intent intent=new Intent(Intent.ACTION_VIEW); flag, and tried Intent intent=new Intent(String.valueOf(Intent.FLAG_ACTIVITY_NEW_TASK)); after it displayed this exception.

Can anybody tell my what does this exception refers to, your aid is completely, and respectively appreciated. Thanks in advance.

Upvotes: 0

Views: 2069

Answers (4)

Anees Ahmad
Anees Ahmad

Reputation: 83

In the adapter constructor, it is essential to initialize the mContext variable. When incorporating this adapter into an activity, it is imperative to supply the context of that activity. Alternatively, you can utilize the this keyword to furnish the necessary context.

Upvotes: 0

j2ko
j2ko

Reputation: 2539

Don't know what is your mContext. But you can always take proper context from parent.getContext(). Also your intent looks weird. It should crash with ActivityNotFoundException: No Activity found to handle Intent.

Intent flag should be used like so:

Inten intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...

Upvotes: 0

Mohamed Atef
Mohamed Atef

Reputation: 69

This is my block of current Intent Code now, and it's working

Intent intent=new Intent(); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
intent.setData(Uri.parse(feedItem.getTRAILER_KEY_STRING())); 
getContext().startActivity(intent); 

Upvotes: 0

ianhanniballake
ianhanniballake

Reputation: 199825

Your mContext variable is not an Activity - perhaps you are using getApplicationContext() to create mContext.

In any case, there's no need to have a Context variable at all - you can use getContext() at any point to retrieve the current Context.

Upvotes: 1

Related Questions