Android App error while the app is running - java

I am not able to figure out what is causing this error while the app is running. It causes the app to stop. Is it a wrong practice to sleep the main thread to finish execution of the asynchronous thread. If so why and what is a good practice?
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4707)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22295)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4702)
at android.view.View.performClick(View.java:5619) 
at android.view.View$PerformClick.run(View.java:22295) 
at android.os.Handler.handleCallback(Handler.java:754) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:163) 
at android.app.ActivityThread.main(ActivityThread.java:6342) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.sairahul5223.e_bot.Main4Activity.onButtonClickName43(Main4Activity.java:330)
at java.lang.reflect.Method.invoke(Native Method) 
at android.view.View$DeclaredOnClickListener.onClick(View.java:4702) 
at android.view.View.performClick(View.java:5619) 
at android.view.View$PerformClick.run(View.java:22295) 
at android.os.Handler.handleCallback(Handler.java:754) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:163) 
at android.app.ActivityThread.main(ActivityThread.java:6342) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) 

First thing: we don't make the main thread sleep because this will cause an Application Not Responding (ANR) error.
To get the result of an asynchronous task, we normally use a callback to determine the task's status. Take a look at this answer to give you idea: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

Related

My Activity is getting null object exception when the app resumes after bing in the background

I have an android app that has a second module included in the app when the main activity starts an activity in the module everything is good. The problem is if you go to another app and then resume this app the activity crashes with a null object pointer. My app uses Esri ArcGIS runtime SDK. I have a MapView open in the activity when the crash occurs. I don't have any problem with an open map in the MainActivity and the app goes to the background then back to the current app.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.thevillages.myfirstapp, PID: 23267
java.lang.RuntimeException: Unable to resume activity {com.thevillages.myfirstapp/com.thevillages.maplib.MapNavActivity}: com.esri.arcgisruntime.ArcGISRuntimeException: Null pointer.: object cannot be null.
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4205)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: com.esri.arcgisruntime.ArcGISRuntimeException: Null pointer.: object cannot be null.
at com.esri.arcgisruntime.internal.jni.CoreGeoView.nativeResume(Native Method)
at com.esri.arcgisruntime.internal.jni.CoreGeoView.b(SourceFile:5)
at com.esri.arcgisruntime.a.i.f.g.d(SourceFile:3)
at com.esri.arcgisruntime.mapping.view.GeoView$RenderingThread.resume(SourceFile:4)
at com.esri.arcgisruntime.mapping.view.MapView.resume(SourceFile:1)
at com.thevillages.maplib.MapNavActivity.onResume(MapNavActivity.java:816)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1446)
at android.app.Activity.performResume(Activity.java:7939)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4195)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237) 
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I systematically went through my onCreate() method and added each piece of the code to find where the problem was. I found some code that was suspicious and I could not recall why I added the code. With the code removed the app works fine.

Exception when creating new instance of OkHttp3.OkHttpClient

I'm trying to use OkHttp to download a file from a webserver in an Android app. After creating a new Project with Android Studio (API level 28) I added a button and am ImageView to my Activity. The idea is to download the an image when I press the button and show it on the screen.
I'm using the latest version of OkHttp, 3.14.0.
I added <uses-permission android:name="android.permission.INTERNET"/> to the AndroidManifest.xml file.
I tried to use the OkHttpClient exactly as described in the examples. This is what my code looks like:
public class MainActivity extends AppCompatActivity {
... // onCreate method
public void btnClicked(View view) {
new DownloadImageAsyncTask().execute("https://link.to/image.jpg");
}
public class DownloadImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
OkHttpClient client = new OkHttpClient();
... // override doInBackground()
}
}
When l run the app and press the button, the btnClicked() method is executed and the app crashes. The error occurs when calling OkHttpClient() no matter if I place it as a field in the DownloadImageAsyncTask class, as a local variable of the doInBackground() method or as a field in the ManActivity class.
I've also ried to use the Builder (new OkHttpClient.Builder().build()) to initialize the object but with the same outcome.
Here's the stack trace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.httpstuff, PID: 17796
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Caused by: java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method
at okhttp3.internal.Util.<clinit>(Util.java:87)
at okhttp3.internal.Util.immutableList(Util.java:234)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124)
at com.myapp.httpstuff.MainActivity$DownloadImageAsyncTask.<init>(MainActivity.java:31)
at com.myapp.httpstuff.MainActivity.btnClicked(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at okhttp3.internal.Util.<clinit>(Util.java:87) 
at okhttp3.internal.Util.immutableList(Util.java:234) 
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124) 
at com.myapp.httpstuff.MainActivity$DownloadImageAsyncTask.<init>(MainActivity.java:31) 
at com.myapp.httpstuff.MainActivity.btnClicked(MainActivity.java:26) 
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
OkHttp 3.13+ requires Java 8+ or Android 5+. I think you've encountered a situation where a Java 8 lambda is not expected by the ART runtime.
https://android.googlesource.com/platform/art/+/master/runtime/interpreter/interpreter_common.cc#1399
Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
if (UNLIKELY(object.IsNull())) {
// This will typically be for LambdaMetafactory which is not supported.
ThrowClassCastException("Bootstrap method returned null");
return nullptr;
}
You'll probably have success updating your build.gradle.
android {
compileOptions {
targetCompatibility = "8"
sourceCompatibility = "8"
}
}
https://github.com/square/okhttp/issues/4597
After trying everythin that came to my mind I tried using an older version of OkHttp.
Using OkHttp:2.7.2 instead, my code works as expected.
I just tested Okhttp3:3.12.2 as well and this fixes the issue as well.
Interestingly OkHttp3:3.12.2 was released a day after OkHttp3:3.14.0 (https://github.com/square/okhttp/releases)
Here is the answer why it works with the Okhttp3:3.12.2:
Google’s distribution dashboard shows that ~11% of the devices that visited the Play Store in October 2018 were running Android 4.x. We’ve created a branch, OkHttp 3.12.x, to support these devices. Should we encounter any severe bugs or security problems we’ll backport the fixes and release. We plan to maintain this branch through December 31, 2020.
Source: https://medium.com/square-corner-blog/okhttp-3-13-requires-android-5-818bb78d07ce

How to add Internet Connection Problem Dialog in android app on Async Task

I want to add Internet Connection Problem Dialog in my Async Task subclass. I tried many methods but the same error happened when my internet not connected.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.yasht.cricketapp, PID: 12793
java.lang.ClassCastException: java.lang.String cannot be cast to java.io.InputStream
at com.example.yasht.cricketapp.m_Rss.Downloader.onPostExecute(Downloader.java:66)
at android.os.AsyncTask.finish(AsyncTask.java:667)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Error Pointing On 66 line:
Please comment If you want any other information.
You are casting data into inputStream, if you want to create this object you should do something like : ByteArrayInputStream( data.getBytes() )
probably you should post your onExecute too to see what are you trying to do

getFloatArray is not receiving array

I'm working with a list:
List<Float> subjects = Arrays.asList(0f,0f,0f,0f,0f,0f);
List<Float> question9 = Arrays.asList(0f,0f);
And I want to put send it to another activity using Intent like this:
Intent iQuizz= new Intent(BachelorsResults.this, Results.class);
iQuizz.putExtra(BACHELOR, spinner.getSelectedItem().toString());
iQuizz.putExtra(QUESTION_9, question9.toArray());
iQuizz.putExtra(FAILED_SUBJECTS, subjects.toArray());
BachelorsResults.this.startActivity(iQuizz);
In the another activity I'm trying to receive float values:
Bundle extras = getIntent().getExtras();
float[] q9= extras.getFloatArray(QUESTION_9);
float[] fSubjects= extras.getFloatArray(FAILED_SUBJECTS);
But once I start this second activiy it launchs this Exception:
3-05 15:30:28.221 24640-24640/citrapp.exatec E/AndroidRuntime: FATAL EXCEPTION: main
Process: citrapp.exatec, PID: 24640
java.lang.RuntimeException: Unable to start activity ComponentInfo{citrapp.exatec/citrapp.exatec.Resultados}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at citrapp.exatec.Resultados.onCreate(Resultados.java:85)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
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) 
Somehow when I pass them to the another activity, float arrays become nulls, because I test extras.getFloatArray inside the first activity and it shows the content, but in the second activity it doesn't. If somebody could help me would be awesome..
Thanks!
Try replacing getIntent().getExtras() with getIntent().getFloatArrayExtra().

android - Short sounds on MediaPlayer throw NullPointerException

I have encountered a problem in which when ever I use
final MediaPlayer tick = MediaPlayer.create(getApplicationContext(), R.raw.tick);
tick.start();// tick is a short .mp3 file
in my app, no matter where, I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at com.a.aar.MainActivity.onClicks(MainActivity.java:123)
at com.a.aar.MainActivity$2.onClick(MainActivity.java:97)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
But, whenever I use a slightly shorter sound effect, the app runs great.(I need a short sound because I want a lot of sounds coming fast and so the sounds will not stop)
What is the reason to this error and how can I solve it?

Categories