gpt4 book ai didi

android - 从适配器启动 Activity 不适用于 SwipeListView 库

转载 作者:行者123 更新时间:2023-11-30 03:39:20 24 4
gpt4 key购买 nike

我正在使用 SwipeListView库,尝试在我的适配器中启动 Activity 时出现错误。

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 时,它工作正常。我认为开始 Activity 有些不一致。

日志:

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)

最佳答案

context 在您的案例中是 ProductAdapter 的一个字段,您没有使用任何值对其进行初始化,因此它是 null。请改用 getContext() 或使用 this.context = context 初始化 context 字段。

关于android - 从适配器启动 Activity 不适用于 SwipeListView 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16045006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com