0

i am trying to customize listview which is working fine and now i have made customclass extends from baseAdapter in this part getView method i am applying onclickListener in imageview and starting new Activity this is crashing app code

 public class CustomAdapterClass extends BaseAdapter
{
ArrayList<FatwaBean> fatwaArrayList;
Context context;

CustomAdapterClass(ArrayList<FatwaBean> data,Context c) {
    fatwaArrayList = data;
    context = c;
}
public int getCount() {
    // TODO Auto-generated method stub
    return fatwaArrayList.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return fatwaArrayList.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     View v = convertView;
     if (v == null)
     {
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.search_fatwa_row, null);
     }

      final ImageView image = (ImageView) v.findViewById(R.id.search_image_row);
       TextView dateTextView = (TextView)v.findViewById(R.id.search_date_row);

       FatwaBean fatwaBean= fatwaArrayList.get(position);
     //image.setImageResource(fatwaBean.getQuestionImage());
       image.setImageResource(R.drawable.app_icon);
      //image.setTag(fatwaBean.getFatwaTopicQuestionId());
       dateTextView.setText(fatwaBean.getFatwaDate());
       Log.i("fatwaBean",""+fatwaBean.getFatwaDate());
       Log.i("fatwaBean",""+fatwaBean.getFatwaImageName());
       Log.i("fatwaBean Topic Question Image Name",""+fatwaBean.getQuestionImage());

       image.setOnClickListener(new OnClickListener() {              
               public void onClick(View v) {
               // TODO Auto-generated method stub


               final int itemname= (int)fatwaArrayList.get(position).getFatwaTopicQuestionId();
               Log.i("clicked Fatwa Image Topic Id",""+fatwaArrayList.get(position).getFatwaTopicQuestionId());
               Intent intent = new Intent(context,FatwaZoomActivityImageView.class);
           //passing fatwa topic question Id to get FatwaImage in NextActivity

              intent.putExtra("questionTopicIdForFatwa",fatwaArrayList.get(position).getFatwaTopicQuestionId());
              intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
               context.startActivity(intent);

           }                        
       });
    return v;
}

Logcat:

02-19 22:02:05.220: E/AndroidRuntime(578): FATAL EXCEPTION: main
02-19 22:02:05.220: E/AndroidRuntime(578): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.app.ContextImpl.startActivity(ContextImpl.java:803)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
02-19 22:02:05.220: E/AndroidRuntime(578):  at com.jamia.binoria.CustomAdapterClass$1.onClick(CustomAdapterClass.java:73)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.view.View.performClick(View.java:3100)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.view.View$PerformClick.run(View.java:11644)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.os.Handler.handleCallback(Handler.java:587)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.os.Looper.loop(Looper.java:126)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.app.ActivityThread.main(ActivityThread.java:3997)
02-19 22:02:05.220: E/AndroidRuntime(578):  at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:02:05.220: E/AndroidRuntime(578):  at java.lang.reflect.Method.invoke(Method.java:491)
02-19 22:02:05.220: E/AndroidRuntime(578):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-19 22:02:05.220: E/AndroidRuntime(578):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-19 22:02:05.220: E/AndroidRuntime(578):  at dalvik.system.NativeStart.main(Native Method)
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
user3233280
  • 279
  • 2
  • 7
  • 21

4 Answers4

1

Add this before the startActivity(), as suggested in the LogCat.

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
0

The logcat message seems pretty straightforward to me:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

If you don't want to use FLAG_ACTIVITY_NEW_TASK, then your Adapter should probably call back to your main Activity or Fragment to start the next Activity.

Since you are calling intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);, I'm guessing that you don't really want the new task flag, and calling back to the parent Activity/Fragment is your best option.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • please mention whats the solution ? – user3233280 Feb 19 '14 at 17:11
  • I did. If you don't need `FLAG_ACTIVITY_MULTIPLE_TASK`, then you can replace that flag with `FLAG_ACTIVITY_NEW_TASK`. If you need the multiple task flag, then your adapter will need to call back to your Activity (through an interface or a public method in the Activity), which should then start the next Activity. – Bryan Herbst Feb 19 '14 at 17:13
0

Maybe this should be a comment but it's a little long, so...

context.startActivity(intent);

Context has a StartActivity method, and so does Activity. Your context variable is of type Context, which points to an Activity object, but I don't know if the method being invoked belongs to the Context or to the Activity object.

Maybe you could try replacing your member variable Context context with Activity parentActivity??

Merlevede
  • 8,140
  • 1
  • 24
  • 39
0

The Context you passed to the CustomAdapterClass constructor was not an Activity context but something different, possibly an Application. Hence the exception. Read this article for more explanation what different Contexts are good for.

Two solutions:

  1. Pass in an Activity context. This should be the preferred option.

  2. Add the FLAG_ACTIVITY_NEW_TASK as suggested by some other answers. Note that doing so has implications to how activity back stack navigation works and it's not necessarily something you want.

laalto
  • 150,114
  • 66
  • 286
  • 303