From Portrait to Landscape mode issue - java

I'm having issue with the rotation from portrait to landscape mode on my app that I'm working on right now. The app lunches ok but when i rotate my phone or emulator I'm experiencing the problem. [
This is the error that I get when rotate from one mode to another.
Failed to inflate ColorStateList, leaving it to the framework
java.lang.RuntimeException: Failed to resolve attribute at index 0
at android.content.res.TypedArray.getColor(TypedArray.java:402)
at android.support.v7.content.res.AppCompatColorStateListInflater.inflate(AppCompatColorStateListInflater.java:114)
at android.support.v7.content.res.AppCompatColorStateListInflater.createFromXmlInner(AppCompatColorStateListInflater.java:88)
at android.support.v7.content.res.AppCompatColorStateListInflater.createFromXml(AppCompatColorStateListInflater.java:67)
at android.support.v7.content.res.AppCompatResources.inflateColorStateList(AppCompatResources.java:116)
at android.support.v7.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:74)
at android.support.v7.widget.TintTypedArray.getColorStateList(TintTypedArray.java:170)
at android.support.v7.widget.AppCompatTextHelper.onSetTextAppearance(AppCompatTextHelper.java:296)
at android.support.v7.widget.AppCompatTextView.setTextAppearance(AppCompatTextView.java:162)
at android.support.v4.widget.TextViewCompat$TextViewCompatBaseImpl.setTextAppearance(TextViewCompat.java:155)
at android.support.v4.widget.TextViewCompat.setTextAppearance(TextViewCompat.java:476)
at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:634)
at com.jovan.matetracker.LoginActivity$2.onTextChanged(LoginActivity.java:68)
at android.widget.TextView.sendOnTextChanged(TextView.java:8126)
at android.widget.TextView.setText(TextView.java:4323)
at android.widget.TextView.setText(TextView.java:4169)
at android.widget.EditText.setText(EditText.java:85)
at android.widget.TextView.setText(TextView.java:4144)
at android.widget.TextView.onRestoreInstanceState(TextView.java:4044)
at android.view.View.dispatchRestoreInstanceState(View.java:14141)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3089)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3089)
at android.support.design.widget.TextInputLayout.dispatchRestoreInstanceState(TextInputLayout.java:1040)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3089)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3089)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3089)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3089)
at android.view.View.restoreHierarchyState(View.java:14119)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2220)
at android.app.Activity.onRestoreInstanceState(Activity.java:1086)
at android.app.Activity.performRestoreInstanceState(Activity.java:1027)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2668)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4378)
at android.app.ActivityThread.access$1000(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1436)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

This problem may be occuring when there is a change in state in the Activity lifecycle. When you change the orientation, landscape to portrait or vise versa, the Activity is destroyed and created again.
More specifically, when orientation change occurs, the method onDestroy() is called.
Take a look at the methods where your Layout Inflater is being manipulated/receiving the reference.

Related

"java.lang.IllegalStateException: Cannot perform this action on a sealed instance." FATAL EXCEPTION: main

I am getting this error on pre-launch report from playstore console.
I am not able to generate this crash on any real device I try.
The device from pre-launch report is Samsung s9 (Android 8.0).
This is the stacktrace :
FATAL EXCEPTION: main
java.lang.IllegalStateException: Cannot perform this action on a sealed instance.
at android.view.accessibility.AccessibilityNodeInfo.enforceNotSealed(AccessibilityNodeInfo.java:2992)
at android.view.accessibility.AccessibilityNodeInfo.setSource(AccessibilityNodeInfo.java:818)
at android.view.accessibility.AccessibilityNodeInfo.setSource(AccessibilityNodeInfo.java:795)
at android.view.accessibility.AccessibilityNodeInfo.obtain(AccessibilityNodeInfo.java:3008)
at android.view.View.createAccessibilityNodeInfoInternal(View.java:7897)
at android.view.View.createAccessibilityNodeInfo(View.java:7883)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1051)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchAccessibilityNodeInfos(AccessibilityInteractionController.java:876)
at android.view.AccessibilityInteractionController.findAccessibilityNodeInfoByAccessibilityIdUiThread(AccessibilityInteractionController.java:199)
at android.view.AccessibilityInteractionController.-wrap1(Unknown Source:0)
at android.view.AccessibilityInteractionController$PrivateHandler.handleMessage(AccessibilityInteractionController.java:1225)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Without seeing more code, I'd recommend looking through the following links to try and determine an exact cause.
https://stackoverflow.com/a/57964014/5779200
When updating values for AccessibilityNodeInfo it has been seen that calling .getClassName() to see if the value is NULL can help prevent this error.
https://stackoverflow.com/a/32829963/5779200
Similar to the previous, If you are changing the text of an accessibilityNodeInfo you can check to see if it's sealed by calling isSealed(). on the AccessibilityNodeInfo object

Android app crashed when change orientation

When the orientation of app change this error is occur kindly help me
W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.explore_kpk, PID: 24892
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.explore_kpk/com.example.explore_kpk.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5389)
at android.app.ActivityThread.access$1200(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
***Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference***
***at com.example.explore_kpk.MainActivity.onCreate(MainActivity.java:121)
at android.app.Activity.performCreate(Activity.java:6877)***
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5389) 
at android.app.ActivityThread.access$1200(ActivityThread.java:223) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7231) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
I/Process: Sending signal. PID: 24892 SIG: 9
This is code in which error pointing**
at com.example.explore_kpk.MainActivity.onCreate(MainActivity.java:121)
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recyler_view);
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this, recyclerList);
long seed = System.nanoTime();
Collections.shuffle(recyclerList, new Random(seed));
GridLayoutManager gridLayoutManager=new GridLayoutManager(this, mNoOfColumns);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(recyclerViewAdapter);
Your exception is saying that you are trying to use setLayoutManager on null object reference. This means that your recylcerView is null at the moment. I don't see your whole code so I can't give you an instant solution. What I can say for you is that each time the screen orientation is changed your current activity will be destroyed and called again. So, onCreate will be called again and everything else like you just started new Activity. Sometimes issues happen here so there is few options on how to handle these events.
Retaining an object during a configuration change
What this means is for you to keep your data while screen rotation is happening. This can be done with Bundle but if you have a large set of data then it is better to use the ViewModel object. How to do this check here:
Retaining an object during a configuration change | Android Developers
ViewModel Guide
Helpful answer on SO
Handling the configuration change yourself
This other option comes with big CAUTION. Consider this as a last resort when you really need to avoid restarts because of a configuration change. What this does is preventing your activity from the restart. If you can't save your instance then I would recommend doing this but first, try to play with ViewModel and Bundles to fix this issue.
Handling the configuration change yourself | Documentation
How to do this?
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|screenLayout" />
add android:configChanges line to your AndroidManifest.xml inside your <activity/> tag.

Can an activity loose its intent extras?

The app crashes in setParametersFromIntent which is called in onCreate of the activity. The method throws a custom RumtimeException if a specific intent extra is not set.
The crash is only reported rarely, I was not able to reproduce the bug.
I checked the code and there seems to be no possible route the activity could be started without the parameter being set.
Caused by java.lang.RuntimeException: intent extra not set
at com.myapp.myapp.activities.MyActivity.setParametersFromIntent(MyActivity.java)
at com.myapp.myapp.activities.MyActivity.onCreate(MyActivity.java)
at android.app.Activity.performCreate(Activity.java:5442)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(NativeStart.java)
I read that upon recreation of an activity, the intent extras are also restored, so I rule that out as a possible cause.
Does the log give any hints about how the activity has been started?
The android.app.Instrumentation.callActivityOnCreate also seems suspicious, as instrumentation should be disabled in the released app. Could this be someone trying to analyze and mess around with the activity, causing this exception?

Failed to allocate memory from loading jpg files - Android studio Erro

I have a function in my app that adds offline pictures that are stored inside the app's memory to a listview.
The code is extremely simple as you can see:
if(FirstTag != null) {
if (FirstTag.equals("care"))
imageView.setImageDrawable(context.getApplicationContext().getResources().getDrawable(R.drawable.care));
else if (FirstTag.equals("holidays"))
imageView.setImageDrawable(context.getApplicationContext().getResources().getDrawable(R.drawable.holidays));
else if (FirstTag.equals("Thing Different"))
imageView.setImageDrawable(context.getApplicationContext().getResources().getDrawable(R.drawable.thinkdifferent2));
else if (FirstTag.equals("Fun Activities"))
imageView.setImageDrawable(context.getApplicationContext().getResources().getDrawable(R.drawable.fun));
else if (FirstTag.equals("מערך חברות")) }
it simply checks a value in a json object and put a picture in the listview by this value, of course there are 20 values and not 5, but I didn't think I should Include all...
All the pictures are compressed, and when I don't load many of them the app doesn't crash.
But when I try to launch the app with a picture per each item in the listview, everything seems to work perfectly fine, and then when you scroll down the app lags and in a second it crashes with the following error:
9-30 18:35:18.299 7859-7859/com.example.negev.peulibraryv201 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.negev.peulibraryv201, PID: 7859
java.lang.OutOfMemoryError: Failed to allocate a 681099532 byte allocation with 16777216 free bytes and 261MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621)
at android.content.res.Resources.getDrawable(Resources.java:1640)
at android.content.res.Resources.getDrawable(Resources.java:1611)
at com.example.negev.peulibraryv201.GamesLibraryAdapter.getView(GamesLibraryAdapter.java:70)
at android.widget.AbsListView.obtainView(AbsListView.java:3238)
at android.widget.ListView.makeAndAddView(ListView.java:2147)
at android.widget.ListView.fillDown(ListView.java:767)
at android.widget.ListView.fillGap(ListView.java:731)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:8292)
at android.widget.ListView.trackMotionScroll(ListView.java:2065)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:7719)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
at android.view.Choreographer.doCallbacks(Choreographer.java:702)
at android.view.Choreographer.doFrame(Choreographer.java:635)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
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:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
I already added those line to my manifest:
android:hardwareAccelerated="false"
android:largeHeap="true"
and it didn't work, wht do you think I can do?
Thank you!
What you should do is load the data dynamically as it becomes visible on the screen.
Since this is hard work Google already did it for you.
You need to use an ArrayAdapter with the list view and let Andriod ask you for more data when it is needed.
Its too much code to explain in detail but have a look at this Goolge I/O video.
There are many tutorials on the web that explains how to do that in detail.
Have a look here.

Android Studio - Application has stopped, but 0 code errors

My android application was running smoothly before i install some Android Studio updates, and asked me to upgrade gradle etc after the update been installed.
Later on, the Android Studio asked me to update my Android Emulator (Emulator Nexus 5X API 24 Android v7.0), since i've made all these changes, my application has been unable to open when i click run. But remember it has no codding errors when i debug etc.
The error message from Android Monitor is:
08-13 14:07:30.735 3889-3889/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smartlegal.law.freeman.smartlegal, PID: 3889
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartlegal.law.freeman.smartlegal/com.smartlegal.law.freeman.smartlegal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.smartlegal.law.freeman.smartlegal.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
08-13 14:07:30.744 3889-3896/? W/art: Suspending all threads took: 5.874ms
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
As the error states, you're trying to set an onClickListener on a Button that does not exist (returns null). To simplify, when you're defining your Button, you call findViewById to associate the Button in Java with an XML element, but the ID that you are passing is not valid - either you're trying to call the setOnClickListener before you call setContentView, or the ID you are passing to findViewById has a typo or simply does not exist in the XML layout file.
Some more information, such as the code to your MainActivity and the layout file, would be useful to determine the exact cause of your problem.
Yes you're right, i forgot to specify the ID for the editText and Button, that is why it wasnt possible to find them ID on XML. So i've set it up and reloaded the project and it is running smoothly now. Thanks guys for the support.
"As the error states, you're trying to set an onClickListener on a Button that does not exist (returns null). To simplify, when you're defining your Button, you call findViewById to associate the Button in Java with an XML element, but the ID that you are passing is not valid. by: Timothy Langer".

Categories

Resources