Android app crashed when change orientation - java

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.

Related

Android application, uploading profile picture not functioning [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed last year.
I am developing an android application which requires me the user to sign up and then set profile picture and user name. the registration is working properly and sending the data to Firebase but when it comes to uploading the profile photo, the application reading the gallery and picks the image but does not send the image to the Firebase neither does it set it in the image view. below is the error i get upon examination since the application terminates at that point.
E**/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chatme, PID: 23039
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=123, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/content://media/external/images/media/211575/ORIGINAL/NONE/image/jpeg/79264842 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F211575/ORIGINAL/NONE/image%2Fjpeg/79264842} }} to activity {com.example.chatme/com.example.chatme.profile}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4309)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4353)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1678)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
at com.example.chatme.profile.onActivityResult(profile.java:108)
at android.app.Activity.dispatchActivityResult(Activity.java:7311)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4305)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4353) 
at android.app.ActivityThread.-wrap20(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1678) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6548) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)** 
The error is occurring because the image view in which you setting the image, is null. Did you initialise it? If it is not initialised, it will crash because java will take it as null.setImageUri() but you cannot do anything on null. So initialise the image view in order to get safe from the error

From Portrait to Landscape mode issue

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.

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".

Android app crashes with java.lang.NoClassDefFoundError

I have an application that creates an Intent when selecting item menu, but the first time the user clicks this button, the app crashes with the following stack trace.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.systemsat.monitriip, PID: 17818
java.lang.NoClassDefFoundError: br.com.systemsat.monitriip.activity.ConfigurationActivity
at br.com.systemsat.monitriip.activity.MainActivity.onOptionsItemSelected(MainActivity.java:212)
at android.app.Activity.onMenuItemSelected(Activity.java:2912)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:421)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:188)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:663)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:947)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:155)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Below is my Activity declaration inside AndroidManifest.xml:
<activity
android:name=".activity.ConfigurationActivity"
android:parentActivityName=".activity.MainActivity">
</activity>
Also, below is the line that is accused to be the cause of the problem by the call stack:
Intent hashCodeIntent = new Intent(getApplicationContext(), ConfigurationActivity.class);
Some answers found in StackOverflow pointed me that clean and rebuild would do the trick but I've tried without any luck. I have also tried adding MultiDex to gradle file as well as to my custom Application class, but that also resulted in a no go.
Could someone else have any insights as to what I might have done wrong? What is really intriguing is that this error only happens on the first time the user clicks the "Configuration" screen button. After the app crashes, the user can reopen it and click the button normally.
Thank you in advance.
UPDATE
I have forgot to mention that Android Monitor also logs the following info right before the crash:
Rejecting re-init on previously-failed class java.lang.Class<br.com.systemsat.monitriip.activity.ConfigurationActivity>
UPDATE 2
Here is my onOptionsItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_config:
Intent hashCodeIntent = new Intent(this, ConfigurationActivity.class);
hashCodeIntent.putExtra("isConfigurationCall", true);
startActivity(hashCodeIntent);
break;
}
return true;
}
UPDATE 3
I have just generated an apk for testing purposes and the error did not show. Maybe it has something to do with debuging? Or even something to do with gradle version? I currently use the last version of gradle.
Try to change getAplicationContext() for getActivity()
Sometimes getAplicationContext returns null.

Starting Android activity gives NullPointerException

I have an activity, LookingForGPS, that starts another activity, Run. After starting Run, LookingForGPS doesn't finish, but rather continually updates some TextViews in Run:
Run run = new Run();
if(runHasBeenStarted)run.getAndSetValues(someParameters, this);
To my understanding, this returns the LookingForGPS activity. However, after a while (when the parameters are a certain value) I want to start a new activity, PostRun. The following method is called from the Run instance's getAndSetValues:
private void killEverythingAndProceed(Context context){
Intent finishRun = new Intent(context, PostRun.class);
//putting some extras into the intent
startActivity(finishRun);
}
I then get a NullPointerException at the line of startActivity:
11-17 08:34:33.647 2472-2472/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: lv.rv1g.kj0112.forward, PID: 2472
java.lang.NullPointerException
at android.app.Activity.startActivityForResult(Activity.java:3424)
at android.app.Activity.startActivityForResult(Activity.java:3385)
at android.app.Activity.startActivity(Activity.java:3627)
at android.app.Activity.startActivity(Activity.java:3595)
at lv.rv1g.kj0112.forward.Run.killEverythingAndProceed(Run.java:143)
at lv.rv1g.kj0112.forward.Run.getAndSetValues(Run.java:88)
at lv.rv1g.kj0112.forward.LookingForGPS.onLocationChanged(LookingForGPS.java:100)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
What am I doing horribly wrong? I guess it's my lack of knowledge about object-oriented programming that's causing this. It's not the extras causing it, and the context should be correct as well, as it works for another method from the same instance.
Edit: detailed explanation
The LookingForGPS activity has a LocationManager (not using a service because it has some limitations that are too complicated for me to work around). Each time onLocationChanged is called, the Run activity is instantiated (I check whether it has actually been started first, it gets started by the user independetly of the LocationManager), and the getAndSetValues method inside Run is called, giving a bunch of parameters. getAndSetValues then performs some calculations and updates some TextViews in the Run activity (which all works fine). A calculation is also performed to see whether a certain condition has been met. If it has been, the postRun activity is supposed to be started, which is when I run into the exception above.
Here's the problem:
Run run = new Run();
Looks like Run is an Activity. Never instantiate activities with new - you cannot use activities instantiated such way for anything you'd want to use an activity for. For example, to be used as a Context for startActivity().
Either use an Activity instance that has been set up by the system for you, or use an Intent to launch a new activity instance.

Categories

Resources