android RecyclerView onMeasure nullpointer - android

I wanna use recyclerview, but null pointer has appeared.
So I searched this issue, They said i have to make LinearLayoutManager set like below, I did it.:
setLayoutManager(new LinearLayoutManager(context))
But, It doesn't work.
My error log is :
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1694)
at android.view.View.measure(View.java:16628)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16628)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:883)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2298)
at android.view.View.measure(View.java:16628)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5610)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(Native Method)
My code :
GridLayoutManager gridLayoutManager = new GridLayoutManager(IndexActivity.this , 2 );
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(mAdapter);
xml :
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="#dimen/index_gridview_horizontal_spacing"
android:verticalSpacing="#dimen/index_gridview_vertical_spacing"
android:numColumns="2"
android:id="#+id/recyclerView" />
This problem was solved by changing gradle.
My reference is here : beecanvas.com/yjcSh8Tp

Try like this:
RecyclerView recyclerView = (RecyclerView) findViewById(
R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setAdapter(new NumberedAdapter(30));
Here I have used span count of 2, you will have 30 elements, you can modify as per your need. Hope this helps you.

have you used an old revision of recyclerview? you can check my another answer for details, hope it help.

I suppose you are using wrong GridLayoutManager. Please make sure you have import android.support.v7.widget.GridLayoutManager, and then:
//for Activity
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
or //for Fragment
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
should be ok.
[UPDATE]
You only can set one LayoutManager for RecyclerView, LinearLayoutManager or GridLayoutManager or StaggeredGridLayoutManager, not both of them. For your case, just set GridLayoutManager.
Hope this help!

Related

FragmentManager IllegalStateException: No host

UPDATE:
Crashes gone when i put nullcheks (fragment != null, getActivity() != null) whereever i could.
My app is using ViewPager (offscreen limit 5) with FragmentStatePagerAdapter initialized with getSupportFragmentManager(). Each page has it's own fragments and backstack.
App crashes on startup. Problem occurs only on preLollipop devices and emulator. Using latest support lbrary (tried 24.0.0 also - same result)
Stacktrace:
java.lang.IllegalStateException: No host at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1273)
at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at
android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:2138)
at android.support.v4.app.Fragment.onCreate(Fragment.java:1254)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2062)
at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)
at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at
android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
at
android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
at
android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1237)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at
android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at
android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at
com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at
android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
If you have nested Fragment (extends FragmentActivity), then use getChildFragmentManager() instead of getFragmentManager().
may be it will help you ..
In my case, I am using viewpager inside fragment and was using getChildFragmentManager() while setting viewpager adapter. It is also recommended to use this I guess.
There is also an issue, java.lang.IllegalStateException: No host exception was not showing everytime but it was showing too often when I called a method of viewpager containing Activity or coming back from another Activity class before setting viewpager.setCurrentItem(POSITION);
I tried in many ways and finally I changed getChildFragmentManager() to getActivity().getSupportFragmentManager();
After changing this java.lang.IllegalStateException: No host exception is not showing any more.
I think its related to Activity state issue which contains viewpager.
When you are using viewPager , make sure you are following these practices
call super(fragmentManager) in your constructor
if you are using Viewpager inside a Fragment, pass getChildFragmentManager in to Adapter

RecycleView using GridLayout

I got this exception using recyclingView in a Fragment:
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
There is already a solution for this kind of bug:
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
java.lang.NullPointerException at android.support.v7.widget.RecyclerView.onMeasure
Using this code everything works as it should but the thing is that I want to use a GridLayout instead.
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),3);
recyclerView.setLayoutManager(gridLayoutManager); ...
I have already debugged but GridLayoutManager in not null...
Please I appreciate any help
Initialize the Recyclerview like:
RecyclerView recyclerView = (RecyclerView) findViewById(
R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setAdapter(new myAdapter(count));
Source: Recyclerview Gridlayout
Try this before setting adapter to recyclerView
recyclerView.setHasFixedSize(true);

NullPointerException at android.support.v7.widget.RecyclerView.onInterceptTouchEvent [duplicate]

I am getting following Exception
01-27 11:15:15.756 18348-18348/com.example.pnimje.newswipelistview E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:15848)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
and RecyclerView.java:1764 is
mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);
Why is mLayout (LayoutManager) null?
because you have not set LinearLayoutManager to RecyclerView.
for example:
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(new CustomAdapter());
These kinds of errors usually originate in not setting the RecyclerView's layout manager. Bein' in a hurry myself I forgot to set one which ended in the following error:
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:1636)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1820)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2177)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1878)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2177)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1878)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2177)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1878)
The following line should resolve it:
mRecyclerView.setLayoutManager(new LinearLayoutManager((Activity)this));
Antón response is good but can be confusing in beginners like me, talves this serves to clarify some doubts.
RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buscar);
rv = (RecyclerView) findViewById(R.id.RecViewTest);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
}
You need to verify that what you are sending to the adapter is not an empty List<Type>. I recommend you to either create the RecyclerView by code and add it later when you have the information, or to put an empty NULL object before passing it to the constructor of the adapter.

Cause for NullPointerException android.support.v7.widget.RecyclerView.onMeasure

I am getting following Exception
01-27 11:15:15.756 18348-18348/com.example.pnimje.newswipelistview E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:15848)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
and RecyclerView.java:1764 is
mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);
Why is mLayout (LayoutManager) null?
because you have not set LinearLayoutManager to RecyclerView.
for example:
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(new CustomAdapter());
These kinds of errors usually originate in not setting the RecyclerView's layout manager. Bein' in a hurry myself I forgot to set one which ended in the following error:
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:1636)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1820)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2177)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1878)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2177)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1878)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2177)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1878)
The following line should resolve it:
mRecyclerView.setLayoutManager(new LinearLayoutManager((Activity)this));
Antón response is good but can be confusing in beginners like me, talves this serves to clarify some doubts.
RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buscar);
rv = (RecyclerView) findViewById(R.id.RecViewTest);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
}
You need to verify that what you are sending to the adapter is not an empty List<Type>. I recommend you to either create the RecyclerView by code and add it later when you have the information, or to put an empty NULL object before passing it to the constructor of the adapter.

ButterKnife does not injects views for MyFragment

Something is wrong with ButterKnife injection. Upon starting my app, I'm getting below error:
01-23 11:35:36.229 23663-23663/lt.andro.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: lt.andro.myapp, PID: 23663
java.lang.RuntimeException: Unable to inject views for MyNewFragment{528796ec #0 id=0x7f0a017c android:switcher:2131362172:1}
at butterknife.ButterKnife.inject(ButterKnife.java:263)
at butterknife.ButterKnife.inject(ButterKnife.java:207)
at lt.andro.myapp.BaseFragment.onViewCreated(BaseFragment.java:66)
at lt.andro.myapp.MyNewFragment.onViewCreated(MyNewFragment.java:117)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:961)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.
Looks like all ButterKnife's #InjectView types are correct, but still ButterKnife is unable to inject views for my new fragment.
My layout looks like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
and my new fragment has this #InjectViews:
#InjectView(R.id.swipe_refresh) SwipeRefreshLayout swipeRefreshLayout;
#InjectView(R.id.recycler) RecyclerView list;
In a fragment you should use it in onCreateView(...)
View v = inflater.inflate(R.layout.meh, container, false);
ButterKnife.inject(this, v);
Apparently I had to check other ButterKnife's annotations like #OnItemClick. I've used it for a RecyclerView, which was causing that RuntimeException for me.
It also pays to watch any bindings you are injecting from within the injected view that throws the error.
In my instance I had a fragment foo that injected a view bar
// Foo.java
View v = inflater.inflate(R.layout.bar, container, false);
ButterKnife.inject(this, v);
This was throwing the described error. It turns out that in foo I was binding to an element in baz that I expected to find inside my injected view bar.
// Foo.java
#InjectView(R.id.baz)
TextView filterTextView;
Except I had a typo in 'baz' in the layout xml for bar
// bar.xml
<EditText
android:id="#+id/bazz"
/>
And this caused Unable to inject views for...

Categories

Resources