I'm trying to implement google's barcode scanner within my application.
The following code calls the barcode activity:
public void onClick(View v) {
if (v.getId() == R.id.read_barcode) {
Intent intent = new Intent(this, BarcodeCaptureActivity.class);
intent.putExtra(BarcodeCaptureActivity.AutoFocus, autoFocus.isChecked());
intent.putExtra(BarcodeCaptureActivity.UseFlash, useFlash.isChecked());
startActivityForResult(intent, RC_BARCODE_CAPTURE);
}
}
It then jumps to this part of the Activity.java code:
It throws the flag Source code does not match byte code and returns to the calling function where it then crashes with an InflationException that points to an XML file.
On further inspection, many of the imports are red:
As a result, there are many error flags on Activity.java. The code still compiles and runs up until the OnClick method is hit. I have tried following this and this on SO to no success. TIA
You should use an Android emulator (or real device) with the same api level as the compileSdkVersion in your Gradle file.
Related
I have a Unity Scene built with Cardboard SDK and exported as a library for Android. The library is used to play videos in cardboard mode on the android app. it's not the whole app, but a part in it. The rest of the android app is built with Kotlin and Java.
I have implemented that and all the functions work as expected, but, exiting the scene crashes the android.
We tried various ways to clear player prefs and even clear memory before closing the scene. But on android it always crashes. I have two android phones with android 9 and 10 for testing.
In the android app, I have made it such that as soon as the app crashes, I try to recover. My crash is that some lateinit var variables are destroyed. Their value becomes null and recovering the previous activity crashes it. So right after I exit the unity scene, I load the dereferenced variables back into memory and everything works again.
Note: I have tried using Application.Quit(); in unity, but it just closes the whole app. On the other hand, I only want to close the running scene
In unity [I call a function goBack in android part to close the app]:
public void GoToHome()
{
Pause();
Stop();
Resources.UnloadUnusedAssets();
PlayerPrefs.DeleteAll();
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("goBack");
}
In App:
public void goBack()
{
UnityPlayer.currentActivity.finish();
finish();
loadDerereferencedVars();
}
This goes perfectly on android 9. On the other phone with android 10, after I close the scene, the app continues to function, but, there comes a message
When I click close app, the app continues to work.
I have checked the logs and there is a null pointer dereference cause for the crash in Unity Main >...
If you'd like to see the Unity Crash Log from LogCat in Android Studio
So, since the app is still running, I thought, it would be better to just hide the crash report and just let the user not know about this crash, but still report it.
I tried enclosing my app in Application and added a method to catch uncaughtException.
here is my application class:
public class MyApp extends Application {
private static final String TAG = "MyAPP";
#Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException (Thread thread, Throwable e) {
handleUncaughtException (thread, e);
}
});
}
/**
* Handles Uncaught Exceptions
*/
private void handleUncaughtException (Thread thread, Throwable e) {
// The following shows what I'd like, though it won't work like this.
Toast.makeText(getApplicationContext(), "Looks like I am having a bad day!", Toast.LENGTH_LONG).show();
Log.e("UncaughtException", "I found an exception!");
// Add some code logic if needed based on your requirement
}
}
Again, this works perfectly in Android 9 and I also got the error reported. However in the phone with android 10, I just get the crash report like the image above and no error is reported.
I want to know why the crash handling is not working and how can I fix it?
I would not finish the Activity you came from, instead just open a new intent (on UnityActivity). When you end this intent the app will come back to the last active Activity.
I will give you my script as an example:
public void sendJobToUnity(String fileName, boolean isNewJob){
//creates a new job. It exists inside the JobSelector Activity
isUnityLoaded = true;
//this is what you are looking for part1
Intent i = new Intent(JobSelector.this, MainUnityActivity.class); //same as (CurrentActivity.this, UnityActivity.this)
//those are how I send some data across the app. just ignore it
//i.putExtra("jobName", fileName);
//i.putExtra("isNewJob",isNewJob);
//i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityForResult(i, 1); //this is what you are looking for part2
}
For closing it, in the MainUnityActivity Activity I have an override that Unity sends to Android in order to Unload the activity (not quit it completely cause you cannot load it again if you do it) like this:
#Override
protected void receiveJobAndUnloadUnity(String data){
saveCurrentJob(data); //saves the job it receives from Unity
mUnityPlayer.unload(); //this is what you are looking for part3
}
If you want to unload Unity from android you can put "mUnityPlayer.unload();" wherever you want, provided you have started the Activity the way I've shown you.
Note that "mUnityPlayer" is a default Unity variable and cannot be renamed
So I have a Fragment that calls the following method which launches the camera:
private void launchCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
And I expect to receive the picture data in this method in my fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!(resultCode == RESULT_OK)) return;
if (requestCode == REQUEST_IMAGE_CAPTURE || requestCode == REQUEST_GALLERY_IMAGE) {
Uri imageURI = data.getData();
// do something
}
}
However, after I take a picture and confirm it, the app goes to my launcher. After setting breakpoints in the onActivityResult method, the app never even reaches this method before crashing. I've granted made sure to grant all permissions in both the manifest and at runtime.
There are also no outputs to Logcat with this crash, both in the app logs and the device logs. I have also tested on both my device (Moto G5 Plus) & Pixel XL API 26 emulator; both have the same result.
I think you need to call super.onActivityResult().
The fragment is the one making the startActivityForResult() call, but the activity gets the first shot at handling the result so you need to implement super.onActivityResult() to make the fragment handle the result.
the app immediately crashes to my launcher
No, the camera app immediately brings up the launcher. Apparently, the developers of this camera app wrote it to bring up the home screen when the user is done taking the picture. This is a bug, of course, but there is little that you can do about it.
Use ACTION_IMAGE_CAPTURE when you would like a picture but do not mind if you do not get it, due to bugs in some of the hundreds of camera apps that you are integrating with. Otherwise, use a camera library (Fotoapparat, CameraKit-Android, etc.) to take the picture directly in your own app.
Also, and FWIW, ACTION_IMAGE_CAPTURE does not return a Uri, so your onActivityResult() code will not work anyway. Given your particular ACTION_IMAGE_CAPTURE Intent configuration, in onActivityResult(), data.getParcelableExtra("data") will return a Bitmap representing a thumbnail-sized image.
I believe this is yet another manifestation of the Android: Activity getting Destroyed after calling Camera Intent.
The bottom line is, the system restarts your app from scratch and your activity is created but has a chance to restore its state.
So you must implement onRestoreInstanceState(). I am not sure you can guarantee that the fragment will be ready to receive onActiviyResult() timely, so to be in the safe side, I prefer to handle the captured image in activity itself.
So this issue was actually because of an intent flag I had attached to the activity. My activity was started using the NO_HISTORY intent flag, which apparently prevented it from being recreated when returned from a startActivityForResult call.
I have an android app which passes some results to the next activity using StartActivityForResult. This is working fine on every phone/tablet I've tested it on except for a Motorola MZ601 which is on Android 4.0.4. By debugging through the code on any other device, it runs the below code:
private void showNextPage() {
Intent i = new Intent(this, InstructionsActivity.class);
startActivityForResult(i, TEST_LEFT_ACTIVITY);
}
This then takes me to the onActivityResult method as expected. Now when I debug through the code on the Motorola instead of taking to the onActivityResult method, it takes me to the onCreate method and reinitialises the results to 0 before moving to the onActivityResult method. So it passes null results over.
Can anyone explain to me why it takes me to the onCreate method before taking my to the onActivityResult method only on this device?
Is it a bug in the version of android or is it the device?
I am trying to take a photo in my Android application using my PC's built-in webcam. I am using the eclipse Android emulator and have set the AVD to use webcam0 as the rear-facing camera, but when I run my program it always crashes, saying "Unfortunately, Camera has stopped". I have added the following line to my Manifest xml:
<uses-permission android:name="android.permission.CAMERA"/>
though it still does not seem to work. I have read in a few places that there's supposed to be a "Hardware" section in the AVD manager edit/create screen, but mine does not have it.
Am I missing something? Here is the logcat that appears when I try to run the app:
Any thoughts about what might be happening? I've searched for solutions all over the place and can't seem to find any that solve this exact problem. Thanks for the help.
EDIT
Here's my image capture button/method code:
/* Create capture button */
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//create directory/picture file
count++;
file = dir + count + ".jpg";
File picFile = new File(file);
try {
picFile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(picFile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
/* Check if valid photo */
#Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
}
}
This code was mostly created based on a thread I found here on SO, I'm afraid I don't recall which one though.
Have you added:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
in your manifest?
If you feel that you have followed all the right procedures for activating your camera but fails, then as for my case, I had to try the in-built camera in the emulator to rule out any possibility that it is my codes. I realized that the same error was being produced by the default camera app - as you reported.
So, after several trials: (adding space, ram, more manifest files, clearing Google Play Services Data, updating Play Services, etc), I decided to use an Android Studio on Windows 10 x64 machine (the earlier one was Windows 7 x64). To my surprise, the error was gone and the camera did not crash. Now, I don't know if it has to do with OS (Win 7) or the Processor, or any other hardware or software issue. You can try this if you are not already using Windows 10.
I am trying to open Gogole Maps to a specific location with the below code, however the app is crashing with the error "No Activity Found to Handle Intent". Can anyone see what the problem is ?
ImageButton addressbutton = (ImageButton) findViewById(R.id.addressbutton);
addressbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String uri = "geo:0,0?q=MCNAMARA+TERMINAL+ROMULUS+MI+48174";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(uri));
startActivity(i);
}
});
The code works fine. The problem is the device/emulator you are testing the code at.
If you use AVD having Google APIs target (any level since 3), it works as expected. However, if you use AVD having normal Android target (that's a target without maps support), you get the error.
Try adding this:
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
before the call to startActivity.
HTH
try this
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(uri));
instead of
Intent i = new Intent(Intent.ACTION_VIEW);