我在 BaseAdapter 中遇到错误
I'm having an error in BaseAdapter
我的简单应用程序出现错误,我不知道为什么。我曾获得过该应用程序 运行,但现在它崩溃了。它没有显示任何语法错误,并且所有内容都可以毫无问题地编译。这是代码:
Main.java
public class Main extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_example);
final List<CustomListItem> items = new ArrayList<>(3);
items.add(new CustomListItem (1, "Item 1", getResources().getDrawable(R.drawable.ic_launcher)));
items.add(new CustomListItem (2, "Item 2", getResources().getDrawable(R.drawable.ic_launcher)));
items.add(new CustomListItem (3, "Item 3", getResources().getDrawable(R.drawable.ic_launcher)));
ListAdapter adapter = new MyListItemAdapter(items, this);
ListView lv = getListView();
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final CustomListItem item = items.get(position);
final int menuIndex = item.getIndex();
switch (menuIndex) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
});
}
MyListItemAdapter.java
public class MyListItemAdapter 扩展了 BaseAdapter {
private List<CustomListItem> items;
private Context context;
private int numItems = 0;
public MyListItemAdapter(final List<CustomListItem> items, Context context) {
this.items = items;
this.context = context;
this.numItems = items.size();
}
public int getCount() {
return numItems;
}
public CustomListItem getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
final CustomListItem item = items.get(position);
final RelativeLayout itemLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
ImageView imgIcon = (ImageView) itemLayout.findViewById(R.id.image);
imgIcon.setImageDrawable(item.getPicture());
TextView txtLabel = (TextView) itemLayout.findViewById(R.id.txtLabel);
txtLabel.setText(item.getLabel());
return itemLayout;
}
CustomListItem.java
public class CustomListItem {
private int mIndex;
private String mLabel;
private Drawable mPicture;
public CustomListItem(final int index, final String label, final Drawable pic) {
mIndex = index;
mLabel = label;
mPicture = pic;
}
public int getIndex() {
return mIndex;
}
public String getLabel() {
return mLabel;
}
public Drawable getPicture() {
return mPicture;
}
}
logcat
14806-14806/com.robigroza.baseadaptertest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException at com.robigroza.baseadaptertest.MyListItemAdapter.getView(MyListItemAdapter.java:46)
at android.widget.AbsListView.obtainView(AbsListView.java:2465)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
at android.widget.ListView.onMeasure(ListView.java:1162)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)
at android.view.View.measure(View.java:15371)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2011)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1250)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1425)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4674)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
如果它在 getView
中,可能其中一个元素(第 46 行的那个)在此布局中不存在。
因为 getView 需要 return 一个视图,试试这个:
public View getView(int position, View convertView, ViewGroup parent) {
View rootView = convertView;
if(rootView == null){
LayoutInflater inflater = (LayoutInflater)LayoutInflater.from(context);
rootView = inflater.inflate(R.layout.list_item, parent, false);
final CustomListItem item = items.get(position);
}
if(item != null){
ImageView imgIcon = (ImageView) rootView.findViewById(R.id.image);
imgIcon.setImageDrawable(item.getPicture());
TextView txtLabel = (TextView) rootView.findViewById(R.id.txtLabel);
txtLabel.setText(item.getLabel());
}
return rootView;
}
试试这个希望对你有帮助!!!
我的简单应用程序出现错误,我不知道为什么。我曾获得过该应用程序 运行,但现在它崩溃了。它没有显示任何语法错误,并且所有内容都可以毫无问题地编译。这是代码:
Main.java
public class Main extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_example);
final List<CustomListItem> items = new ArrayList<>(3);
items.add(new CustomListItem (1, "Item 1", getResources().getDrawable(R.drawable.ic_launcher)));
items.add(new CustomListItem (2, "Item 2", getResources().getDrawable(R.drawable.ic_launcher)));
items.add(new CustomListItem (3, "Item 3", getResources().getDrawable(R.drawable.ic_launcher)));
ListAdapter adapter = new MyListItemAdapter(items, this);
ListView lv = getListView();
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final CustomListItem item = items.get(position);
final int menuIndex = item.getIndex();
switch (menuIndex) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
});
}
MyListItemAdapter.java
public class MyListItemAdapter 扩展了 BaseAdapter {
private List<CustomListItem> items;
private Context context;
private int numItems = 0;
public MyListItemAdapter(final List<CustomListItem> items, Context context) {
this.items = items;
this.context = context;
this.numItems = items.size();
}
public int getCount() {
return numItems;
}
public CustomListItem getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
final CustomListItem item = items.get(position);
final RelativeLayout itemLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
ImageView imgIcon = (ImageView) itemLayout.findViewById(R.id.image);
imgIcon.setImageDrawable(item.getPicture());
TextView txtLabel = (TextView) itemLayout.findViewById(R.id.txtLabel);
txtLabel.setText(item.getLabel());
return itemLayout;
}
CustomListItem.java
public class CustomListItem {
private int mIndex;
private String mLabel;
private Drawable mPicture;
public CustomListItem(final int index, final String label, final Drawable pic) {
mIndex = index;
mLabel = label;
mPicture = pic;
}
public int getIndex() {
return mIndex;
}
public String getLabel() {
return mLabel;
}
public Drawable getPicture() {
return mPicture;
}
}
logcat
14806-14806/com.robigroza.baseadaptertest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException at com.robigroza.baseadaptertest.MyListItemAdapter.getView(MyListItemAdapter.java:46)
at android.widget.AbsListView.obtainView(AbsListView.java:2465)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
at android.widget.ListView.onMeasure(ListView.java:1162)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15371)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)
at android.view.View.measure(View.java:15371)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2011)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1250)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1425)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4674)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
如果它在 getView
中,可能其中一个元素(第 46 行的那个)在此布局中不存在。
因为 getView 需要 return 一个视图,试试这个:
public View getView(int position, View convertView, ViewGroup parent) {
View rootView = convertView;
if(rootView == null){
LayoutInflater inflater = (LayoutInflater)LayoutInflater.from(context);
rootView = inflater.inflate(R.layout.list_item, parent, false);
final CustomListItem item = items.get(position);
}
if(item != null){
ImageView imgIcon = (ImageView) rootView.findViewById(R.id.image);
imgIcon.setImageDrawable(item.getPicture());
TextView txtLabel = (TextView) rootView.findViewById(R.id.txtLabel);
txtLabel.setText(item.getLabel());
}
return rootView;
}
试试这个希望对你有帮助!!!