我正在使用SwipeListView库,并且在尝试在我的适配器中启动活动时遇到错误。
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;
}
}
当我点击按钮时,我收到此错误:“应用程序意外停止”。但是当我使用Toast时,它工作正常。我认为有一些与开始活动不一致的事情。
logcat的:
04-16 18:42:29.531:E / AndroidRuntime(29813):致命异常:主要 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):在android.view.View.performClick(View.java:2485) 04-16 18:42:29.531:E / AndroidRuntime(29813):在android.view.View $ PerformClick.run(View.java:9080) 04-16 18:42:29.531:E / AndroidRuntime(29813):在android.os.Handler.handleCallback(Handler.java:587) 04-16 18:42:29.531:E / AndroidRuntime(29813):在android.os.Handler.dispatchMessage(Handler.java:92) 04-16 18:42:29.531:E / AndroidRuntime(29813):在android.os.Looper.loop(Looper.java:130) 04-16 18:42:29.531:E / AndroidRuntime(29813):在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)
答案 0 :(得分:1)
context
在您的情况下是ProductAdapter
的字段,您不会使用任何值对其进行初始化,因此它是null
。请改为使用getContext()
或使用context
初始化this.context = context
字段。
答案 1 :(得分:0)
您在构造函数中收到Context
,因此您可以存储到变量上下文中:
private Context context;
public ProductAdapter(Context context, int textViewResourceId, List<Product> objects) {
super(context, textViewResourceId, objects);
this.context = context; //*** Here!
}
并将其用于Adapter
课程,例如:
...
...
viewHolder.bAction1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
}
});
...
...