当前位置: 代码迷 >> Android >> LayoutInflater.from(context) 空指针异常
  详细解决方案

LayoutInflater.from(context) 空指针异常

热度:130   发布时间:2016-05-01 17:43:20.0
LayoutInflater.from(context) 空指针错误
public class MyListAdapter extends BaseAdapter {
private int img[] = {R.drawable.ic_media,R.drawable.ic_playlist,R.drawable.right_arrows};
private Context context;
private LayoutInflater inflater;

public MyListAdapter(Context context){
context = this.context;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return img.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView local_music_img = null;
TextView local_music_txt = null;
ImageView local_music_rignt_img = null;
Log.i("aaa", convertView+"");
if(convertView == null){
Log.i("aa", "aaaaaaaaaaaa");
convertView = inflater.inflate(R.layout.local_music_item, null);
local_music_img = (ImageView)convertView.findViewById(R.id.local_music_img);
local_music_txt = (TextView)convertView.findViewById(R.id.local_music_txt);
local_music_rignt_img = (ImageView)convertView.findViewById(R.id.local_music_rignt_img);
}

Log.i("aaa", convertView+"");


if(position == 0){
local_music_img.setImageResource(img[0]);
local_music_txt.setText(R.string.title_local_media);
}else {
local_music_img.setImageResource(img[1]);
local_music_txt.setText(R.string.local_music_list);
}

local_music_rignt_img.setImageResource(img[2]);

return convertView;
}

}



错误信息:07-29 10:11:40.009: ERROR/AndroidRuntime(643): Caused by: java.lang.NullPointerException
07-29 10:11:40.009: ERROR/AndroidRuntime(643): at android.view.LayoutInflater.from(LayoutInflater.java:171)
07-29 10:11:40.009: ERROR/AndroidRuntime(643): at com.liumeng.adapter.MyListAdapter.<init>(MyListAdapter.java:23)
07-29 10:11:40.009: ERROR/AndroidRuntime(643): at com.liumeng.ui.LocalMusicActivity.onCreate(LocalMusicActivity.java:18)


------解决方案--------------------
context = this.context;
inflater = LayoutInflater.from(context);
this.context为null,
应该是this.context = context;
  相关解决方案