我制作了自己的自定义适配器,膨胀了另一个布局文件,如果我点击一个项目,我会得到一个NullPointer异常。我不知道这是什么错,但我希望你能帮助我。这是我的适配器的代码:
public class EntryAdapter extends BaseAdapter {
private Context context;
private ArrayList<DrawerItem> mTitles;
public EntryAdapter(Context context, ArrayList<DrawerItem> mTitles) {
this.context = context;
this.mTitles = mTitles;
}
@Override
public int getCount() {
return mTitles.size();
}
@Override
public Object getItem(int position) {
return mTitles.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.text1);
txtTitle.setText(mTitles.get(position).getTitle());
txtTitle.setTextSize(26);
Typeface font = Typeface.createFromAsset(context.getAssets(),
"DancingScript- Regular.ttf");
txtTitle.setTypeface(font);
if (position == 3) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.seperator_item, null);
TextView txtTitle1 = (TextView) convertView
.findViewById(R.id.text2);
txtTitle1.setText(mTitles.get(position).getTitle());
txtTitle1.setTextSize(15);
txtTitle1.setGravity(Gravity.BOTTOM);
txtTitle1.setTypeface(font, Typeface.BOLD);
txtTitle1.setBackgroundColor(color.background_light);
} else {
txtTitle.setText(mTitles.get(position).getTitle());
}
return convertView;
}
@Override
public boolean isEnabled(int position) {
if (position == 3) {
return false;
} else {
return true;
}
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
}
提前致谢
编辑:
继承了drawer_list_item的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/list_selector">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="@android:color/black"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
</LinearLayout>`
LogCat:
01-20 13:37:50.165: E/AndroidRuntime(17299): FATAL EXCEPTION: main
01-20 13:37:50.165: E/AndroidRuntime(17299): Process: com.example.trink_spiele, PID: 17299
01-20 13:37:50.165: E/AndroidRuntime(17299): java.lang.NullPointerException
01-20 13:37:50.165: E/AndroidRuntime(17299): at com.example.trink_spiele.EntryAdapter.getView(EntryAdapter.java:52)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.AbsListView.obtainView(AbsListView.java:2240)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.ListView.makeAndAddView(ListView.java:1790)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.ListView.fillDown(ListView.java:691)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.ListView.fillSpecific(ListView.java:1349)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.ListView.layoutChildren(ListView.java:1608)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.AbsListView.onLayout(AbsListView.java:2091)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.View.layout(View.java:14817)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.View.layout(View.java:14817)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.View.layout(View.java:14817)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-20 13:37:50.165: E/AndroidRuntime(17299): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.View.layout(View.java:14817)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.View.layout(View.java:14817)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.ViewGroup.layout(ViewGroup.java:4631)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.Choreographer.doFrame(Choreographer.java:544)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.os.Handler.handleCallback(Handler.java:733)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.os.Handler.dispatchMessage(Handler.java:95)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.os.Looper.loop(Looper.java:136)
01-20 13:37:50.165: E/AndroidRuntime(17299): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-20 13:37:50.165: E/AndroidRuntime(17299): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 13:37:50.165: E/AndroidRuntime(17299): at java.lang.reflect.Method.invoke(Method.java:515)
01-20 13:37:50.165: E/AndroidRuntime(17299): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-20 13:37:50.165: E/AndroidRuntime(17299): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-20 13:37:50.165: E/AndroidRuntime(17299): at dalvik.system.NativeStart.main(Native Method)