当我尝试显示数据库中的所有信息时,我收到此异常。
02-27 01:49:49.074: E/AndroidRuntime(1956): FATAL EXCEPTION: main
02-27 01:49:49.074: E/AndroidRuntime(1956): java.lang.NullPointerException
02-27 01:49:49.074: E/AndroidRuntime(1956): at my.fyp.inticlassifieds.CustomAdapter.getView(CustomAdapter.java:86)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.widget.ListView.onMeasure(ListView.java:1159)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-27 01:49:49.074: E/AndroidRuntime(1956): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-27 01:49:49.074: E/AndroidRuntime(1956): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.Choreographer.doFrame(Choreographer.java:532)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.os.Handler.handleCallback(Handler.java:730)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.os.Looper.loop(Looper.java:137)
02-27 01:49:49.074: E/AndroidRuntime(1956): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-27 01:49:49.074: E/AndroidRuntime(1956): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 01:49:49.074: E/AndroidRuntime(1956): at java.lang.reflect.Method.invoke(Method.java:525)
02-27 01:49:49.074: E/AndroidRuntime(1956): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-27 01:49:49.074: E/AndroidRuntime(1956): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-27 01:49:49.074: E/AndroidRuntime(1956): at dalvik.system.NativeStart.main(Native Method)
这是我的自定义适配器类:
public class CustomAdapter extends BaseAdapter {
private ArrayList<Products> m_productsList = null;
private LayoutInflater mInflater = null;
private class Row{
TextView mTextViewPID;
TextView mTextViewName;
TextView mTextViewPrice;
}
public CustomAdapter(Context context, ArrayList<Products> productsList){
this.mInflater = LayoutInflater.from(context);
this.m_productsList = productsList;
}
// --------------------------------------------------
// BaseAdapter Overrides
// --------------------------------------------------
@Override
public int getCount() {
int count = 0;
if((m_productsList !=null) && (m_productsList.size() >= 1)){
count = m_productsList.size();
}
return count;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return m_productsList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Row theRow;
// If ConvertVIew is null
if(convertView == null){
theRow = new Row();
convertView = mInflater.inflate(R.layout.activity_display_requested, null);
theRow.mTextViewPID = (TextView) convertView.findViewById(R.id.pid);
theRow.mTextViewName = (TextView) convertView.findViewById(R.id.name);
theRow.mTextViewPrice = (TextView) convertView.findViewById(R.id.budget);
convertView.setTag(theRow);
}else{
theRow = (Row) convertView.getTag();
}
theRow.mTextViewPID.setText(m_productsList.get(position).pid);
theRow.mTextViewName.setText(m_productsList.get(position).name);
theRow.mTextViewPrice.setText(m_productsList.get(position).price);
return convertView;
}
}
抛出此异常的行是:
theRow.mTextViewPID.setText(m_productsList.get(position).pid);
activity_display_requested xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
我已经陷入这个问题大约6个小时了。
答案 0 :(得分:3)
activity_display_requested.xml
中没有TextView。
你可能正在夸大错误的布局。你正在膨胀activity_display_requested.xml
。
convertView = mInflater.inflate(R.layout.activity_display_requested, null);
findViewById
在当前膨胀的布局中查找视图。如果你有带textview的布局,那么膨胀那个布局并初始化视图
答案 1 :(得分:0)
使用此
if((m_productsList !=null)){
count = m_productsList.size();
} 在getCount()方法中,它可以工作。