在调用其他代码之前,我怎么能等到我的改装下载成功?

时间:2015-02-18 22:17:45

标签: android rest retrofit

在我的oncreate中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    eventList = new ArrayList<Event>();
    loadEvents();
    init();

}

public void init() {

    recyclerView = (RecyclerView) findViewById(R.id.eventListRV);
    recyclerView.setHasFixedSize(true);
    adapter = new EventAdapter(this, eventList);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

public void loadEvents() {
    MarvelApi.create(privateKey, publicKey, getApplicationContext(), 5*1024*1024);

    EventRequest request = new EventRequest(RequestSignature.create());
    request.setLimit(20);
    request.setOffset(0);

    EventManager client = new EventManager();
    client.listCreators(request, new Callback<ServiceResponse<Event>>() {
        @Override
        public void success(ServiceResponse<Event> eventServiceResponse, Response response) {
            eventList.addAll(eventServiceResponse.data.results);
        }

        @Override
        public void failure(RetrofitError error) {
            mTV.setText(error.toString());
        }
    });

}

有没有办法等到我的loadEvents()代码返回成功并在调用init()之前加载数据?

现在我相信我的load()代码在loadEvents()完成加载之前被调用。我想等到我的loadEvents()成功并在继续我的程序之前加载所有数据。

错误日志:

 02-18 21:41:19.789  31420-31420/kim.albert.marveleventtracker E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: kim.albert.marveleventtracker, PID: 31420
java.lang.NullPointerException
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1694)
        at android.view.View.measure(View.java:17387)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
        at android.view.View.measure(View.java:17387)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17387)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:17387)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17387)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17387)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2533)
        at android.view.View.measure(View.java:17387)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2214)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1351)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1550)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1235)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6472)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
        at android.view.Choreographer.doCallbacks(Choreographer.java:603)
        at android.view.Choreographer.doFrame(Choreographer.java:573)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        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:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

也是应用程序运行时的调试日志,但不显示任何内容

02-18 21:53:13.835      686-686/kim.albert.marveleventtracker I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-18 21:53:13.835      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to resolve virtual method 12746: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-18 21:53:13.835      686-686/kim.albert.marveleventtracker D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-18 21:53:13.835      686-686/kim.albert.marveleventtracker I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-18 21:53:13.835      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to resolve virtual method 12752: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-18 21:53:13.835      686-686/kim.albert.marveleventtracker D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-18 21:53:13.835      686-686/kim.albert.marveleventtracker I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-18 21:53:13.835      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to resolve virtual method 9103: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-18 21:53:13.835      686-686/kim.albert.marveleventtracker D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-18 21:53:13.845      686-686/kim.albert.marveleventtracker I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-18 21:53:13.845      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to resolve virtual method 377: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-18 21:53:13.845      686-686/kim.albert.marveleventtracker D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-18 21:53:13.845      686-686/kim.albert.marveleventtracker I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-18 21:53:13.845      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to resolve virtual method 399: Landroid/content/res/TypedArray;.getType (I)I
02-18 21:53:13.845      686-686/kim.albert.marveleventtracker D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker I/dalvikvm﹕ Could not find method java.nio.file.Files.newOutputStream, referenced from method okio.Okio.sink
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to resolve static method 16973: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream;
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker I/dalvikvm﹕ Could not find method java.nio.file.Files.newInputStream, referenced from method okio.Okio.source
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker W/dalvikvm﹕ VFY: unable to resolve static method 16972: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream;
02-18 21:53:13.945      686-686/kim.albert.marveleventtracker D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a

2 个答案:

答案 0 :(得分:0)

在EventManager的成功回调方法中移动初始化代码,你应该得到你想要的东西。

        @Override 
        public void success(ServiceResponse<Event> eventServiceResponse, Response response) { 
                  recyclerView = (RecyclerView) findViewById(R.id.eventListRV);
                  recyclerView.setHasFixedSize(true); 
                  adapter = new EventAdapter(this, eventServiceResponse.data.results);
                  recyclerView.setAdapter(adapter); 
                  recyclerView.setLayoutManager(new LinearLayoutManager(this));
        } 

答案 1 :(得分:0)

是的,您所要做的就是将init()电话转移到改装回调。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    eventList = new ArrayList<Event>();
    loadEvents();
    //init();

}

public void init() {

    recyclerView = (RecyclerView) findViewById(R.id.eventListRV);
    recyclerView.setHasFixedSize(true);
    adapter = new EventAdapter(this, eventList);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

public void loadEvents() {
    MarvelApi.create(privateKey, publicKey, getApplicationContext(), 5*1024*1024);

    EventRequest request = new EventRequest(RequestSignature.create());
    request.setLimit(20);
    request.setOffset(0);

    EventManager client = new EventManager();
    client.listCreators(request, new Callback<ServiceResponse<Event>>() {
        @Override
        public void success(ServiceResponse<Event> eventServiceResponse, Response response) {
            eventList.addAll(eventServiceResponse.data.results);
            init();
        }

        @Override
        public void failure(RetrofitError error) {
            mTV.setText(error.toString());
        }
    });

}

一旦调用成功函数,这将调用init(),因此只有在加载数据后才会执行它。