1

我似乎无法追踪这个错误的罪魁祸首。这是我的整个活动:

public class CalendarActivity extends Activity implements OnClickListener {

private GestureDetector mGestureDetector;
private ProgressDialog mProgressDialog;
private int mGridWidth, mGridHeight;
private MonthlyCalendarWidget mMonthlyCalendar;
private Button mNext;
private Button mPrev;
private CalendarGridAdapter mDayGridAdapter;
private CalendarEventsListAdapter mEventsListAdapter;
private CalendarAssignmentsListAdapter mAssignmentsListAdapter;
private Calendar mCalendarSource;
private String mMonthId;
private int mMonth, mYear;
private CalendarDbAdapter mCalendarDb;
private CalendarSQLiteAdapter mCalendarSQL;
private CalendarEventData mCalendarEvent;
private ArrayList<CalendarEventData> mEventsList;
private CalendarAssignmentData mCalendarAssignment;
private ArrayList<CalendarAssignmentData> mAssignmentsList;

public ArrayList<CalendarEventData> getCalendarEventData() throws Exception {
    ArrayList<CalendarEventData> eventsList = CalendarDbAdapter
            .getCalendarEvent("2011", 9, 36, "ParentAndroidSerpong");
    return eventsList;

}

public void cacheCalendarEventData(ArrayList<CalendarEventData> data) {
    for (int i = 0; i < data.size(); i++) {
        mCalendarSQL.addEvents(data.get(i));
    }
}

private SimpleCursorAdapter fillEventData() {

    Cursor c = mCalendarSQL.fetchAllEvents();
    startManagingCursor(c);

    String[] from = new String[] { CalendarSQLiteAdapter.KEY_EVENTS_NAME, CalendarSQLiteAdapter.KEY_EVENTS_START_DATE };
    int[] to = new int[] { R.id.monthlylistchip_texttop, R.id.monthlylistchip_textbottom };

    return new SimpleCursorAdapter(this, R.layout.monthly_listchip, c, from, to);

}

public ArrayList<CalendarAssignmentData> getCalendarAssignmentData()
        throws Exception {
    ArrayList<CalendarAssignmentData> assignmentList = CalendarDbAdapter
            .getCalendarAssignment("0970001931", "2011", "4", 36,
                    "ParentAndroidSerpong");
    return assignmentList;

}

public void cacheCalendarAssignmentData(
        ArrayList<CalendarAssignmentData> data) {
    for (int i = 0; i < data.size(); i++) {
        mCalendarSQL.addAssignments(data.get(i));
    }
}

private void updateView(int month, int year) {
    mDayGridAdapter = new CalendarGridAdapter(this,
            R.id.monthly_gridchip_date, mGridWidth, mGridHeight, month,
            year);

    mEventsListAdapter = new CalendarEventsListAdapter(this,
            R.layout.monthly_listchip, mEventsList);

    mAssignmentsListAdapter = new CalendarAssignmentsListAdapter(this,
            R.layout.monthly_listchip, mAssignmentsList);

    mCalendarSource.set(year, month - 1,
            mCalendarSource.get(Calendar.DAY_OF_MONTH));

    mMonthId = (String) DateUtils.getFormattedDateString(
            mCalendarSource.getTime(), DateUtils.MONTH_AND_YEAR);

    mMonthlyCalendar.instantiate(this, mMonthId, mDayGridAdapter,
            fillEventData(), mAssignmentsListAdapter);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    mGridHeight = ScreenUtils.getDisplayHeight(this) / 10;
    mGridWidth = ScreenUtils.getDisplayWidth(this) / 7;

    mNext = (Button) findViewById(R.id.monthlyview_header_monthnext);
    mNext.setOnClickListener(this);
    mPrev = (Button) findViewById(R.id.monthlyview_header_monthprev);
    mPrev.setOnClickListener(this);

    mCalendarSQL = new CalendarSQLiteAdapter(this);
    mCalendarSQL.open();

    try {
        cacheCalendarEventData(getCalendarEventData());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    mCalendarSource = Calendar.getInstance(Locale.getDefault());
    mMonth = mCalendarSource.get(Calendar.MONTH) + 1;
    mYear = mCalendarSource.get(Calendar.YEAR);

    updateView(mMonth, mYear);

}

@Override
public void onClick(View v) {
    if (v == mPrev) {
        if (mMonth <= 1) {
            mMonth = 12;
            mYear--;
        } else {
            mMonth--;
        }
        updateView(mMonth, mYear);
    } else if (v == mNext) {
        if (mMonth > 11) {
            mMonth = 1;
            mYear++;
        } else {
            mMonth++;
        }
        updateView(mMonth, mYear);
    }

}

}

这是错误的 LogCat 条目:

07-03 09:58:29.112: E/AndroidRuntime(5185): FATAL EXCEPTION: main
07-03 09:58:29.112: E/AndroidRuntime(5185): java.lang.RuntimeException: Unable to start activity ComponentInfo{stub.binusitdirectorate.calendar/stub.binusitdirectorate.calendar.controller.CalendarActivity}: java.lang.NullPointerException
07-03 09:58:29.112: E/AndroidRuntime(5185):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at android.os.Looper.loop(Looper.java:123)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at java.lang.reflect.Method.invokeNative(Native Method)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at java.lang.reflect.Method.invoke(Method.java:521)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at dalvik.system.NativeStart.main(Native Method)
07-03 09:58:29.112: E/AndroidRuntime(5185): Caused by: java.lang.NullPointerException
07-03 09:58:29.112: E/AndroidRuntime(5185):     at stub.binusitdirectorate.calendar.controller.CalendarActivity.updateView(CalendarActivity.java:117)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at stub.binusitdirectorate.calendar.controller.CalendarActivity.onCreate(CalendarActivity.java:151)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-03 09:58:29.112: E/AndroidRuntime(5185):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-03 09:58:29.112: E/AndroidRuntime(5185):     ... 11 more

我所知道的是,当我调用它时代码会中断:

mMonthlyCalendar.instantiate(this, mMonthId, mDayGridAdapter,
            mEventsListAdapter, mAssignmentsListAdapter);

该方法应该将适配器绑定到我的自定义视图。

提供了所有必需的变量,那么为什么系统给了我一个NullPointerException

4

0 回答 0