java.lang.IllegalStateException:尚未创建的内容视图

时间:2013-05-10 08:30:47

标签: android-listfragment illegalstateexception

mMatchAdapter ”在onCreate初始化,但在“ setCursor ”中为空,为什么? 这个ListFragment是tabHost的一个标签,我用它来显示列表。我想在setCursor中更改适配器以显示其他列表,但在行mMatchAdapter.changeCursor(c);处发生错误 因为mMatchAdapter为空,为什么?当getListView在这一行之上时,将

public class RecentsListFragment extends ListFragment implements onSetRecentsContactsCursor {
    public final static String TAG = "RecentsListFragment";
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    Log.d(TAG, "---------> onViewCreated ");
    mListView = getListView();
    Log.d(TAG, "onViewCreated------mListView = " + mListView);
    mViewCreated = true;
    setListShown(false);
    super.onViewCreated(view, savedInstanceState);
}

@Override
public void onDestroyView() {
    // TODO Auto-generated method stub
    Log.d(TAG, "---------> onDestroyView ");
    mViewCreated = false;
    super.onDestroyView();
}


@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
    // TODO Auto-generated method stub
    Cursor cursor;
    ListAdapter adapter = getListAdapter();
    if (adapter == mLogsAdapter){
        cursor = (Cursor)mLogsAdapter.getItem(pos);
    }else if (adapter == mMatchAdapter){
        cursor = (Cursor)mMatchAdapter.getItem(pos);
    }
    super.onListItemClick(l, v, pos, id);
}


@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    Log.d(TAG, "------>onAttach");
    mActivity = activity;
    super.onAttach(activity);
}


@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    Log.d(TAG, "---------> onCreate ");

    mNumberQuery = NumberLocalQuery.getInstance(mActivity.getApplicationContext());
    mQueryHandler = new CursorQueryHandler(mActivity.getContentResolver());
    mPhotoLoader = new ContactPhotoLoader(mActivity);
    mLogsAdapter = new BBKRecentCallsAdapter(mActivity, mPhotoLoader, mNumberQuery);

    mMatchAdapter = new BBKDialerMatchListAdapter(mActivity, mPhotoLoader, mNumberQuery);
    super.onCreate(savedInstanceState);
}

@Override
public void onStart() {
    // TODO Auto-generated method stub
    mCallsChanged = true;
    Log.d(TAG, "---------> onStart ");
    setListAdapter(mLogsAdapter);
    mLogsAdapter.clearCache();
    startQuery();
    super.onStart();
}


@Override
public void onStop() {
    // TODO Auto-generated method stub
    mCallsChanged = false;
    Log.d(TAG, "---------> onStop ");

    if (null != mQueryHandler){
        mQueryHandler.cancelOperation(CUROSR_QUERY_TOKEN_CALLS);
    }
    super.onStop();
}


@Override
public void onResume() {
    // TODO Auto-generated method stub
    Log.d(TAG, "---------> onResume ");
    boolean is24Hour = DateFormat.is24HourFormat(mActivity);
    if (null != mLogsAdapter){
        mLogsAdapter.set24Hour(is24Hour);
    }
    if (null != mMatchAdapter){
        mMatchAdapter.set24Hour(is24Hour);
    }
    mIsMatching = false;
    super.onResume();
}


private void startQuery() {
    Log.d(TAG, "---------> startQuery ");
    if(mCallsChanged) {
        Log.d(TAG, "start query call_log database");
        mCallsChanged = false;
        mQueryHandler.cancelOperation(CUROSR_QUERY_TOKEN_CALLS);

        mQueryHandler.startQuery(CUROSR_QUERY_TOKEN_CALLS, null,
                Calls.CONTENT_URI, RecentCallsListActivity.CALL_LOG_PROJECTION, null,
                null, Calls.DEFAULT_SORT_ORDER);
    }else if(mLogsAdapter != null && (mLogsAdapter.getCursor() != null)) {
        mLogsAdapter.notifyDataSetChanged();
    }
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    Log.d(TAG, "---------> onActivityCreated ");
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    Log.d(TAG, "---------> onDestroy ");
    if (null != mLogsAdapter){
        mLogsAdapter.clearCache();
        mLogsAdapter.changeCursor(null);
    }
    if (null != mMatchAdapter){
        mMatchAdapter.changeCursor(null);
    }
    super.onDestroy();
}

@Override
public void onDetach() {
    // TODO Auto-generated method stub
    Log.d(TAG, "---------> onDetach ");
    super.onDetach();
}

@Override
public void setCursor(Cursor c) {
    // TODO Auto-generated method stub
    Log.d(TAG, "====setCursor======mViewCreated====" + mViewCreated);
    Log.d(TAG, "--------->mListView = " + mListView);
    if (null != c && c.getCount() > 0){
        setListAdapter(mMatchAdapter);
    }
    mMatchAdapter.changeCursor(c);
}
}

0 个答案:

没有答案