本文介绍了在自定义抽屉适配器上吹气空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让我的抽屉式导航栏自定义适配器,并不断收到对布局吹气一个空指针异常。我试过声明内外 getView
法外 LayoutInflater
变量作为其他职位的建议,但都无济于事。
I'm trying to make a custom adapter for my navigation drawer, and keep getting a nullpointer exception on the layout inflater. I've tried declaring the LayoutInflater
variable inside and outside the getView
method as suggested in other posts, but to no avail.
下面是我的自定义适配器类
Below is my Custom Adapter Class
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
Here is where I call it in my MainActivity within the onCreate()
method
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)
推荐答案
这行 this.mContext = mContext;
应 mContext =背景;
在DrawerListAdapter构造。
This line this.mContext = mContext;
should be mContext = context;
in DrawerListAdapter constructor.
这篇关于在自定义抽屉适配器上吹气空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!