1

I'm using SwipeListView library and I'm getting error when trying to start an activity in my adapter.

public class ProductAdapter extends ArrayAdapter<Product> {

private Context context;

public ProductAdapter(Context context, int textViewResourceId, List<Product> objects) {
    super(context, textViewResourceId, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;

    final Product product = getItem(position);

    if (convertView == null) {
        viewHolder = new ViewHolder();

        convertView = LayoutInflater.from(getContext()).inflate(R.layout.cproduct_list_item, null);

        viewHolder.nom = (TextView) convertView.findViewById(R.id.name);
        viewHolder.commerce = (TextView) convertView.findViewById(R.id.commerce);
        viewHolder.prix = (TextView) convertView.findViewById(R.id.price);
        viewHolder.bAction1 = (Button) convertView.findViewById(R.id.example_row_b_action_1);
        viewHolder.bAction2 = (Button) convertView.findViewById(R.id.example_row_b_action_2);
        viewHolder.bAction3 = (Button) convertView.findViewById(R.id.example_row_b_action_3);

        convertView.setTag(viewHolder);

    } 
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.nom.setText(product.getName());
    viewHolder.commerce.setText(product.getCommerce());
    viewHolder.prix.setText(String.format("%s F", product.getPrice()));

    viewHolder.bAction1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, MainActivity.class);
            context.startActivity(intent);
        }
    });

    return convertView;
}

private class ViewHolder {
    TextView nom;
    TextView commerce;
    TextView prix;
    Button bAction1;
    Button bAction2;
    Button bAction3;
}
}

When I click on the button I get this error: "The application has stopped unexpectedly". But when I use a Toast, it works fine. I think there is something inconsistent with starting activity.

Logcat:

04-16 18:42:29.531: E/AndroidRuntime(29813): FATAL EXCEPTION: main 04-16 18:42:29.531: E/AndroidRuntime(29813): java.lang.NullPointerException 04-16 18:42:29.531: E/AndroidRuntime(29813): at com.tomsyweb.suna.ProductAdapter$2.onClick(ProductAdapter.java:71) 04-16 18:42:29.531: E/AndroidRuntime(29813): at android.view.View.performClick(View.java:2485) 04-16 18:42:29.531: E/AndroidRuntime(29813): at android.view.View$PerformClick.run(View.java:9080) 04-16 18:42:29.531: E/AndroidRuntime(29813): at android.os.Handler.handleCallback(Handler.java:587) 04-16 18:42:29.531: E/AndroidRuntime(29813): at android.os.Handler.dispatchMessage(Handler.java:92) 04-16 18:42:29.531: E/AndroidRuntime(29813): at android.os.Looper.loop(Looper.java:130) 04-16 18:42:29.531: E/AndroidRuntime(29813): at android.app.ActivityThread.main(ActivityThread.java:3687) 04-16 18:42:29.531: E/AndroidRuntime(29813): at java.lang.reflect.Method.invokeNative(Native Method) 04-16 18:42:29.531: E/AndroidRuntime(29813): at java.lang.reflect.Method.invoke(Method.java:507) 04-16 18:42:29.531: E/AndroidRuntime(29813): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 04-16 18:42:29.531: E/AndroidRuntime(29813): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 04-16 18:42:29.531: E/AndroidRuntime(29813): at dalvik.system.NativeStart.main(Native Method)

tsil
  • 2,069
  • 7
  • 29
  • 43

2 Answers2

1

context is a field of ProductAdapter in your case and you don't initialize it with any value, so it's null. Use getContext() instead or initialize context field with this.context = context.

Michael
  • 53,859
  • 22
  • 133
  • 139
  • getContext() doesn't solve the problem. I get another log error: 04-16 19:20:32.226: E/AndroidRuntime(32041): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 04-16 19:20:32.226: E/AndroidRuntime(32041): at android.app.ContextImpl.startActivity(ContextImpl.java:654) – tsil Apr 16 '13 at 19:24
  • 1
    Thanks for your help. Finally get a solution here (use myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);) http://stackoverflow.com/questions/3918517/calling-startactivity-from-outside-of-an-activity-context – tsil Apr 16 '13 at 19:31
  • It's not a good idea really. If the context you pass to the `ProductAdapter` is an `Activity`, you'd better save it in a field and use to start another `Activity` without creating a new task. – Michael Apr 17 '13 at 05:51
  • Don't exactly understand what you say. Can you illustrate it with some piece of code? – tsil Apr 17 '13 at 22:40
  • Save the `context` you pass to the `ProductAdapter` in a field: `this.context = context;`. Then use this field to start an `Activity`, like you did in the question. – Michael Apr 18 '13 at 07:59
  • I tried it but it doesn't work: "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag" – tsil Apr 18 '13 at 17:28
  • What do you pass as a `context` to `ProductAdapter`? – Michael Apr 18 '13 at 17:35
  • Context context; public ProductAdapter(Context context, int textViewResourceId, List objects) { super(context, textViewResourceId, objects); this.context = context; } – tsil Apr 18 '13 at 18:34
  • No, I mean when you create a `ProductAdapter` with `new ProductAdapter(...)` what is the first argument there? – Michael Apr 19 '13 at 03:37
  • 2
    Oh, never use it! I think you call it from an `Activity` subclass, so just pass this activity to the constructor (`this` in your case). Then you can remove the `context` field from the `ProductAdapter` class and use `getContext()` without any limitations. – Michael Apr 19 '13 at 09:10
0

You are receiving the Context in the constructor, so you can store into the variable context:

private Context context;

public ProductAdapter(Context context, int textViewResourceId, List<Product> objects) {
    super(context, textViewResourceId, objects);
    this.context = context; //*** Here!
}

And use it into your Adapter class, for example:

...
...
  viewHolder.bAction1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, MainActivity.class);
            context.startActivity(intent);
        }
    });
...
...
Jorgesys
  • 124,308
  • 23
  • 334
  • 268