I am trying to use BackgroundMode.disableWebViewOptimizations(); in my ionic app but when i launch the app in the android studio then i am getting this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.siloc.users, PID: 6275
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View org.apache.cordova.CordovaWebViewEngine.getView()' on a null object reference
at de.appplant.cordova.plugin.background.BackgroundModeExt$1.lambda$run$0$BackgroundModeExt$1(BackgroundModeExt.java:169)
at de.appplant.cordova.plugin.background.-$$Lambda$BackgroundModeExt$1$ENae6xHnDD7pQOdOSwld6cT0QNo.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:230)
at android.app.ActivityThread.main(ActivityThread.java:7880)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:526)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
E/NetdClient: querySockNetidForUid nid:0, uid:10357, command.cmdId:11, id:0
This is the function in BackgroundModeExt.java file
private void disableWebViewOptimizations() {
Thread thread = new Thread(){
public void run() {
try {
Thread.sleep(1000);
getApp().runOnUiThread(() -> {
View view = webView.getEngine().getView();
try {
Class.forName("org.crosswalk.engine.XWalkCordovaView")
.getMethod("onShow")
.invoke(view);
} catch (Exception e){
view.dispatchWindowVisibilityChanged(View.VISIBLE);
}
});
} catch (InterruptedException e) {
// do nothing
}
}
};
thread.start();
}
iS there any way i can solve this problem?
Change line: View view = webView.getEngine().getView(); to View view = webView.getView();
Related
I am getting the following error in my android studio. Is there any way I can solve this issue. Its causing a lot of crashes in my app.
E/AndroidRuntime: FATAL EXCEPTION: Firebase-Messaging-Intent-Handle
Process: com.cnlcabs.cnldrivers, PID: 19224
java.lang.RuntimeException: Can't create handler inside thread Thread[Firebase-Messaging-Intent-Handle,5,main] that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:227)
at android.os.Handler.<init>(Handler.java:129)
at com.cnlcabs.cnldrivers.data.MyFirebaseMessagingService.stopPlayer(MyFirebaseMessagingService.java:264)
at com.cnlcabs.cnldrivers.data.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:233)
at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(com.google.firebase:firebase-messaging##21.1.0:13)
at com.google.firebase.messaging.FirebaseMessagingService.passMessageIntentToSdk(com.google.firebase:firebase-messaging##21.1.0:8)
at com.google.firebase.messaging.FirebaseMessagingService.handleMessageIntent(com.google.firebase:firebase-messaging##21.1.0:3)
at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(com.google.firebase:firebase-messaging##21.1.0:3)
at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0$EnhancedIntentService(com.google.firebase:firebase-messaging##21.1.0:1)
at com.google.firebase.messaging.EnhancedIntentService$$Lambda$0.run(Unknown Source:6)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement##17.5.0:6)
at java.lang.Thread.run(Thread.java:923)
my code for myfirebasemessagingservice line 264 is
private void stopPlayer() {
try {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (NotificationReceiverSoundPlay.mPlayer != null)
NotificationReceiverSoundPlay.mPlayer.stop();
}
}, 2000);
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
You need to run these lines in UI thread not in a separate thread. Like this answer.
new Handler().postDelayed(new Runnable() {
activity.runOnUiThread(new Runnable() {
public void run() {
if (NotificationReceiverSoundPlay.mPlayer != null)
NotificationReceiverSoundPlay.mPlayer.stop();
}
});
}, 2000);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
When i try to click on the button several time then i encountered this error.
Below are the error in logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.huiiy.myapplication, PID: 3933
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at com.example.huiiy.myapplication.Activity9$13.onClick(Activity9.java:288)
at android.view.View.performClick(View.java:5712)
at android.view.View$PerformClick.run(View.java:22514)
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:6141)
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)
Below are the coding of my application:
iv_33.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer ivbsound = new MediaPlayer();
ivbsound = MediaPlayer.create(Activity9.this,R.raw.ivbutton);
// MediaPlayer ivbsound = MediaPlayer.create(Activity9.this,R.raw.ivbutton);
ivbsound.start();
int theCard = Integer.parseInt((String) view.getTag());
doStuff(iv_33, theCard);
}
});
Hi try to create player with another audio file.
Maybe the ivbutton file is corrupt
[UPDATE]
If you are reusing it declare lazy property of this sound player and on every click play it
class MyActivity extends AppCompatActivity {
private MediaPlayer soundPlayer = null;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
soundPlayer = MediaPlayer.create(this, R.raw.ivbutton);
iv_33.setOnClickListener(view -> {
soundPlayer.start();
int theCard = Integer.parseInt((String) view.getTag());
doStuff(iv_33, theCard);
});
}
}
It means that the creation of your MediaPlayer failed (maybe you have warn/debug messages about it).
It's useless to create a new MediaPlayer and then replace it by a new one.
Check if R.raw.ivbutton exist and correspond to a good media (which can be runned).
Personnally, this is how I do:
File mediaFile = obj.getFile();
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaFile.setReadable(true); // be sure that the program have access to the media content
// this try/catch is to support multiple Android version
try {
mediaPlayer.setDataSource(mediaFile.getAbsolutePath());
} catch (Exception e) {
mediaPlayer.setDataSource(context, Uri.fromFile(mediaFile));
}
mediaPlayer.prepare(); // prepare it, after you can change volume ...
} catch (Exception e){
e.printStackTrace();
}
mediaPlayer.start(); // finally start the media
please make sure that your sound file format you try to play is supported one check list from here. as the MediaPlayer.create returns null in case of the file is not supported which cause you null pointer exception. besides i recommend to check if(ivbsound != null) before calling ivbsound.start();
I have the following class that implements runnable. When I start the thread I get an error preventing me from updating the image. I have tried setting the image as you will see in the runOnUiThread function but it doesn't work. I would like to ultimately update the image every x seconds but I cannot seem to do this unless I put the thread inside the main activity class. Once I move it out nothing I try works.
public class Test implements Runnable {
MainActivity activity;
ArrayList<Media> media = new ArrayList<Media>();
public Test(MainActivity activity){
this.activity = activity;
}
public void run(){
activity.runOnUiThread(new Runnable(){
#Override
public void run() {
ImageView img= (ImageView) activity.findViewById(R.id.imageView1);
img.setImageResource(R.drawable.ic_launcher_foreground);
}
});
}
}
In MainActivity.java's onCreate() function I start the thread.
Test p = new Test(this);
Thread t = new Thread(p);
t.start();
Then my error:
2019-10-31 19:26:47.274 5407-5407/com.desotomatrix.digitalsignageplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.desotomatrix.digitalsignageplayer, PID: 5407
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at com.desotomatrix.digitalsignageplayer.Test$1.run(Test.java:30)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:209)
at android.app.ActivityThread.main(ActivityThread.java:7021)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:486)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:872)
I'm having a problem with my MediaPlayer which I have set to play and pause with one button.
It plays on the first click on any position but after this nothing plays and it shows this in logcat:
java.lang.IllegalStateException
08-23 11:06:40.330 6482-9285/package.com W/System.err: at android.media.MediaPlayer.nativeSetDataSource(Native Method)
08-23 11:06:40.330 6482-9285/package.com W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1078)
08-23 11:06:40.330 6482-9285/package.com W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:103
I call this method within a getView method in my CustomListAdapter and the sounds are passed in by arrays through TabActivities:
public void setTheSounds(ImageView playPause, final int position){
try {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
if(!mp.isPlaying()){
mp.setDataSource(sound[+position]);
mp.prepare();
mp.start();
} else {
mp.pause();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("GGGGGG", "prepare() failed");
}
}
});
t.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Click Hear. I think it will help you to handle media player callbacks.
You need to reset Media player object before passing new value.
We're working in an android app with the 4 version and we need a mysql connection. We are doing this with a php file which returns the data base result. The following code are working because if we debug the project we can see the correct database result in the the System.out.println. line.
But, if we try to change one text in our app for the result of the database our app can’t run correctly and the error was in the t.setText(txt); line.
Can anybody help me with this problem? Do you have another idea to do the database connection or is it the correct way?
Thanks a lot.
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//FUNCIONA NO BORRAR
final httpHandler handler = new httpHandler();
Thread thread = new Thread()
{
#Override
public void run() {
try {
if(true) {
sleep(1000);
String txt = handler.post("http://www.golftipp.com/pruebas_android/app.php");
TextView t = (TextView) findViewById(R.id.textView2);
t.setText(txt);
System.out.println(txt);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
The error
05-30 02:42:50.331 1276-1291/es.softline.connexiodb.app E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-87
Process: es.softline.connexiodb.app, PID: 1276
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.requestLayout(View.java:16431)
at android.view.View.requestLayout(View.java:16431)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
at android.view.View.requestLayout(View.java:16431)
at android.widget.TextView.checkForRelayout(TextView.java:6600)
at android.widget.TextView.setText(TextView.java:3813)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.TextView.setText(TextView.java:3646)
at es.softline.connexiodb.app.MainActivity$1.run(MainActivity.java:33)
Non-UI threads cannot access views, use runOnUiThread():
Thread thread = new Thread()
{
#Override
public void run() {
try {
if(true) {
sleep(1000);
String txt = handler.post("http://www.golftipp.com/pruebas_android/app.php");
TextView t = (TextView) findViewById(R.id.textView2);
runOnUiThread(new Runnable() {
#Override
public void run() {
// do UI updates here
t.setText(txt);
}
});
System.out.println(txt);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();