How to fix getCount() in viewPager adapter in Android [duplicate] - android

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I want use viewPager in my application and I want use this library: UltraViewPager.
I write below codes in adapter:
public class BoxOfficeAdapter extends PagerAdapter {
private Context context;
private List<BoxOfficeDatum> model;
private LayoutInflater inflater;
private BoxOfficeDateListener listener;
public BoxOfficeAdapter(Context context, List<BoxOfficeDatum> model, BoxOfficeDateListener listener) {
this.context = context;
this.model = model;
this.listener = listener;
this.inflater = (LayoutInflater) this.context.getSystemService(this.context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return model.size();
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
View view = this.inflater.inflate(R.layout.row_box_office, container, false);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
But when run application, show me this error :
FATAL EXCEPTION: main
Process: com.example.app, PID: 5948
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.tmall.ultraviewpager.UltraViewPagerAdapter.getCount()' on a null object reference
at com.tmall.ultraviewpager.UltraViewPagerView.getCurrentItem(UltraViewPagerView.java:220)
at com.tmall.ultraviewpager.UltraViewPagerView.onMeasurePage(UltraViewPagerView.java:88)
at com.tmall.ultraviewpager.UltraViewPagerView.onMeasure(UltraViewPagerView.java:84)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at com.tmall.ultraviewpager.UltraViewPager.onMeasure(UltraViewPager.java:177)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.CardView.onMeasure(CardView.java:211)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19826)
at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1420)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:482)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19826)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
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:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
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:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:688)
at android.view.View.measure(View.java:19826)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2335)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1410)
06-04 16:37:47.075 5948-5948/com.example.app E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1660)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1286)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6536)
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:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
How can I fix it?

List is null that why null Pointer is coming .
try different method to set data from response to your list
Like:
BoxOfficeResponse responseBody=response.body();
boxOfficeModel.addAll(responseBody.getBoxOfficeData().getBoxOfficeDatum())
or
for(int i=0; i<responseBody.getBoxOfficeData().getBoxOfficeDatum(); i++){
boxOfficeModel.add(responseBody.getBoxOfficeData().getBoxOfficeDatum().get(i))
}

Related

java.lang.ClassCastException when trying to add constraints to textview

I'm programmatically creating a constraint layout and adding a text view to it, but when I try to add the constraints it raises a java.lang.ClassCastException exception. Here is my code:
var textlayoutbottom = ConstraintLayout(this)
textlayout.addView(textlayoutbottom)
var typelabel = TextView(this)
textlayoutbottom.addView(typelabel)
textlayoutbottom.layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
typelabel.text = getAnimeType(anime.type)
var params = typelabel.layoutParams as ConstraintLayout.LayoutParams
params.leftToLeft = textlayoutbottom.id
params.bottomToBottom = textlayoutbottom.id
typelabel.requestLayout()
But the app crashes and it doesn't even show the error line.
Here is my log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.package, PID: 5234
java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
at android.widget.LinearLayout.measureVertical(LinearLayout.java:731)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
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:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
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:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
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:19857)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1293)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.widget.ScrollView.onMeasure(ScrollView.java:340)
at android.view.View.measure(View.java:19857)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:792)
at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:583)
at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.layout(ConstraintWidgetContainer.java:682)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solveLinearSystem(BasicMeasure.java:159)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:290)
at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:119)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1578)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1690)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
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:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
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:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
at android.view.View.measure(View.java:19857)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Any ideas?
Hey you are setting ConstraintLayout.LayoutParams to ConstraintLayout itself, try setting it to TextView and for textlayoutbottom use LinearLayout.LayoutParams
textlayoutbottom.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT) //for constraint layout
typeLabel.layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)//for typeLabel

Fragment Error when moving the same screen

https://github.com/MobileSeoul/2017seoul-18
I brought open source from Github.
But, this code was designed to push only one button at a time.
So, as soon as I started, I changed 4 information to appear on the screen.
#Override
public void onResume() {
super.onResume();
String[] urls = {queryUrlFsn, queryasthmaWho, querybrainWho, queryskinWho};
if (checkInternet() == true) {
INTERNET_STATE = true;
thread(urls[0], date, 0);
new addThread(fsnIndex, "Food Poisoning Index", value[0], FSN_DIVIDE_VALUE, R.drawable.fsn, showDateTime(showDate)).start();
thread(urls[1], date, 1);
new addThread(asthmaWhoIndex, "Asthma lung disease possible index", value[1], WINTERLIFE_DIVIDE_VALUE, R.drawable.asthmawho, showDateTime(showDate)).start();
thread(urls[2], date, 2);
new addThread(brainWhoIndex, "Stroke index", value[2], WINTERLIFE_DIVIDE_VALUE, R.drawable.brainwho, showDateTime(showDate)).start();
thread(urls[3], date, 3);
new addThread(skinWhoIndex, "Skin disease potential index", value[3], WINTERLIFE_DIVIDE_VALUE, R.drawable.skinwho, showDateTime(showDate)).start();
} else {
Toast.makeText(getActivity(),"Network not connected", Toast.LENGTH_SHORT).show();
}
}
https://youtu.be/NuxS6MH55oA
↑ However, when moving from screen A back to screen A, there was a problem that the app was shutting down.
When I checked it, I did not have any problem when I removed the 4 pieces of information displayed on the screen A by clicking on the "trash can picture" and then moved to the screen A again.
So, in order to initialize the screen when I switch the screen, I did googleing.
I put the code presented in the stack overflow in the if / else statement below.
Reset/reload fragment container
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
MosquitoFragment mosquitoFragment = new MosquitoFragment();
WeatherFragment weatherFragment = new WeatherFragment();
ExpressionFragment expressionFragment = new ExpressionFragment();
AirFragment airFragment = new AirFragment();
int id = item.getItemId();
if (id == R.id.nav_weather) {
// I inserted this point.
transaction.replace(R.id.content_frame, weatherFragment);
}
else if (id == R.id.nav_mosquito) {
// I inserted this point.
transaction.replace(R.id.content_frame, mosquitoFragment);
}
else if (id == R.id.nav_expression) {
// I inserted this point.
transaction.replace(R.id.content_frame, expressionFragment);
}
else if (id == R.id.nav_air) {
// I inserted this point.
transaction.replace(R.id.content_frame, airFragment);
}
transaction.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
However, when I move from A screen to A screen again, an error still occurs. How do we solve it? If I can not solve this problem, I will not fix this open source.
Error Code:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: kr.cds.jisulife, PID: 8226
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:483)
at java.lang.Integer.valueOf(Integer.java:611)
at kr.cds.jisulife.BrainWhoIndex.getGradeToString(BrainWhoIndex.java:36)
at kr.cds.jisulife.WeatherFragment$ListViewAdapter.getView(WeatherFragment.java:452)
at android.widget.AbsListView.obtainView(AbsListView.java:2363)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1326)
at android.widget.ListView.onMeasure(ListView.java:1233)
at android.view.View.measure(View.java:19857)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:19857)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1391)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
at android.view.View.measure(View.java:19857)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
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:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
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:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
at android.view.View.measure(View.java:19857)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

recyclerView fragment image not display using firebase

Image is not load to the fragment if i remove the Glide image code it's run fine
but adding image run with error
I think i get the error because of the fragment but i don't understand what is it
public void onBindViewHolder(AuthorListAdapter.MyViewHolder holder, int position) {
holder.name.setText(authorList.get(position).getName());
Glide.with(context).load(authorList.get(position).getImageUrl()).into(holder.image);
}
Here is the error:
java.lang.NullPointerException: You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:27)
at com.bumptech.glide.Glide.getRetriever(Glide.java:607)
at com.bumptech.glide.Glide.with(Glide.java:638)
at com.example.hardik.myapplication.AuthorListAdapter.onBindViewHolder(AuthorListAdapter.java:42)
at com.example.hardik.myapplication.AuthorListAdapter.onBindViewHolder(AuthorListAdapter.java:22)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6508)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6541)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5484)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5750)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5589)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5585)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2231)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:610)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3135)
at android.view.View.measure(View.java:19759)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
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:19759)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19759)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:19759)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:719)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:91)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1361)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:789)
at android.view.View.measure(View.java:19759)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1060)
at android.view.View.measure(View.java:19759)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19759)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
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:19759)
Any help appreciated
Thank You
you can see in your log you get activity or context is null.
If you want to know why your activity is getting empty, then you can follow this answer. https://stackoverflow.com/a/43748449/8588259
then you can follow these steps,
1) write onAttach(Activity activity) in your fragment
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity){
mActivity =(Activity) context;
}
}
2) pass this activity in your adapter
val adapter = MyAdapter(activity)
3) now you can write glide code
Glide.with(activity).load(imageUrl).into(view)
Try this
The issue itself occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed.
So check getActivity() !=null and bind your image view
if( getActivity()!= null ){
Glide.with(getActivity()).load(authorList.get(position).getImageUrl()).into(holde
r.image);
}
if( getActivity() != null ){
Glide.with(getActivity()).load(authorList.get(position).getImageUrl()).into(holde
r.image);
}

How to get rid of Index Out of Bounds Exception error while removing a row from RecyclerView [duplicate]

This question already has answers here:
Android - Custom ListView Adapter - Multi Selection remove - Indexoutofbounds - why?
(6 answers)
Closed 5 years ago.
I understand that how the error is being caused as there seems to be a problem with array indexing after removal of a row at random ,so how do go around to fix it.Any help will be appreciated.
Here is my Code
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<String> reminderMessage;
private ArrayList<String> receiverName;
public DataAdapter(ArrayList<String> reminderMessage,ArrayList<String> receiverName) {
this.reminderMessage = reminderMessage;
this.receiverName = receiverName;
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final DataAdapter.ViewHolder viewHolder, final int i) {
viewHolder.reminderText.setText(reminderMessage.get(i));
viewHolder.receiverName.setText(receiverName.get(i));
viewHolder.button_reject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rejectItem(viewHolder.getAdapterPosition());
}
});
}
#Override
public int getItemCount() {
return reminderMessage.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView reminderText;
private TextView receiverName;
Button button_reject;
public ViewHolder(View view) {
super(view);
reminderText = (TextView)view.findViewById(R.id.remindertext);
receiverName = (TextView)view.findViewById(R.id.receiverName);
button_reject=(Button)view.findViewById(R.id.button_reject);
}
}
private void rejectItem(int position) {
DatabaseReference mdatabase = FirebaseDatabase.getInstance().getReference();
try {
mdatabase.child("reminders").child(messageKeys.get(position)).child("status").setValue("reject");
Log.d("in the loop","writing");
} catch (Exception e) {
e.printStackTrace();
}
reminderMessage.remove(position);
receiverName.remove(position);
messageKeys.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, reminderMessage.size());
}
}
And here is the error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.import2, PID: 6891
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.admin.import2.DataAdapter.onBindViewHolder(DataAdapter.java:47)
at com.example.admin.import2.DataAdapter.onBindViewHolder(DataAdapter.java:27)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6279)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6312)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5258)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5521)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2969)
at android.view.View.measure(View.java:19734)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19734)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:19734)
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:19734)
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:139)
at android.view.View.measure(View.java:19734)
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:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19734)
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:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
at android.view.View.measure(View.java:19734)
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)
E/AndroidRuntime: 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)
E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/com.example.admin.import2-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.admin.import2-2/lib/x86, /system/lib, /vendor/lib]]
Application terminated.
Why don't you just use one list of some object like this?
class Data{
String name, message;
}
Plus
public DataAdapter(List<Data> data) {
this.data = data;
}
By using two lists, you are only using the size of one, while removing and getting from both, which only causes errors later on
Besides that, messageKeys is not defined in the question, so please debug that too
If you want to use Firebase, give FirebaseUI a chance

FragmentManager throws IllegalArgumentException in Kotlin

I have the following method in my Fragment
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val fm = childFragmentManager
var prefsFragment: PrefsFragment? = fm.findFragmentByTag(tableName) as PrefsFragment?
if (prefsFragment == null) {
prefsFragment = PrefsFragment()
val ft = fm.beginTransaction()
ft.add(R.id.parameters_fragment_container, prefsFragment, tableName)
ft.commit()
fm.executePendingTransactions()
}
}
I have to say this is what the Java->Kotlin automagic translator did with my previous Java code (save for the ? at the end of the line where I find the PrefsFragment by tag), so it may not be entirely kosher. I'm learning Kotlin by doing.
Now, when I call executePendingTransactions(), my app crashes throwing java.lang.IllegalArgumentException with error message "Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState". Now, yes, savedInstanceState is in fact null, but what does it have to do with the call that apparently throws the exception? I made sure that no other values/variables are null, so I'd trust the error message... but why?
EDIT Here is the stack trace, and the source code that's giving me nightmares
https://gist.github.com/Morpheu5/8ec7e2064f53679405769b43c64f2cb7
E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.morpheu5.bleedingscorecalculator, PID: 2685
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
at net.morpheu5.bleedingscorecalculator.CalculatorScreen$PrefsFragment.onCreatePreferences(CalculatorScreen.kt:0)
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:223)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2075)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1060)
at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838)
at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:861)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:594)
at net.morpheu5.bleedingscorecalculator.CalculatorScreen.onActivityCreated(CalculatorScreen.kt:34)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2096)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
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:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
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:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
at android.view.View.measure(View.java:19857)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
The exception isn't actually coming from that code. It is coming from your CalculatorScreen.PrefsFragment.onCreatePreferences() method. If you look there, it looks like --
override fun onCreatePreferences(savedInstanceState: Bundle, rootKey : String)
and it should be the following to allow savedInstanceState to be null --
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey : String)

Categories

Resources