所以我尝试使用自定义适配器重建我的ListView,这样我可以让它看起来更好一些,并为它添加更多功能。无论如何,这是我的适配器
HistoryAdapter.java
package com.this.app;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class HistoryAdapter extends BaseAdapter {
private Context mContext;
Cursor cursor;
public HistoryAdapter(Context context, Cursor cur){
super();
mContext = context;
cursor = cur;
}
public int getCount(){
// return the number of records in cursor
return cursor.getCount();
}
// getView method is called for each item of ListView
public View getView(int position, View view, ViewGroup parent){
// inflate the layout for each item of listView
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.history_list_item, null);
// move the cursor to required position
cursor.moveToPosition(position);
// fetch the information for each card
String pricePerGallon = cursor.getString(cursor.getColumnIndex("pricePerGallon"));
String gallons = cursor.getString(cursor.getColumnIndex("gallons"));
String odometer = cursor.getString(cursor.getColumnIndex("odometer"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String filledOrNot = cursor.getString(cursor.getColumnIndex("filledOrNot"));
String comments = cursor.getString(cursor.getColumnIndex("comments"));
//String milesPerGallon = cursor.getString(cursor.getColumnIndex("miledPerGallon"));
String totalSpent = cursor.getString(cursor.getColumnIndex("totalSpent"));
// get the reference of TextViews
TextView textViewPricePerGallon = (TextView) view.findViewById(R.id.pricePerGallon);
TextView textViewGallons = (TextView) view.findViewById(R.id.gallons);
TextView textViewOdometer = (TextView) view.findViewById(R.id.odometer);
TextView textViewDate = (TextView) view.findViewById(R.id.date);
TextView textViewFilledOrNot = (TextView) view.findViewById(R.id.cardFilledOrNot);
TextView textViewComments = (TextView) view.findViewById(R.id.comments);
//TextView textViewMilesPerGallon = (TextView) view.findViewById(R.id.mpg);
TextView textViewTotalSpent = (TextView) view.findViewById(R.id.totalSpentOnFillups);
// Set the data to each TextView
Log.d("HistoryAdapter", pricePerGallon);
textViewPricePerGallon.setText(pricePerGallon);
textViewGallons.setText(gallons);
textViewOdometer.setText(odometer);
textViewDate.setText(date);
textViewFilledOrNot.setText(filledOrNot);
textViewComments.setText(comments);
//textViewMilesPerGallon.setText(milesPerGallon);
textViewTotalSpent.setText(totalSpent);
return view;
}
public Object getItem(int position){
return position;
}
public long getItemId(int position){
return position;
}
}
我在第一个setText
处得到一个NullPointerExceptiontextViewPricePerGallon.setText(pricePerGallon);
现在,如果我注释掉所有的setText,它会很好地加载列表(它只是不附加所有数据......而是显示XML中设置的所有默认值)
这是调试的副本
02-21 11:21:06.156: E/AndroidRuntime(30769): FATAL EXCEPTION: main
02-21 11:21:06.156: E/AndroidRuntime(30769): Process: com.this.app, PID: 30769
02-21 11:21:06.156: E/AndroidRuntime(30769): java.lang.NullPointerException
02-21 11:21:06.156: E/AndroidRuntime(30769): at com.this.app.HistoryAdapter.getView(HistoryAdapter.java:60)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.ListView.makeAndAddView(ListView.java:1790)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.ListView.fillDown(ListView.java:691)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.ListView.fillFromTop(ListView.java:752)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.ListView.layoutChildren(ListView.java:1630)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.AbsListView.onLayout(AbsListView.java:2091)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.View.layout(View.java:14825)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.ViewGroup.layout(ViewGroup.java:4641)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2016)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1773)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1022)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5708)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.Choreographer.doFrame(Choreographer.java:532)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.os.Handler.handleCallback(Handler.java:733)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.os.Handler.dispatchMessage(Handler.java:95)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.os.Looper.loop(Looper.java:137)
02-21 11:21:06.156: E/AndroidRuntime(30769): at android.app.ActivityThread.main(ActivityThread.java:5083)
02-21 11:21:06.156: E/AndroidRuntime(30769): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 11:21:06.156: E/AndroidRuntime(30769): at java.lang.reflect.Method.invoke(Method.java:515)
02-21 11:21:06.156: E/AndroidRuntime(30769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-21 11:21:06.156: E/AndroidRuntime(30769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-21 11:21:06.156: E/AndroidRuntime(30769): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
看起来是通话:view.findViewById(R.id.pricePerGallon)
正在返回null
。您确定pricePerGallon
在项目中应该存在吗?
答案 1 :(得分:0)
确保您拥有ID
XML中的R.id.pricePerGallon
layout.history_list_item
如果您说该字符串不为null,则会离开TextView。