我想创建一个图像和3个textViews的自定义列表视图,但我得到这个NullPointException错误行“listView.setAdapter(adapter);”我的代码。 通过调试应用程序,我可以看到我的List产品从我的SQLite数据库中获取了所有必要的数据,所以我不相信问题出在那里。我现在正在设置修复图像,因为这是我第一次使用海关列表视图进行测试。所以我试图首先完成这项工作,然后我将执行其余的工作。NullPointerException CustomList ViewAdapter与基本适配器Android
`应该实现自定义listview--
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;
}
我的活动布局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>
和日志我getting--
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.access$900(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.access$900(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)
`
在你的getView方法中添加一个异常处理程序,以捕捉任何异常那里,把一个断点catch(Exception e)区域并查看是否发生异常。 – faljbour
错误的布局选择存在问题。 – Piyush
Piyush Gupta,你说得对,我已经修好了,但现在它没有呈现任何东西。 fljabour我设置了一个try catch,并用break int int catch(Exception)调试它,虽然它在运行时没有通过它 –