Android片段“内容视图尚未创建”

时间:2015-09-11 05:34:42

标签: android android-listfragment

一切都运行良好。但经过我的应用程序的一些测试后,它崩溃了,在我的Logcat中,我看到了“内容视图尚未创建”。我正在使用带有ListFragment的BaseAdapter。 我的ListFragment代码是:

public class TaskFragment extends ListFragment {

// Adapter class for driver's task list.
private TaskListAdapter mTaskListAdapter;

// all driver trip list setter getter
private ArrayList<TaskModel> mTaskModelArrayList;

// Creating flag for view is created or not.
private boolean hasViewCreated;

private boolean mHasNewTask;

public static TaskFragment newInstance(){
    return new TaskFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Fix for styling list fragment :- Starts
    View layout = super.onCreateView(inflater, container,
            savedInstanceState);

    assert layout != null;

    ListView lv = (ListView) layout.findViewById(android.R.id.list);
    ViewGroup parent = (ViewGroup) lv.getParent();

    // Remove ListView and add CustomView in its place
    int lvIndex = parent.indexOfChild(lv);
    parent.removeViewAt(lvIndex);
    RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(
            R.layout.fragment_task, container, false);
    parent.addView(mRelativeLayout, lvIndex, lv.getLayoutParams());

    return layout;
}

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

    // Fix for styling list fragment ends
    setHasOptionsMenu(true);
    hasViewCreated = true;
}

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

    mTaskModelArrayList = new ArrayList<>();
    mTaskListAdapter = new TaskListAdapter(getActivity(), mTaskModelArrayList, this);

    setListAdapter(mTaskListAdapter);
    setListShown(false);
}

@Override
public void onResume() {
    super.onResume();
    loadTasks(false, false);
}

private void loadTasks(boolean showError, boolean startButtonFlag) {

    // Load task from SQLite
    loadTaskFromSQLiteDb();

    // Check Online for new data
    loadTaskFromServer(showError, startButtonFlag);
}

/**
 * Fetching driver's task from mobile.
 */
private void loadTaskFromSQLiteDb() {
    try {
        if (mTaskModelArrayList.size() == mDBHelper.getActiveTaskCount())
            return;

        mTaskModelArrayList.clear();

        if (mDBHelper.getActiveTaskCount() > 0) {
            ArrayList<TaskModel> tasks = mDBHelper.getAllTaskView();


            for (TaskModel task : tasks) {
                mTaskModelArrayList.add(task);
            }

            setListShown(true);
        }

        refreshTaskList(); // notify data change
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Fetching driver's task list from server if showError value true.
 * @param showError
 *      passing boolean value 'true'.
 */
private void loadTaskFromServer(final boolean showError, final boolean startButtonFlag) {

    // if showError value true then show progress bar.
    if (showError)
        showProgress();

    JSONObject params = new JSONObject();
    JSONObject appBranchId = new JSONObject();

    try {

        appBranchId.put("branchId", getString(R.string.app_client_id));

        params.put("myParam", appBranchId);
        params.put("mobileImei", ApplicationController.getInstance()
                .getDeviceInfo().get(ApplicationController.DEVICE_IMEI));

    } catch (JSONException e) {
        e.printStackTrace();
    }

    new VolleyHelper().doJsonObjectRequest(getActivity(),
            VolleyHelper.POST, UrlConstants.DRIVER_TASK_LIST, params,
            new VolleyResponseCallBack() {

                @Override
                public void onResponse(JSONObject response) {
                    super.onResponse(response);

                    try {
                        ApplicationController.getInstance().getRequestQueue().getCache().clear();
                        endProgress();
                        if (response.has("data")) {
                            switch (response.getJSONObject("data").getString("status")) {
                                case "success":
                                    JSONArray employeeDetailData = response.getJSONObject("data")
                                            .getJSONArray("empDetails");
                                    for (int i = 0; i < employeeDetailData.length(); i++) {
                                        TaskModel taskModel = new TaskModel();
                                        JSONObject employeeDetailJsonObject = employeeDetailData
                                                .getJSONObject(i);

                                        if (!mDBHelper.isTaskExist(employeeDetailJsonObject
                                                .getString("requestId"))) {
                                            taskModel.setmTaskEmployeeRequestId(employeeDetailJsonObject
                                                    .getString("requestId"));
                                            taskModel.setmTaskEmployeeId(employeeDetailJsonObject
                                                    .getString("employeeId"));
                                            taskModel.setmTaskEmployeeName(employeeDetailJsonObject
                                                    .getString("name"));
                                            taskModel.setmTaskEmployeeAddress(employeeDetailJsonObject
                                                    .getString("address"));
                                            taskModel.setmTaskEmployeeShiftTime(employeeDetailJsonObject
                                                    .getString("tripTime"));

                                            if (response.getJSONObject("data")
                                                    .getString("tripType").equalsIgnoreCase("PICKUP")) {
                                                taskModel.setmTaskEmployeePickupTime(employeeDetailJsonObject
                                                        .getString("pickUpTime"));
                                                taskModel.setmTaskDriverArrivedFlag(employeeDetailJsonObject
                                                        .getString("reachedFlg"));
                                            } else if (response.getJSONObject("data")
                                                    .getString("tripType").equalsIgnoreCase("DROP")) {
                                                taskModel.setmTaskEmployeePickupTime("No");
                                            }

                                            taskModel.setmTaskEmployeeLocationStatus(employeeDetailJsonObject
                                                    .getString("locationStatus"));
                                            taskModel.setmTaskEmployeeTripType(response.getJSONObject("data")
                                                    .getString("tripType"));

                                            taskModel.setmTaskEmployeeBoardedFlag(employeeDetailJsonObject
                                                    .getString("boardedFlg"));

                                            if (employeeDetailJsonObject
                                                    .getString("boardedFlg").equalsIgnoreCase("N")) {
                                                taskModel.setIsCheckBoxSelected(false);
                                            } else {
                                                taskModel.setIsCheckBoxSelected(true);
                                            }

                                            taskModel.setIsActive(true);
                                            mHasNewTask = true;

                                            mTaskModelArrayList.add(taskModel);
                                            mDBHelper.addTask(taskModel);
                                        }
                                    }

                                    break;

                                default:
                                    break;
                            }
                        } else {
                            mBtnViewMap.setVisibility(Button.GONE);
                        }

                        if (mHasNewTask) {
                            mHasNewTask = false;
                        }

                        if (hasViewCreated)
                            refreshTaskList();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    if (hasViewCreated)
                        setListShown(true);

                    if (showError) {
                        endProgress();
                    }
                }

                @Override
                public void onError(VolleyError Error) {
                    endProgress();
                }
            }, true, new DialogCallBack() {
                @Override
                protected void onPositiveBtnClick() {
                    super.onPositiveBtnClick();
                    loadTaskFromServer(showError, startButtonFlag);
                }

                @Override
                protected void onNegativeBtnClick() {
                    super.onNegativeBtnClick();
                }
            });
}

/**
 * This method will refresh the entire task list of
 * driver if something new comes from server.
 */
private void refreshTaskList() {
    if (getActivity() != null) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mTaskListAdapter != null) {
                    mTaskListAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}}

我的Logcat错误是:

java.lang.IllegalStateException: Content view not yet created
        at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
        at android.support.v4.app.ListFragment.setListShown(ListFragment.java:280)
        at android.support.v4.app.ListFragment.setListShown(ListFragment.java:258)            
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案