I'm trying to get a LiveData Observer to work, and everything was working, but after a week my app keeps crashing when attaching the fragment to my activity with the live data. The fragment is very simple, it has a RecyclerView that displays a list of Bloggers in a grid. The ViewModel is the following:
class SimpleBloggersViewModel : ViewModel() {
var bloggers: MutableLiveData<List<Blogger>> = MutableLiveData()
}
And I initialize it in my fragment using:
private val viewModel: SimpleBloggersViewModel by lazy {
ViewModelProvider(this).get(SimpleBloggersViewModel::class.java)
}
The code that causes the crash is the following:
override fun onAttach(context: Context) {
super.onAttach(context)
try {
viewModel.bloggers.observe(viewLifecycleOwner, Observer { changeObserver })
} catch (e: IllegalStateException) {
Log.e(TAG, "onAttach: Failed to attach BloggersFragment.", e)
}
}
Without the try/catch in the onAttach method, I get the following cryptic exception (and an app crash)
Process: com.mydomain.appname, PID: 31393
java.lang.IllegalStateException: Already attached
at androidx.fragment.app.FragmentManager.attachController(FragmentManager.java:3000)
at androidx.fragment.app.Fragment.performAttach(Fragment.java:2622)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1291)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1264)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1147)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:120)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2369)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2327)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2230)
at androidx.fragment.app.FragmentManager$3.run(FragmentManager.java:414)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
After adding the try/catch block you see above, the exception ahs a little bit more info, but I still can't figure out what causes the problem.
java.lang.IllegalStateException: You can consumeRestoredStateForKey only after super.onCreate of corresponding component
at androidx.savedstate.SavedStateRegistry.consumeRestoredStateForKey(SavedStateRegistry.java:77)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:65)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:177)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:145)
at com.mydomain.appname.Bloggers.BloggersFragment$viewModel$2.invoke(BloggersFragment.kt:31)
at com.mydomain.appname.Bloggers.BloggersFragment$viewModel$2.invoke(BloggersFragment.kt:28)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.mydomain.appname.Bloggers.BloggersFragment.getViewModel(Unknown Source:7)
at com.mydomain.appname.Bloggers.BloggersFragment.onAttach(BloggersFragment.kt:90)
at androidx.fragment.app.Fragment.performAttach(Fragment.java:2638)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1291)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1264)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1147)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:120)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2369)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2327)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:2199)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)
at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:236)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
at android.view.View.measure(View.java:22238)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22238)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22238)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22238)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:743)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:815)
at android.view.View.measure(View.java:22238)
at androidx.drawerlayout.widget.DrawerLayout.onMeasure(DrawerLayout.java:1119)
at android.view.View.measure(View.java:22238)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:22238)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22238)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22238)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
2019-09-19 12:18:11.181 31963-31963/com.mydomain.appname E/BloggersFragment: at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22238)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:725)
at android.view.View.measure(View.java:22238)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2531)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1558)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1826)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1446)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7014)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:916)
at android.view.Choreographer.doCallbacks(Choreographer.java:728)
at android.view.Choreographer.doFrame(Choreographer.java:660)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:902)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Please note that I call super on all of the overriding methods, before doing anything else, so the second exception still doesn't help me figure the bug out. Any help is greatly appreciated. Thank you for your time.
Related
I'm getting an error in the title. Application I also have a mixed structure and use a few nested structures.
There is a structure like " CategoryFragment -> ViewPager -> RecyclerView inside CategoryDetailsFragment -> ViewPager-> HeadlinePagerFragment"
when changed to CategoryFragment then return to back will be this error.
CategoryFragment
val adapter = CategoriesPagerAdapter(activity?.supportFragmentManager!!,list as List<MutableMap<String,String>>)
vp_categories_pager.adapter = adapter
tl_categories.setupWithViewPager(vp_categories_pager)
CategoryDetailsFragment
This class use recyclerview and usage inside of childview viewpager.
rv_category_details.adapter = ContentAdapter(
context!!,this#CategoryDetailsFragment(usage as fragment),
this#CategoryDetailsFragment)
-> Adapter
vp_headline.adapter =HeadlineAdapter(
fragment?.childFragmentManager!!,
items, itemClickListener)
tl_headline_indicator.setupWithViewPager(vp_headline)
HeadlineAdapter
class HeadlineAdapter(
fragment: FragmentManager,
var list:MutableList<MutableMap<*,*>>,
val itemclickListener:ContentAdapter.Itemclicklistener)
:FragmentPagerAdapter(fragment){
override fun getItem(position: Int): Fragment =
HeadlinePagerFragment(PutData(mutableMapOf(
'm' to list[position],
'l' to itemclickListener
)))
override fun getCount(): Int = list.size
override fun getItemPosition(`object`: Any): Int {
return PagerAdapter.POSITION_NONE
}
}
Return this error
No view found for id 0x7f0901de (com.bursadabugun.android:id/vp_headline) for fragment HeadlinePagerFragment{17872d5 (85c9643d-c164-417e-a428-7105105970a1) id=0x7f0901de android:switcher:2131296734:0}
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:875)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2656)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2610)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2619)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:434)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2076)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1821)
at androidx.fragment.app.FragmentManagerImpl.execSingleAction(FragmentManagerImpl.java:1696)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:299)
at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:230)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
at android.view.View.measure(View.java:22094)
at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:22094)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22094)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22094)
at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:22094)
at androidx.drawerlayout.widget.DrawerLayout.onMeasure(DrawerLayout.java:1119)
at android.view.View.measure(View.java:22094)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:22094)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22094)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22094)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22094)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724)
at android.view.View.measure(View.java:22094)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2483)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1806)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1432)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6826)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6543)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
Found the solution
I added getItemId function CategoriesPagerAdapter
override fun getItemId(position: Int): Long = View.generateViewId().toLong()
I'm trying to set margin dynamically for a scroll view,
Surprisingly, my code is returning FrameLayout.layoutParam instead of ScrollView.LayoutParam here is my code, which is causing Class Cast Exception
ScrollView.LayoutParams scrollViewParam = new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.MATCH_PARENT);
scrollViewParam.setMargins(0, 0, 0, 500);
scrollView.setLayoutParams(scrollViewParam);
here is the stacktrace
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams
at android.support.constraint.ConstraintLayout.getViewWidget(ConstraintLayout.java:877)
at android.support.constraint.ConstraintLayout.setChildrenConstraints(ConstraintLayout.java:643)
at android.support.constraint.ConstraintLayout.updateHierarchy(ConstraintLayout.java:631)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:971)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22071)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6602)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:724)
at android.view.View.measure(View.java:22071)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2422)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1504)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
So, What is wrong with my code?
Every view has layout params associated with it's parent ViewGroup.
In your case, your ScrollView must live inside a ConstraintLayout (or a subclass of it). So, when you try to set it's layout params, it'll be expending an instance of a ConstraintLayout.LayoutParams.
Change your code with this and it should work just fine:
ConstraintLayout.LayoutParams scrollViewParam = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT);
scrollViewParam.setMargins(0, 0, 0, 500);
scrollView.setLayoutParams(scrollViewParam);
This crash has only been reproducible in Android 8/8.1 for me. This also only happens in obfuscated builds, unobfuscated builds are not affected. I have tried adding keep rules for the entire codebase and all dependencies and it does not fix the crash, but -dontobfuscate does.
On first launch, the app works fine and when you click a spinner, it pops up the SelectPopupAdapter dialog. On second launch, the same spinner will crash the app if selected. Clearing the app data makes the spinner work again.
The class is a fairly simple webview:
public class WebViewActivity {
#BindView(R.id.web_view)
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
ButterKnife.bind(this);
setupWebSettings();
webView.setWebViewClient(new CustomWebview());
webView.postUrl(url, postData.getBytes());
webView.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}
return false;
});
}
private void setupWebSettings() {
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
}
#Override
public void onDialogPositiveClick(DialogFragment fragment) {
fragment.dismiss();
finish();
}
#Override
public void finish() {
setResult(RESULT_OK);
super.finish();
}
private class CustomWebview extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//logging
}
}
}
Here is the crash log:
Crashed: main: 0 0 0x0000000000000000
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:195)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2133)
at android.content.res.Resources.getLayout(Resources.java:1142)
at com.guardsquare.dexguard.CV.getLayout(Unknown Source:94)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:416)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
at org.chromium.content.browser.input.SelectPopupAdapter.getView(SelectPopupAdapter.java:13)
at android.widget.AbsListView.obtainView(AbsListView.java:2372)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1408)
at android.widget.ListView.onMeasure(ListView.java:1315)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21998)
at com.android.internal.widget.AlertDialogLayout.tryOnMeasure(AlertDialogLayout.java:144)
at com.android.internal.widget.AlertDialogLayout.onMeasure(AlertDialogLayout.java:69)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
at android.view.View.measure(View.java:21998)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1471)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
--
Fatal Exception: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:195)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2133)
at android.content.res.Resources.getLayout(Resources.java:1142)
at com.guardsquare.dexguard.CV.getLayout(Unknown Source:94)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:416)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
at org.chromium.content.browser.input.SelectPopupAdapter.getView(SelectPopupAdapter.java:13)
at android.widget.AbsListView.obtainView(AbsListView.java:2372)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1408)
at android.widget.ListView.onMeasure(ListView.java:1315)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21998)
at com.android.internal.widget.AlertDialogLayout.tryOnMeasure(AlertDialogLayout.java:144)
at com.android.internal.widget.AlertDialogLayout.onMeasure(AlertDialogLayout.java:69)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21998)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
at android.view.View.measure(View.java:21998)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1471)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
I've written a custom view for displaying events in a 24 hour vertical scroll timeline.
In the onCreateView method in the fragment that inflates this view, I am inflating the view and then trying to set callbacks which are exposed by the view. However I am getting a null reference exception when trying to set a property on the custom view (i.e. the custom view is null).
Here is my layout (with my custom DayView inside the constraint layout):
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".application.home.HomeActivity">
<com.condecosoftware.core.dayview.DayView
android:id="#+id/dayView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:timeColumnWidth="52dp"
app:timeColumnPadding="16dp"
app:hourHeight="96dp"
app:textSize="14sp"
app:eventNameTextSize="18sp"
app:hourSeparatorHeight="1dp"
app:timeTextColor="#color/slateGrey"
app:timeColumnBGColor="#color/white_2"
app:hourSeparatorColor="#color/white_2"
app:nowLineColor="#color/lipstick"
app:eventColumnBGColor="#android:color/white"
app:eventBGColor="#color/brightSkyBlue"
app:eventCornerRadius="3.8dp"
app:eventTextColor="#android:color/white"/>
</android.support.constraint.ConstraintLayout>
</layout>
Now, I should note at this point, that the view IS inflating and being displayed correctly (if I remove the assignments to the callback in onCreateView).
Here is the code that is causing the NPE (bizzarly, Kotlin does NOT think that the variable I am trying to access can be null (I don't need to do safe cast or anything to get it to compile)):
#SuppressLint("MissingSuperCall")
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val viewBinding = FragmentBookingsTodayBinding.inflate(inflater ?: LayoutInflater.from(context))
// Setup day view listeners
viewBinding.dayView.eventClickListener = this
viewBinding.dayView.eventLongPressListener = this
viewBinding.dayView.dayViewCallbacks = this
this.viewBinding = viewBinding
return viewBinding.root
}
I am aware I am supressing the super call on that method, but as you can see I am returning the viewbinding root at the bottom so it's ok.
Does anyone have any ideas?
EDIT (Stack Trace):
FATAL EXCEPTION: main
Process: com.condecosoftware.roombooking.develop, PID: 3949
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.condecosoftware.core.dayview.DayView.setEventClickListener(com.condecosoftware.core.dayview.DayView$EventClickListener)' on a null object reference
at com.condecosoftware.roombooking.application.home.FragmentBookingsToday.onCreateView(FragmentBookingsToday.kt:53)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1236)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:22002)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1028)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
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:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
at android.view.View.measure(View.java:22002)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
10-21 14:05:30.514 3949-3949/com.condecosoftware.roombooking.develop E/AndroidRuntime:
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
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)