问题
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)
回答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
.
回答2:
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);
}
});
...
...
来源:https://stackoverflow.com/questions/16045006/start-activity-from-adapter-dont-work-with-swipelistview-library