Get error on transparent image using huawei image segmentation - android

MLImageSegmentationSetting analyzerSetting = (new MLImageSegmentationSetting.Factory()).setExact(true).setAnalyzerType(MLImageSegmentationSetting.BODY_SEG).setScene(2).create();
MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory().setAnalyzerType(MLImageSegmentationSetting.BODY_SEG).create();
this.analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(analyzerSetting);//error on this line
error :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.brandpost.brandpro365, PID: 9861
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2001, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/61/ORIGINAL/NONE/image/png/1980871995 flg=0x1 clip={text/uri-list {U(content)}} }} to activity {com.brandpost.brandpro365/com.mlkit.sample.activity.StillCutPhotoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:5301)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5340)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:54)
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at com.huawei.agconnect.config.a.c.a(Unknown Source:0)
at com.huawei.agconnect.config.a.g.(Unknown Source:41)
at com.huawei.agconnect.config.a.a.getString(Unknown Source:43)
at com.huawei.agconnect.config.a.a.getString(Unknown Source:1)
at com.huawei.hms.mlsdk.common.AgConnectInfo.(AgConnectInfo.java:53)
at com.huawei.hms.mlsdk.common.MLApplicationSetting.fromResource(MLApplicationSetting.java:262)
at com.huawei.hms.mlsdk.common.MLApplication.initialize(MLApplication.java:180)
at com.huawei.hms.mlsdk.common.MLApplication.getInstance(MLApplication.java:125)
at com.huawei.hms.mlsdk.MLAnalyzerFactory.getInstance(MLAnalyzerFactory.java:53)
at com.mlkit.sample.activity.StillCutPhotoActivity.createImageTransactor(StillCutPhotoActivity.java:149)
at com.mlkit.sample.activity.StillCutPhotoActivity.onActivityResult(StillCutPhotoActivity.java:101)
at android.app.Activity.dispatchActivityResult(Activity.java:8382)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5294)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5340) 
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:54) 
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loopOnce(Looper.java:201) 
at android.os.Looper.loop(Looper.java:288) 
at android.app.ActivityThread.main(ActivityThread.java:7839) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 

Please follow the instuctions for HMS ML SDK : https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/image-segmentation-0000001050040109
First, check the preparations:
Configure App info. in AGC: https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/config-agc-0000001050990353
Enabling the service: https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/enable-service-0000001050038078
Then, as for how to Create an image segmentation analyzer, please refer here: https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/image-segmentation-0000001050040109
// Method 1: Use default parameter settings to configure the image segmentation analyzer.
// The default mode is human body segmentation in fine mode. All segmentation results of human body segmentation are returned (pixel-level label information, human body image with a transparent background, gray-scale image with a white human body and black background, and an original image for segmentation).
MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer();
// Method 2: Use MLImageSegmentationSetting to customize the image segmentation analyzer.
MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory()
// Set whether to support fine segmentation. The value true indicates fine segmentation, and the value false indicates fast segmentation.
.setExact(false)
// Set the segmentation mode to human body segmentation.
.setAnalyzerType(MLImageSegmentationSetting.BODY_SEG)
// Set returned result types.
// MLImageSegmentationScene.ALL: All segmentation results are returned (pixel-level label information, human body image with a transparent background, gray-scale image with a white human body and black background, and an original image for segmentation).
// MLImageSegmentationScene.MASK_ONLY: Only pixel-level label information and an original image for segmentation are returned.
// MLImageSegmentationScene.FOREGROUND_ONLY: A human body image with a transparent background and an original image for segmentation are returned.
// MLImageSegmentationScene.GRAYSCALE_ONLY: A gray-scale image with a white human body and black background and an original image for segmentation are returned.
.setScene(MLImageSegmentationScene.FOREGROUND_ONLY)
.create();
MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);

Related

Unable to get button background color

I am trying to obtain the current background color of a button from a randomized color palette. However, my app is always crashing when I try the following code.
int activeColor = ((ColorDrawable)color1.getBackground()).getColor();
I always get this error message.
Process: com.boredgiant.chora, PID: 17418
java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable
at com.boredgiant.chora.DrawActivity$103.onClick(DrawActivity.java:850)
at android.view.View.performClick(View.java:6314)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:24793)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6543)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
You need to cast it to RippleDrawable
int activeColor = ((RippleDrawable)color1.getBackground()).getColorStateList().getDefaultColor();

Application crashes sometimes on androidx.appcompat.widget.ContentFrameLayout.setDecorPadding() method

Sometimes, I get this crash while my app is opening:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.widget.ContentFrameLayout.setDecorPadding(int, int, int, int)' on a null object reference
at androidx.appcompat.app.AppCompatDelegateImpl.applyFixedSizeWindow(AppCompatDelegateImpl.java:1026)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:820)
at androidx.appcompat.app.AppCompatDelegateImpl.onPostCreate(AppCompatDelegateImpl.java:527)
at androidx.appcompat.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:127)
at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1381)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3499)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7814)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
As you can see, there's no reference to something in my code in the stack trace so for this reason I'm not able to figure out what the problem is. What could be the cause of this crash?.
In my case, I was getting this error in production when I load the first screen. I also use androidx and no Support Libraries. Some times this crash looks like:
Caused by java.lang.RuntimeException
Window couldn't find content container view ***.onCreate
Unfortunately, I still don't understand what exactly the error is but I found this post. I changed onCreate method to:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
getWindow().getDecorView();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
}
Just added getWindow().getDecorView(); before super.onCreate(savedInstanceState); and it solved my problem.
Add as null check before calling this method
if (YourContentFrameLayout != null)
YourContentFrameLayout.setDecorPadding(intvalue,intvalue,intvalue,intvalue);

Android Picasso image loading app crash when scrolls the RecyclerView

I'm using the Retrofit to test and learn it as I have a project on it. While I use https://jsonplaceholder.typicode.com/photos for binding the photo into the recycle view. The issue is I can't scroll above 9 images; I mean whenever I scroll down to the 10th image, it is shown and then the app crashes.
img
2020-03-21 15:11:48.286 9426-9426/navneet.com.carsrecyclerview E/AndroidRuntime: FATAL EXCEPTION: main
Process: navneet.com.carsrecyclerview, PID: 9426
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:714)
at com.pkmmte.view.CircularImageView.refreshBitmapShader(CircularImageView.java:341)
at com.pkmmte.view.CircularImageView.invalidate(CircularImageView.java:262)
at android.widget.ImageView.setImageDrawable(ImageView.java:572)
at com.squareup.picasso.PicassoDrawable.setPlaceholder(PicassoDrawable.java:59)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:728)
at navneet.com.carsrecyclerview.CarsAdapter.onBindViewHolder(CarsAdapter.java:58)
at navneet.com.carsrecyclerview.CarsAdapter.onBindViewHolder(CarsAdapter.java:24)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:286)
at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:343)
at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:359)
at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:366)
at android.support.v7.widget.GapWorker.run(GapWorker.java:397)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
the issue is similar to Android Picasso image loading app crash when scrolling RecyclerView but my point looks different.
Picasso.get().load(carsModels.get(i).getUrl()).noFade().resize(150,150).into(viewHolder.car_image, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
Toast.makeText(context, "An error occurred", Toast.LENGTH_SHORT).show();
}
});
any help will be appriciated. thank you.
It seems to me that the problem may not be with Picasso. It could be with the third party CircularImageView that you are using. You can put a debug breakpoint at the android.graphics.Bitmap.createScaledBitmap(Bitmap.java:714) and see why the Bitmap object is null there. Then, you can go down the stack trace and figure out why it is null. That will give you a better idea of what you need to do to make sure your Bitmap object is instantiated before hitting that piece of code.
If you do find something, please update here so that I can edit this answer after better understanding the reason for your NullPointerException.
I think its better if you just load the image but the resizing happens on the ImageView end
Picasso.get().load(carsModels.get(i).getUrl()).noFade().into(viewHolder.car_image);
In your case, the car_image ImageView in layout XML should have:
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
This ensures that the image you are loading inside is size 150x150 density pixels and making sure it doesn't stretch or look disoriented. It will start fitting in center if you have images that are not of equal aspect ratio.
I hope this helps!

Sometimes get android.content.res.Resources$NotFoundException

We're debugging a crash in a Cordova app that seems to happen on launch / foregrounding, after the app was backgrounded for a moderate amount of time and has been to some extent killed by the OS.
At the time of the crash the app is attempting to show a native dialog fragment for Fingerprint Authentication. For some reason in this case the resources for this fragment cannot be found.
It's been reported for Android 6 and 7, on older and newer devices.
java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.koho/ca.koho.Koho}: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2421)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:174)
at android.app.ActivityThread.main(ActivityThread.java:5440)
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)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1437)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2887)
at android.content.res.Resources.getLayout(Resources.java:1251)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at com.cordova.plugin.android.fingerprintauth.FingerprintAuthenticationDialogFragment.onCreateView(FingerprintAuthenticationDialogFragment.java:85)
at android.app.Fragment.performCreateView(Fragment.java:2220)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1953)
at android.app.FragmentController.dispatchActivityCreated(FragmentController.java:152)
at android.app.Activity.performCreateCommon(Activity.java:6279)
at android.app.Activity.performCreate(Activity.java:6287)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374)
... 9 more
Here's the problematic bit of code, from the DialogFragment's onCreateView:
int fingerprint_dialog_container_id = getResources()
.getIdentifier("fingerprint_dialog_container", "layout",
FingerprintAuth.packageName);
View v = inflater.inflate(fingerprint_dialog_container_id, container, false);
When the crash happens, getIdentifier(...) is for some reason returning 0. Most of the time the actual identifier is returned.
I have not found a way to reproduce the crash, and advice to enable developer options like 'Don't keep activities' and 'Background process limit' had did not help.
Thanks for any help.

How to debug internal crash of GLSurfaceView?

I'm developing a VR video viewer using Android Cardboard SDK and RajawaliVR (https://github.com/Rajawali/RajawaliVR)
On some devices I have this crash when returning from sleep (others just show black screen):
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.opengl.GLSurfaceView$GLThread.surfaceCreated()' on a null object reference
at android.opengl.GLSurfaceView.surfaceCreated(GLSurfaceView.java:523)
at android.view.SurfaceView.updateWindow(SurfaceView.java:580)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:176)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1970)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5292)
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:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
As you can see the crash is in the android code, how do I start debugging it?
You have access to the source code, so start with that.
Assuming you're using Lollipop, it's failing on this line:
mGLThread.surfaceCreated();
The error message indicates that mGLThread is null. Doing a simple search for the assignment ("mGLThread =") with the web browser reveals two instances, the most interesting of which is this one:
public void setRenderer(Renderer renderer) {
...
mGLThread = new GLThread(mThisWeakRef);
Setting some of the complexities aside, mGLThread should be non-null so long as setRenderer() is called. So the first thing to do is check to see if your application is calling setRenderer() (typically in onCreate()).
If you're following along the Android Developer OpenGL tutorial, it's possible that Android Studio corrected this definition in your Activity class constructor:
mGLView = new MyGLSurfaceView(this);
and changed it to
mGLView = new GLSurfaceView(this);
because the class MyGLSurfaceView had not been coded yet...skipping the MyGLSurfaveView constructor which is where setRenderer(...) is called. Mr. fadden's answer above is excellent, explains how to diagnose the problem.

Categories

Resources