自定义抽屉适配器中 Inflater 上的 Nullpointer 异常
Posted
技术标签:
【中文标题】自定义抽屉适配器中 Inflater 上的 Nullpointer 异常【英文标题】:Nullpointer exception on Inflater in Custom Drawer Adapter 【发布时间】:2015-09-20 15:07:47 【问题描述】:我正在尝试为我的导航抽屉制作一个自定义适配器,并在布局充气器上不断收到空指针异常。我已尝试按照其他帖子中的建议在 getView
方法内部和外部声明 LayoutInflater
变量,但无济于事。
下面是我的自定义适配器类
Context mContext;
private ArrayList<String> mValues = new ArrayList<String>();
LayoutInflater inflater;
public DrawerListAdapter(Context context, ArrayList<String> mValues)
super(context, R.layout.drawer_layout, mValues);
this.mContext = mContext;
this.mValues = mValues;
inflater = LayoutInflater.from(mContext);
@Override
public View getView(int position, View convertView, ViewGroup parent)
View rowView = convertView;
if(rowView == null)
rowView = inflater.inflate(R.layout.drawer_layout, parent, false);
else
Log.d("DEBUG", "rowView is not null");
TextView textView = (TextView) rowView.findViewById(R.id.drawer_text);
switch (position)
case 0:
textView.setBackgroundColor(mContext.getResources().getColor(R.color.materialRed));
textView.setText(mValues.get(position));
break;
case 1:
textView.setBackgroundColor(mContext.getResources().getColor(R.color.materialLightBlue));
textView.setText(mValues.get(position));
break;
case 2:
textView.setBackgroundColor(mContext.getResources().getColor(R.color.materialLightGreen));
textView.setText(mValues.get(position));
break;
case 3:
textView.setBackgroundColor(mContext.getResources().getColor(R.color.materialLime));
textView.setText(mValues.get(position));
break;
return rowView;
这是我在 onCreate()
方法中的 MainActivity 中调用它的地方
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
mDrawerTitles.add("Politics");
mDrawerTitles.add("Technology");
mDrawerTitles.add("Sports");
mDrawerTitles.add("Books");
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(android.R.id.list);
DrawerListAdapter drawerAdapter = new DrawerListAdapter(this, mDrawerTitles);
mDrawerList.setAdapter(drawerAdapter);
//mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
new HttpJSONLoader().execute();
还有我的堆栈跟踪
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:219)
at net.rmoreno.lemonlime.DrawerListAdapter.<init>(DrawerListAdapter.java:26)
at net.rmoreno.lemonlime.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
【问题讨论】:
【参考方案1】:这行this.mContext = mContext;
应该是 DrawerListAdapter 构造函数中的mContext = context;
。
【讨论】:
尝试developer.android.com/tools/debugging/debugging-studio.html 看看哪个变量是空的。【参考方案2】:改一下
@Override
public View getView(int position, View convertView, ViewGroup parent)
Context = parent.getContext()
/** then check convertView & inflate view & u can use placeHolder with setTag()*/
rootView = LayoutInflater.from(context).inflate(R.layout....,parent,false);
....
return rootView;
这里有如何管理适配器和 AbsView 的示例
Example of ListAdapter AbsView & PlaceHolder pattern
【讨论】:
以上是关于自定义抽屉适配器中 Inflater 上的 Nullpointer 异常的主要内容,如果未能解决你的问题,请参考以下文章
android中listvliew中的自定义字体Text-View