我的简单应用内部出错,我不知道为什么。我让那个应用程序运行一次,但现在它只是崩溃了。它没有显示任何语法错误,一切都编译没有问题。这是代码:
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
公共类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
公共类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)
答案 0 :(得分:0)
如果它在getView
中,则此布局中可能不存在其中一个元素(第46行的元素)。
答案 1 :(得分:0)
由于getView需要返回一个View,请尝试:
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;
}
试试这个希望它有帮助!!!