I have found the following code on how to get display height and width from ImageView: Trying to get the display size of an image in an ImageView . When I try to use it in the layout snippet below, I get following error.
<com.mycompany.widget.SizeAwareImageView
android:id="#+id/account_profile_img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/avatar"
android:clickable="true"
android:gravity="bottom|center_horizontal"
android:onClick="onEditPhotoClicked" />
ERROR:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.myapp/com.company.myapp.RegisterActivity}: android.view.InflateException: Binary XML file line #218: Error inflating class com.company.myapp.widget.SizeAwareImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #218: Error inflating class com.company.myapp.widget.SizeAwareImageView
at android.view.LayoutInflater.createView(LayoutInflater.java:602)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
at android.app.Activity.setContentView(Activity.java:1928)
at com.company.myapp.RegisterActivity.onCreate(RegisterActivity.java:63)
at android.app.Activity.performCreate(Activity.java:5250)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
... 11 more
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getConstructor(Class.java:431)
at android.view.LayoutInflater.createView(LayoutInflater.java:567)
... 25 more
UPDATE
From the graphics Layout I also get the error
java.lang.NullPointerException
Exception details are logged in Window > Show View > Error Log
java.lang.NullPointerException
at com.company.myapp.widget.SizeAwareImageView.onMeasure(SizeAwareImageView.java:66)
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.measureHorizontal(LinearLayout.java:1052)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
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.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
at android.view.View.measure(View.java:15848)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.widget.ScrollView.onMeasure(ScrollView.java:321)
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.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)
SECOND UPDATE
the errored line in question is the second of
final Drawable d = getDrawable();
final int origW = d.getIntrinsicWidth();
final int origH = d.getIntrinsicHeight();
The complete code is here on stack overflow at Trying to get the display size of an image in an ImageView . It's the response with the most votes.
Make sure you have provided the right constructor for the XML. Your custom Views constructor should have the following method signature:
public SizeAwareImageView(Context context, AttributeSet attrs){
// todo
}
I'm guessing it currently looks like this:
public SizeAwareImageView(Context context){
// todo
}
Also I'd double check you meant to set the XML parameter android:background, for ImageView you actually set android:src.
Related
following code is work well in android greater than 21 (lollipop),
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_course_not_bought, parent, false);
return new ViewHolder(v);
}
but in android < 21 make a crash...
I use android studio and import the library of CardView
11-20 13:39:43.418 5851-5851 E/AndroidRuntime: FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at ir.dpsoft.lms.RecyclerViewAdapter.RecyclerNotBoughtCourses.onCreateViewHolder(RecyclerNotBoughtCourses.java:55)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6488)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5675)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5558)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5554)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:583)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3694)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3110)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
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:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
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:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
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:15518)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.widget.ScrollView.onMeasure(ScrollView.java:321)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
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:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
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:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
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:15518)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1060)
Add these dependencies to your gradle file:
compile 'com.android.support:appcompat-v7:27.0.0'
compile 'com.android.support:cardview-v7:27.0.0'
Refer to this documentation about compatibility on earlier versions:
https://developer.android.com/training/material/compatibility.html
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
java.lang.NullPointerException
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:89)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1006)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1154)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:700)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1364)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:765)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:700)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:767)
at android.view.View.measure(View.java:15848)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1085)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
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:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
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:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
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:5012)
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)
Application terminated.
A null reference exception always indicates that something is null.
To avoid that use
if(yourObject != null)
{
// put code you want to execute here
}
For a better answer share the code with us.
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.
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.
I'm experiencing an odd behaviour in my Android app. I have a drawable , walkthrough_safe.png and have a version in drawable-mdpi, drawable-hdpi and drawable-xhdpi folders.
I'm seeing this crash in analytics
{main} android.view.InflateException: Binary XML file line #7: Error inflating class
<unknown> at android.view.LayoutInflater.createView(LayoutInflater.java:613) at
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:
56) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) at
android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at
android.view.LayoutInflater.inflate(LayoutInflater.java:489) at
android.view.LayoutInflater.inflate(LayoutInflater.java:396) at
com.a.b.activity.WalkThroughLoginActivity$WalkthroughFragment.onCreateView(WalkThroughLoginA
ctivity.java:247) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
at android.support.v4.app.y.a(FragmentManager.java:942) at
android.support.v4.app.y.a(FragmentManager.java:1121) at
android.support.v4.app.l.run(BackStackRecord.java:682) at
android.support.v4.app.y.d(FragmentManager.java:1484) at
android.support.v4.app.y.b(FragmentManager.java:482) at
android.support.v4.app.ad.finishUpdate(FragmentPagerAdapter.java:141) at
android.support.v4.view.ViewPager.a(ViewPager.java:1073) at
android.support.v4.view.ViewPager.b(ViewPager.java:919) at
android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441) at
android.view.View.measure(View.java:15264) at
android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617) at
android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399) at
android.view.View.measure(View.java:15264) at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916) at
android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at
android.view.View.measure(View.java:15264) at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916) at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390) at
android.widget.LinearLayout.measureVertical(LinearLayout.java:681) at
android.widget.LinearLayout.onMeasure(LinearLayout.java:574) at
android.view.View.measure(View.java:15264) at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916) at
android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at
android.view.View.measure(View.java:15264) at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916) at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390) at
android.widget.LinearLayout.measureVertical(LinearLayout.java:681) at
android.widget.LinearLayout.onMeasure(LinearLayout.java:574) at
android.view.View.measure(View.java:15264) at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916) at
android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at
com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2339) at
android.view.View.measure(View.java:15264) at
android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2131) at
android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1242) at
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435) at
android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1127) at
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4606) at
android.view.Choreographer$CallbackRecord.run(Choreographer.java:747) at
android.view.Choreographer.doCallbacks(Choreographer.java:567) at
android.view.Choreographer.doFrame(Choreographer.java:536) at
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733) at
android.os.Handler.handleCallback(Handler.java:615) at
android.os.Handler.dispatchMessage(Handler.java:92) at
android.os.Looper.loop(Looper.java:153) at
android.app.ActivityThread.main(ActivityThread.java:5006) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:511) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) at
dalvik.system.NativeStart.main(Native Method) Caused by:
java.lang.reflect.InvocationTargetException at
java.lang.reflect.Constructor.constructNative(Native Method) at
java.lang.reflect.Constructor.newInstance(Constructor.java:417) at
android.view.LayoutInflater.createView(LayoutInflater.java:587) ... 59 more Caused by:
android.content.res.Resources$NotFoundException: File res/drawable-mdpi-
v4/walkthrough_safe.png from drawable resource ID #0x7f02015c at
android.content.res.Resources.loadDrawable(Resources.java:2054) at
android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.widget.ImageView.
<init>(ImageView.java:120) at android.widget.ImageView.<init>(ImageView.java:110) ... 62
more Caused by: java.io.FileNotFoundException: res/drawable-mdpi-v4/walkthrough_safe.png at
android.content.res.AssetManager.openNonAssetNative(Native Method) at
android.content.res.AssetManager.openNonAsset(AssetManager.java:410) at
android.content.res.Resources.loadDrawable(Resources.java:2046) ... 65 more
In the line WalkThroughLoginActivity.java:247 I have this:
inflater.inflate(R.layout.fragment_login_walkthrough_2,
container, false);
And this is the layout file for fragment_login_walkthrough_2.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:orientation="vertical" >
<ImageView
style="#style/icon_walkthrough"
android:src="#drawable/walkthrough_safe" />
<TextView
style="#style/title_walkthrough"
android:layout_marginTop="#dimen/margin_walktrough_title"
android:text="#string/walkthrough2_title" />
<TextView
style="#style/text_walkthrough"
android:text="#string/walkthrough2_subtitle"/>
</LinearLayout>
Please correctly if I'm wrong but it seems to me that android is looking up for my drawable in the wrong folder (drawable-mdpi-v4), which obvioulsy does not exist. Not only that, after not finding it, it is not falling back to a more general folder, such us drawable-mdpi.
Have I missed something?