如何知道片段何时完成加载?

时间:2014-09-02 19:41:02

标签: java android android-fragments

我在android中非常新,我有一个问题:

我有一个包含表格的应用 如果存储了一些调用,我想更改表格

现在我对onResume()进行了调用,但是在表完全加载之前调用它,因此我无法访问表的元素。

当我看到关于Resume的文档是片段处于活动状态之前调用的最后一步时,我不知道在哪里调用我的方法来处理调用。

这是ListFragment.java中的代码:

@Override
public void onResume() {
    super.onResume();

    if (Network.isNetworkAvailable(getActivity())) {
        processPendingCalls();
    }
}

private void processPendingCalls() {

    Log.v(TAG, "============= processPendingCalls");

    View listview = getActivity().findViewById(R.id.trigger_button);
    for (Map.Entry<String, Call.CallData> entry : Call.CALL_MAP.entrySet()) {
        Customer.CustomerData customer = Customer.CUSTOMER_MAP.get(entry.getKey());
        Call.CallData call = Call.Call_MAP.get(entry.getKey());

        if (call.storedRequest) {
            Log.v(TAG, "Stored request for " + customer.name);
            CompoundButton toggle = (CompoundButton) listview.findViewWithTag(company.id);  // this line breaks!!!
            Log.v(TAG, "(STORED) Toggle View is: " + toggle);
            ProcessManager oom = new ProcessManager(getActivity());
            oom.processCallInBackground(customer, "", "", (CompoundButton) toggle, false);
        }
    }
}

我使用CustomListAdapter构建表,我可以在CustomListAdapter中的getView方法之前看到processPendingCalls方法的调试打印

这是调用processPendingCalls函数时的错误:

09-02 15:27:22.648    7562-7562/com.MyApp.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {com.MyApp.app/com.MyApp.app.ListActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
        at java.util.ArrayList.get(ArrayList.java:304)
        at com.MyApp.app.ProcessManager.injectIDs(ProcessManager.java:142)
        at com.MyApp.app.ProcessManager.processCallInBackground(ProcessManager.java:72)
        at com.MyApp.app.ListFragment.processPendingCall(ListFragment.java:133)
        at com.MyApp.app.ListFragment.onResume(ListFragment.java:82)
        at android.support.v4.app.Fragment.performResume(Fragment.java:1543)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
        at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1894)
        at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:466)
        at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:455)
        at com.actionbarsherlock.app.SherlockFragmentActivity.onPostResume(SherlockFragmentActivity.java:68)

0 个答案:

没有答案