Null Pointer Exception if changing toolbar icon on observe? Facing problem with kotlin - android

Having a filter Fragment A with custom toolbar which is used commonly for many fragments and activity for filter list on click of filter icon of toolbar opening Fragment B to set filter option and and observing filter option using ViewModel and Live data . and opening Fragment A from any where but some times filter icon getting updated according to selected filter and sometimes throwing null pointer for imageview . any suggestion and help most welcome and thanks in advance .
I had tried to remove observers but didn't worked .
Image view already present in xml .
Tried Delay using handler. but still getting same error
Expecting some better and helpful solution.

This is fixed now .
Problem was Observing data before loading fragment without null
"?" safety

Related

Showing diffrent data from API on different Tab Fragments

I have 3 tab fragments in Activity with viewpager , each fragment showing different list of data from the different API call.
Each fragment has different API call with progress dialog.
Now whenever i open Activity its calling first and second's fragment's onViewCreated . How to prevent this ?
I also tried with viewPager.setOffscreenPageLimit(0); . but minimum and default is 1 .
Also found some stack answers that indicates about Viewpager OnPageChangeListner and setUserVisibleHint but dont get it! I stuck at this point . How to prevent this ? Explain!
I got better answer : onResumeFragment() and onPauseFragment()
https://looksok.wordpress.com/2013/11/02/viewpager-with-detailed-fragment-lifecycle-onresumefragment-including-source-code/

How to update the UI in android

I have an activity A with a listview and upon selecting some items in the list, and navigating to another activity B with the selected items in the list of Activity A. And made some modifications. After return to A. My screen is not updating with the latest modifications.
Please guide me how to do this. Thank you in advance.
If you have unified data storage for both activities (so that you know that this problem is purely UI issue, because the data itself is updated upon returning from activity B), then you should call this method on your ListView adapter:
yourListView.getAdapter().notifyDataSetChanged()
In order to display what you want, you need to have some TextViews in your other activity and then just add a text to them to display what you need.
When you get the data, just do like this in the onCreate method:
TextView tv = (TextView)inflate.findViewById(R.id.myTextView);
tv.setText(theDataStringYouReceived);

Testing Activity with a Fragment with Robolectric

I want to check if the Activity I create in a test is showing a Fragment. How can I do this? I've tried searching through Stack Overflow and Google but I couldn't find anything.
You need to find the fragment by id with
activity.getFragmentManager.findFragmentById(R.id.your_fragment_container);
If this returns something different from null, then it means that your container has a Fragment.

Nullpointer exception on popBackStack

i am developing android application using fragments and sherlock action bar,
i am using the method
Blockquote
getSherlockActivity().getSupportFragmentManager().popBackStack();
for returning previous fragments.
Sometimes it run well,but sometime it gives NULL POINTER exception.
what will the solution ?
My guess is that there's nothing left to pop. Try checking to see if it exists before popping.
You should check if there is any fragment on your backstack before you try to pop it.
if( getSherlockActivity().getSupportFragmentManager().getBackStackEntryCount() > 0 )
getSherlockActivity().getSupportFragmentManager().popBackStack();
popBackStack() will not cause NullPointerException. I've created a piece of code and called it several times in a row - when there is nothing to pop. It just leaves current fragment visible and does nothing.
I think the problem is not is in popBackStack method, however, I wasn't using ABS, just a regular android fragments API's.
Could you provide a piece of code? Maybe the problem is elsewhere?
If there is no backstack it will cause the null pointer, I have an onMenuItemClickListener() that catches this with the following:
if(navController.getPreviousBackStackEntry() != null) {
navController.popBackStack();
}

Custom Intent-Filters in android

What i am currently try to achieve is:
Add a record in one activity and show it on the other activity.
And on the click of the edit button on the show activity it takes back user to the first page for editing the data.
The problem is that i cannot use Intent.putextra() for this because the add page is linked with other so it gives me error for this.
Thought of using shared preferences as well,but i don't think it would help me much, since if a value is set in shared preference it would be available on load of the activity so i wont be able to know from where i entered the activity. so thought of using intent-filters for it.
can anyone help me with this ?
And i dont want to create another class for edit purpose.
Why not check for...
<pre>
<code>
if (getIntent() != null && getIntent().getSerializableExtra("Id") != null)
{
//get the data here becuz it's not null here....
}
</code>
</pre>
also just note that getIntExtra() also exists...

Categories

Resources