在创建自定义listView时,我在给我的xml充气时遇到空指针错误。我知道它可能非常简单,我只是忽略它,但我无法弄明白。
我在if语句后面的第一行得到错误:convertView = inflater.inflate();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Deck Deck = this.getItem(position);
TextView title;
Button study;
Button edit;
Button add;
Button delete;
//if theres no view yet created on the screen, create one
if (convertView == null) {
convertView = inflater.inflate(R.layout.flash_card_row, null);
//give the views their respective ID's
title = (TextView)convertView.findViewById(R.id.flash_card_title);
study = (Button)convertView.findViewById(R.id.flash_card_study);
edit = (Button)convertView.findViewById(R.id.flash_card_edit);
add = (Button)convertView.findViewById(R.id.flash_card_add_to_deck);
delete = (Button)convertView.findViewById(R.id.flash_card_delete_from_deck);
convertView.setTag(new flashCardViewHolder(title, study, edit, add, delete));
}
//this gets called when the user scrolls down or up the screen and the adapter
//wants to maximize efficiency by just calling the viewHolder instead of findViewById
else {
flashCardViewHolder viewHolder = (flashCardViewHolder)convertView.getTag();
title = viewHolder.getTitle();
study = viewHolder.getStudy();
edit = viewHolder.getEdit();
add = viewHolder.getAdd();
delete = viewHolder.getDelete();
}
//working on it, currently null
//title.setText(Statics.listOfDecks.get(position).getTitle());
//down here is where you set the values for all your views/objects
//such as Buttons or textViews.
study.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
edit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return convertView;
}
}
这是我正在膨胀的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/flash_card_study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
<Button
android:id="@+id/flash_card_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/flash_card_study"
/>
<Button
android:id="@+id/flash_card_add_to_deck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/flash_card_edit"
/>
<Button
android:id="@+id/flash_card_delete_from_deck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/flash_card_add_to_deck"
/>
<TextView
android:id="@+id/flash_card_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/flash_card_delete_from_deck"
android:layout_alignBottom="@+id/flash_card_delete_from_deck"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
编辑:logcat报告
09-17 21:25:18.709: E/AndroidRuntime(1584): FATAL EXCEPTION: main
09-17 21:25:18.709: E/AndroidRuntime(1584): java.lang.NullPointerException
09-17 21:25:18.709: E/AndroidRuntime(1584): at com.example.studyOptions.FlashCardListViewAdapter.getView(FlashCardListViewAdapter.java:115)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.AbsListView.obtainView(AbsListView.java:2267)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.ListView.makeAndAddView(ListView.java:1769)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.ListView.fillDown(ListView.java:672)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.ListView.fillFromTop(ListView.java:733)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.ListView.layoutChildren(ListView.java:1622)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.AbsListView.onLayout(AbsListView.java:2102)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.View.layout(View.java:13754)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.View.layout(View.java:13754)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.View.layout(View.java:13754)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.View.layout(View.java:13754)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.View.layout(View.java:13754)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.Choreographer.doFrame(Choreographer.java:525)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.os.Handler.handleCallback(Handler.java:615)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.os.Handler.dispatchMessage(Handler.java:92)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.os.Looper.loop(Looper.java:137)
09-17 21:25:18.709: E/AndroidRuntime(1584): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-17 21:25:18.709: E/AndroidRuntime(1584): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 21:25:18.709: E/AndroidRuntime(1584): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 21:25:18.709: E/AndroidRuntime(1584): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-17 21:25:18.709: E/AndroidRuntime(1584): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-17 21:25:18.709: E/AndroidRuntime(1584): at dalvik.system.NativeStart.main(Native Method)