我有这个CustomCursorAdapter从另一个Activity中获取光标。
public class PastJourneyListAdapter extends CursorAdapter {
private static final String TAG = "<PastJourneyListAdapter>";
private final LayoutInflater mInflater;
private Cursor cursor;
@SuppressWarnings("deprecation")
public PastJourneyListAdapter(Context context, Cursor c) {
super(context, c);
cursor = c;
mInflater = LayoutInflater.from(context);
Log.d(TAG, "6" + c.getColumnCount() + c.getCount() );
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(TAG, "1");
TextView journeyTitle = (TextView) view.findViewById(R.id.past_journey_title);
journeyTitle.setText(cursor.getString(cursor.getColumnIndex("name")));
TextView journeyPlace = (TextView) view.findViewById(R.id.past_journey_place);
journeyPlace.setText(cursor.getString(cursor.getColumnIndex("place")));
TextView journeyDate = (TextView) view.findViewById(R.id.past_journey_date);
journeyDate.setText(cursor.getString(cursor.getColumnIndex("date")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d(TAG, "4");
final View view = mInflater.inflate(R.layout.past_journey_list_item, parent, false);
return view;
}
}
PastJourneyList.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.past_journey_list);
JourneyDataSource jds = new JourneyDataSource(this);
jds.open();
Cursor c = jds.getAllJourneys();
ListView pastJourneyListView = (ListView) findViewById(R.id.addFriendsList);
pastJourneyListViewAdapter = new PastJourneyListAdapter(getBaseContext(), c);
pastJourneyListView.setAdapter(pastJourneyListViewAdapter);
}
不知怎的,我得到了一个 NullPointerException 。 此外,日志显示newView()和bindView都没有被调用!然而,构造函数被调用并打印完美的光标结果。
TIA
**错误记录**
12-06 06:51:47.445: D/<PastJourneyListAdapter>(2429): 671
12-06 06:51:47.455: D/AndroidRuntime(2429): Shutting down VM
12-06 06:51:47.455: W/dalvikvm(2429): threadid=1: thread exiting with uncaught exception (group=0x415d78b0)
12-06 06:37:38.158: E/AndroidRuntime(31796): FATAL EXCEPTION: main
12-06 06:37:38.158: E/AndroidRuntime(31796): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memories/com.example.memories.pastjourney.PastJourneyList}: java.lang.NullPointerException
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.app.ActivityThread.access$600(ActivityThread.java:150)
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.os.Looper.loop(Looper.java:213)
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.app.ActivityThread.main(ActivityThread.java:5225)
12-06 06:37:38.158: E/AndroidRuntime(31796): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 06:37:38.158: E/AndroidRuntime(31796): at java.lang.reflect.Method.invoke(Method.java:525)
12-06 06:37:38.158: E/AndroidRuntime(31796): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
12-06 06:37:38.158: E/AndroidRuntime(31796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-06 06:37:38.158: E/AndroidRuntime(31796): at dalvik.system.NativeStart.main(Native Method)
12-06 06:37:38.158: E/AndroidRuntime(31796): Caused by: java.lang.NullPointerException
12-06 06:37:38.158: E/AndroidRuntime(31796): at com.example.memories.pastjourney.PastJourneyList.onCreate(PastJourneyList.java:35)
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.app.Activity.performCreate(Activity.java:5133)
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-06 06:37:38.158: E/AndroidRuntime(31796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
12-06 06:37:38.158: E/AndroidRuntime(31796): ... 11 more