Noe Nazza Noe Nazza - 1 month ago 12
Android Question

layoutInflater crash in fragment java android

I tried different solutions but continuously when I start the application I receive a layout error, thank you all for the help.

Adapter :

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));
}
}


Crash here :

LayoutInflater inflater = LayoutInflater.from(context);


Main Activity :

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>


Debug :

--------- 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


This is a part of code, if you need more code i'm here. Thank you.

Answer

You inflate layout before getting system layout inflating permission. You just change in you code. Other think if you can see your onCreateView() method then you pass null context object when you initialize your adapter object that's why your recycler view receive null context. You need to initialize your context before you pass in your adapter object. i hope this will solve your problem

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

Then after change your below code on your adapter

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