带基本适配器的 NullPointerException CustomList ViewAdapter Android

NullPointerException CustomList ViewAdapter with base adapter Android

我正在尝试创建一个包含一个图像和 3 个文本视图的自定义列表视图,但我在代码的 "listView.setAdapter(adapter);" 行收到此 NullPointException 错误。 通过调试应用程序,我可以看到我的列表产品正在从我的 SQLite 数据库中获取所有必要的数据,所以我认为问题不存在。我现在正在设置修复图像,因为这是我第一次使用自定义列表视图进行测试。所以我试图先让它工作,然后我会实现其余的。

` 我的 activity 应该实现自定义列表视图--

 public class ShowAllProdutosActivity extends ActionBarActivity {
 ListView listView;
 ArrayList<Produto> produtos;
 String[] titleArray;
 String[] descriptionArray;
 String[] priceArray;
 int[] imagensArray;
 CustomListViewAdapter adapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     // TODO: Implement this method
     super.onCreate(savedInstanceState);
     setContentView(R.layout.listview_procura); /*Fixed*/
     getSupportActionBar().setDisplayHomeAsUpEnabled(true);
     produtos = new ArrayList<>();
     listView = (ListView) findViewById(R.id.listaProdutos);
     final DatabaseHelper db = new DatabaseHelper(this);
     produtos = db.getAllProdutos();
     db.closeDB();
     if (!produtos.isEmpty()) {
         adapter = new CustomListViewAdapter(this,produtos);
        listView.setAdapter(adapter);
     } else {
         Toast.makeText(this, "Nenhum Produto Encontrado", Toast.LENGTH_LONG);
     }
  }
}`

`CustomListViewAdapter.java--

public class CustomListViewAdapter extends BaseAdapter {

Context context;

protected List<Produto> listProds;
LayoutInflater inflater;

public CustomListViewAdapter(Context context, List<Produto> listProds) {
    this.listProds = listProds;
    this.inflater = LayoutInflater.from(context);
    this.context = context;
}

public int getCount() {
    return listProds.size();
}

public Produto getItem(int position) {
    return listProds.get(position);
}

public long getItemId(int position) {
    return listProds.get(position).getId();
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View row = convertView;
    if (row == null) {

        holder = new ViewHolder();
        row = this.inflater.inflate(R.layout.listview_item,
                parent, false);

        holder.txtName = (TextView) row
                .findViewById(R.id.txtViewNomeProduto);
        holder.txtDesc = (TextView) row
                .findViewById(R.id.txtViewDescricaoProduto);
        holder.txtPrice = (TextView) row
                .findViewById(R.id.txtViewPrecoProduto);
        holder.imgProd = (ImageView) row
                .findViewById(R.id.produtoImgView);

        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    Produto prod = listProds.get(position);
    holder.txtName.setText(prod.getProd_name());
    holder.txtDesc.setText(prod.getProd_desc());
    holder.txtPrice.setText(prod.getProd_price() + " R$");
    holder.imgProd.setImageResource(R.mipmap.ic_launcher);

    return convertView;
}

private class ViewHolder {
    TextView txtName;
    TextView txtDesc;
    TextView txtPrice;
    ImageView imgProd;
}

我的activity布局listview_procura.xml--

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/listaProdutos"
    android:layout_alignParentTop="true" />
 </RelativeLayout>

我的项目布局 listview_item.xml--

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/produtoImgView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/txtViewNomeProduto"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/produtoImgView"
    android:layout_toEndOf="@+id/produtoImgView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/txtViewDescricaoProduto"
    android:layout_toRightOf="@+id/produtoImgView"
    android:layout_below="@+id/txtViewNomeProduto"
    android:layout_above="@+id/txtViewPrecoProduto"
    android:layout_toLeftOf="@+id/txtViewPrecoProduto"
    android:layout_toStartOf="@+id/txtViewPrecoProduto" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/txtViewPrecoProduto"
    android:layout_alignBottom="@+id/produtoImgView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
 </RelativeLayout>

我得到的日志--

04-12 20:36:30.533  28463-28463/com.paum.pechinchamercado E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.paum.pechinchamercado, PID: 28463
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paum.pechinchamercado/com.paum.pechinchamercado.activities.ShowAllProdutosActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
        at android.app.ActivityThread.access0(ActivityThread.java:161)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.paum.pechinchamercado.activities.ShowAllProdutosActivity.onCreate(ShowAllProdutosActivity.java:50)
        at android.app.Activity.performCreate(Activity.java:5426)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)      
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
        at android.app.ActivityThread.access0(ActivityThread.java:161)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

`

您正在将内容视图设置为 R.layout.add_prod,但您声称布局名为 listview_procura.xml。如果您将内容视图设置为错误的布局然后调用 findViewById() 以获取 ListView 如果您没有具有相同 ID 的 ListView 将 return 为空add_prod.xml

除了 kris larson 指出的布局问题,我遇到的没有显示列表视图的问题应该是 "return convertView" 行 "return row"