layoutInflater在片段java android中崩溃

时间:2016-11-05 14:36:45

标签: java android android-fragments

我尝试了不同的解决方案,但是当我启动应用程序时,我收到一个布局错误,谢谢大家的帮助。

适配器:

public class Adattatore_Main extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public final int TYPE_MOVIE = 0;
    public final int TYPE_LOAD = 1;
    Context context;
    List<Variabili_Main> movies;
    OnLoadMoreListener loadMoreListener;
    boolean isLoading = false, isMoreDataAvailable = true;

    /*
    * isLoading - to set the remote loading and complete status to fix back to back load more call
    * isMoreDataAvailable - to set whether more data from server available or not.
    * It will prevent useless load more request even after all the server data loaded
    * */


    public Adattatore_Main(Context context, List<Variabili_Main> movies) {
        this.context = context;
        this.movies = movies;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        if(viewType==TYPE_MOVIE){
            return new MovieHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.contenitore_post, parent, false));
        }else{
            return new LoadHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.caricamento, parent, false));
        }
    }

崩溃:

LayoutInflater inflater = LayoutInflater.from(context);

主要活动:

public class Main_Post extends Fragment {
    private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 0;
    RecyclerView recyclerView;
    List<Variabili_Main> movies;
    Adattatore_Main adapter;
    Api api;
    String TAG = "MainActivity - ";
    Context context;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activitymain_post, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        movies = new ArrayList<>();
        adapter = new Adattatore_Main(context, movies);
        adapter.setLoadMoreListener(new Adattatore_Main.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {

                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        int index = movies.size() - 1;
                        loadMore(index);
                    }
                });
                //Calling loadMore function in Runnable to fix the
                // java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling error
            }
        });
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new VerticalLineDecorator(2));
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        recyclerView.setAdapter(adapter);
        api = ServiceGenerator.createService(Api.class);
        load(0);
        return view;
    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

</LinearLayout>

调试:

                                     --------- beginning of crash
11-05 14:44:24.241 659-659/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.androidbelieve.drawerwithswipetabs, PID: 659
                                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                                                                                        at android.view.LayoutInflater.from(LayoutInflater.java:232)
                                                                                        at com.androidbelieve.drawerwithswipetabs.Adattatore_Main.onCreateViewHolder(Adattatore_Main.java:76)
                                                                                        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5464)
                                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4689)
                                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4599)
                                                                                        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988)
                                                                                        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)
                                                                                        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347)
                                                                                        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                                        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3003)
                                                                                        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2600)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
                                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1560)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
11-05 14:44:24.241 659-659/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime:     at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
                                                                                        at android.view.View.measure(View.java:19731)
                                                                                        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
                                                                                        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
                                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
                                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
                                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                                                                                        at android.view.Choreographer.doFrame(Choreographer.java:619)
                                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                                                                                        at android.os.Handler.handleCallback(Handler.java:751)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


                                                                                    --------- beginning of system

如果您需要更多代码,这是代码的一部分。谢谢。

2 个答案:

答案 0 :(得分:0)

在获得系统布局膨胀权限之前,您需要对布局进行充气。你只需更改代码即可。其他人认为,如果您可以看到onCreateView()方法,则在初始化适配器对象时传递空上下文对象,这就是您的回收器视图接收空上下文的原因。在传入适配器对象之前,需要初始化上下文。我希望这能解决你的问题

context = getActivity();
adapter = new Adattatore_Main(context, movies);

然后在适配器上更改以下代码后

LayoutInflater  inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater = inflater.from(parent.getContext()).inflate(R.layout.fragment_row, parent ,false);

答案 1 :(得分:0)

LayoutInflater inflater = LayoutInflater.from(parent.getcontext());

<强>更新

似乎你没有在适配器中传递上下文。如果你需要在片段中使用任何上下文,那么使用getActivity()。它将解决您的上下文初始化问题。