启动从适配器活动不与SwipeListView库工作(Start activity from ada

2019-10-18 00:53发布

我使用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;
}
}

当我按一下按钮我得到这个错误:“应用程序已意外停止”。 但是,当我使用吐司,它工作正常。 我认为有一些与开始活动不一致。

logcat的:

04-16 18:42:29.531:E / AndroidRuntime(29813):致命异常:主04-16 18:42:29.531:E / AndroidRuntime(29813):04-16显示java.lang.NullPointerException 18:42:29.531: E / AndroidRuntime(29813):在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):在机器人.app.ActivityThread.main(ActivityThread.java:3687)04-16 18:42:29.531:E / AndroidRuntime(29813):在java.lang.reflect.Method.invokeNative(本机方法)04-16 18时42: 29.531:E / AndroidRuntime(29813):在java的。 lang.reflect.Method.invoke(Method.java:507)04-16 18:42:29.531:E / AndroidRuntime(29813):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867 )04-16 18:42:29.531:E / AndroidRuntime(29813):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)04-16 18:42:29.531:E / AndroidRuntime(29813 ):在dalvik.system.NativeStart.main(本机方法)

Answer 1:

context是一个领域ProductAdapter在你的情况,你不带任何值初始化它,所以它是null 。 使用getContext()代替或初始化context与现场this.context = context



Answer 2:

您收到的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);
        }
    });
...
...


文章来源: Start activity from adapter don't work with SwipeListView library