1
我有一個任務,使用相對佈局爲名爲todo_item.xml的這個類添加一個名爲「ToDoItem」的類的對象視圖。我剛開始學習android編程和我不知道爲什麼我得到以下錯誤。Android NullPointerException在調用虛擬方法時Context.getResource()
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());
你應該張貼從logcat的一個完整的堆棧跟蹤,所以我們可以看到這行有NullPointerException異常。 – Karakuri