我有一个任务是使用这个名为todo_item.xml的类的相对布局为对象扩展一个名为“ToDoItem”的类。我刚开始学习android编程,我无法弄清楚为什么我得到了跟随错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
我的代码看起来像这样,有更多类特定的函数,但我已经跳过它们以使问题代码更加可见。
public class ToDoListAdapter extends BaseAdapter {
private final List<ToDoItem> mItems = new ArrayList<ToDoItem>();
private final Context mContext;
LayoutInflater mInflater;
private static final String TAG = "Lab-UserInterface";
public ToDoListAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(context);
}// Create a View for the ToDoItem at specified position
// Remember to check whether convertView holds an already allocated View
// before created a new View.
// Consider using the ViewHolder pattern to make scrolling more efficient
// See: http://developer.android.com/training/improving-layouts/smooth- scrolling.html
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO - Get the current ToDoItem
final ToDoItem toDoItem = (ToDoItem) getItem(position);
// TODO - Inflate the View for this ToDoItem
// from todo_item.xml
convertView = mInflater.inflate(R.layout.todo_item,parent,false);
RelativeLayout itemLayout = (RelativeLayout) convertView.findViewById(R.id.RelativeLayout1);
// Fill in specific ToDoItem data
// Remember that the data that goes in this View
// corresponds to the user interface elements defined
// in the layout file
// TODO - Display Title in TextView
final TextView titleView = new TextView(null);
titleView.setText(toDoItem.getTitle());
答案 0 :(得分:0)
在你的getView中试试这个
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_item, parent, false);
无需mInflater = LayoutInflater.from(context)