将数组列表从接口实现传递到类[重复项]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数组列表从接口实现传递到类[重复项]相关的知识,希望对你有一定的参考价值。

此问题已经在这里有了答案:

我是初学者,我正在尝试使用ArrayList中的Interface。我从这里的文章中得到了结构。问题是,当我尝试在Fragment中使用它时,它为我提供了null。

课程是:

public class NewsFRagment extends Fragment implements BackgroundTask.CallBack {

    private ArrayList<NewsPOJO> arrayList1;

    public NewsFRagment() {

        // Required empty public constructor
    }

    @Override
    public void onSuccess(ArrayList<NewsPOJO> arrayList) {

        this.arrayList1 = arrayList;
        Log.d("arraylist in onSuccess", String.valueOf(arrayList.size()));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.news_fragment, container, false);

        BackgroundTask backgroundTask = new BackgroundTask(this.getActivity(), this);
        backgroundTask.execute();

        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv_news);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        Log.d("Arraylist1 size", String.valueOf(+arrayList1.size()));
        recyclerView.setAdapter(new MyRecyclerAdapter(this.getActivity(), arrayList1));

        return v;
    }

    @Override
    public String toString() {
        return "News Fragment";
    }

}

我需要使用onSuccess中的arraylist并将其传递给我的setAdapter方法,当我在setAdaper中传递它时,问题为null,但在onSuccess中不为null,我该如何解决呢?

这是logcat:

06-03 04:52:50.367 21851-21851/hilz.reycfrag W/art: Failed to find OatDexFile for DexFile /data/data/hilz.reycfrag/files/instant-run/dex/slice-slice_8-classes.dex ( canonical path /data/data/hilz.reycfrag/files/instant-run/dex/slice-slice_8-classes.dex) with checksum 0xd8bc1e5a in OatFile /data/data/hilz.reycfrag/cache/slice-slice_8-classes.dex
06-03 04:52:51.150 21851-21851/hilz.reycfrag D/androidRuntime: Shutting down VM
06-03 04:52:51.150 21851-21851/hilz.reycfrag E/AndroidRuntime: FATAL EXCEPTION: main
Process: hilz.reycfrag, PID: 21851
java.lang.RuntimeException: Unable to start activity ComponentInfo{hilz.reycfrag/hilz.reycfrag.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at hilz.reycfrag.NewsFRagment.onCreateView(NewsFRagment.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-03 04:52:51.178 21851-21897/hilz.reycfrag D/arraylist in onSuccess: 4

我是一个初学者,我正在尝试从接口使用ArrayList。我从这里的文章中得到了结构。问题是,当我尝试在Fragment中使用它时,它使我为null。该类是:...

答案
@Override
public void onSuccess(ArrayList<NewsPOJO> arrayList) {

    this.arrayList1 = arrayList;
    Log.d("arraylist in onSuccess", String.valueOf(arrayList.size()));

    // Make sure your fragment is done view loading before on success.
    RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv_news);
    recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    Log.d("Arraylist1 size", String.valueOf(+arrayList1.size()));
    recyclerView.setAdapter(new MyRecyclerAdapter(this.getActivity(), arrayList1));

}
另一答案

检查回调发生的顺序...

以上是关于将数组列表从接口实现传递到类[重复项]的主要内容,如果未能解决你的问题,请参考以下文章

将 JSON 字符串从片段传递到适配器的问题

将接口从片段传递到kotlin中的活动

如何将值从一个片段的回收器视图项传递到另一个片段

如何从 UITableView 获取行索引,其中数组列表由追加到类

将列表项从 c# 传递到 javascript 数组

将数据从活动传递到片段[重复]