android.content.res.Resources$NotFoundException: String resource ID #0x0

https://stackoverflow.com/questions/11189545/android-android-content-res-resourcesnotfoundexception-string-resource-id-0x



07-31 14:36:18.482 24607-24607/com.example.espl.nivedh E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.espl.nivedh, PID: 24607
    android.content.res.Resources$NotFoundException: String resource ID #0x0
        at android.content.res.Resources.getText(Resources.java:328)
        at android.content.res.MiuiResources.getText(MiuiResources.java:125)
        at android.widget.TextView.setText(TextView.java:4432)
        at com.example.espl.nivedh.databinding.ItemCartonViewBindingImpl.executeBindings(ItemCartonViewBindingImpl.java:138)
        at androidx.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:437)
        at androidx.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:409)
        at com.example.espl.nivedh.carton_list.CartonAdapter$MyViewHolder.bind(CartonAdapter.kt:80)
        at com.example.espl.nivedh.carton_list.CartonAdapter.onBindViewHolder(CartonAdapter.kt:33)
        at com.example.espl.nivedh.carton_list.CartonAdapter.onBindViewHolder(CartonAdapter.kt:12)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7033)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7075)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5991)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6258)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6097)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6093)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:561)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4115)
        at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3521)
        at android.view.View.measure(View.java:18830)
        at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:722)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:373)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:112)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:213)
        at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:113)
        at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1458)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1537)
        at android.view.View.measure(View.java:18830)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:18830)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18830)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18830)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.Vie

----------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable name="cartonitem1" type="com.example.espl.nivedh.room_database.CartonsOb" />
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_margin="7dp"
            android:padding="8dp"
            android:clickable="true"
            android:background="@drawable/selection_item_clicked"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <!--"-->
        <!--android:background="@drawable/selection_item"-->
        <!--app:setBackground="@{cartonitem1.isSlected}"-->
        <!--app:loadBackground="@{item1.isSlected}"-->

        <TextView
                android:id="@+id/tv_carto_number"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:textSize="18sp"
                android:text="@{cartonitem1.cartonDiplayText}"
                app:setSelectionForView="@{cartonitem1.isSlected}"
                app:layout_constraintEnd_toStartOf="@+id/tv_no_package"
                app:layout_constraintStart_toStartOf="parent" />
        <!--android:text="@{cartonitem1.code}"-->
        <!--app:setTextColor="@{item1.isSlected}"-->

        <!--<TextView-->
        <!--android:id="@+id/tv_select_value"-->
        <!--android:layout_width="0dp"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:text="@{item1.strVal}"-->
        <!--app:layout_constraintEnd_toEndOf="parent"-->
        <!--app:layout_constraintStart_toEndOf="@+id/tv_select_key" />-->

        <TextView
                android:id="@+id/tv_no_package"
                android:text="@{Integer.toString(cartonitem1.noOfPackages)}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:textSize="18sp"
                app:setSelectionForView="@{cartonitem1.isSlected}"
                app:layout_constraintEnd_toStartOf="@+id/iv_edit"/>

        <ImageView
                android:id="@+id/iv_edit"
                android:layout_width="24dp"
                app:setSelectionForView="@{cartonitem1.isSlected}"
                android:layout_height="24dp"
                app:layout_constraintEnd_toStartOf="@id/iv_delete"
                />

        <ImageView
                android:id="@+id/iv_delete"
                app:setSelectionForView="@{cartonitem1.isSlected}"
                android:layout_width="24dp"
                android:layout_height="24dp"
                app:layout_constraintEnd_toEndOf="parent"/>

        <!--app:imageUrl="@{cartonitem1.code}"-->
        <!--android:src="@drawable/ic_true"-->

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>


android.os.networkonmainthreadexception android

android.os.NetworkOnMainThreadException

NetworkOnMainThreadException
The exception that is thrown when an application attempts to perform a networking operation on its main thread. This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

StrictMode
StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them. StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application's main thread responsive, you also prevent ANR dialogs from being shown to users.

StrictMode.ThreadPolicy.Builder
You should almost always run network operations on a thread or as an asynchronous task.

But it is possible to remove this restriction and you override the default behavior, if you are willing to accept the consequences.

Add:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
In your class,
and
ADD this permission in android manifest.xml file: 
<uses-permission android:name="android.permission.INTERNET"/>

Consequences:
Your app will (in areas of spotty internet connection) become unresponsive and lock up, the user perceives slowness and has to do a force kill, and you risk the activity manager killing your app and telling the user that the app has stopped.
Android has some good tips on good programming practices to design for responsiveness:

AsyncTask:
This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask:
class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> {

    private Exception exception;

    protected RSSFeed doInBackground(String... urls) {
        try {
            URL url = new URL(urls[0]);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader xmlreader = parser.getXMLReader();
            RssHandler theRSSHandler = new RssHandler();
            xmlreader.setContentHandler(theRSSHandler);
            InputSource is = new InputSource(url.openStream());
            xmlreader.parse(is);

            return theRSSHandler.getFeed();
        } catch (Exception e) {
            this.exception = e;

            return null;
        } finally {
            is.close();
        }
    }

    protected void onPostExecute(RSSFeed feed) {
        // TODO: check this.exception
        // TODO: do something with the feed
    }
}

How to execute the task:
In MainActivity.java file you can add this line within your oncreate() method
new RetrieveFeedTask().execute(urlToRssFeed);

Don't forget to add this to AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET"/>
Android volley post json request example

Kotlin Type inference failed: Not enough information to infer parameter T in fun

/home/espl/Android-Studio-Projects/NIVEDH/app/src/main/java/com/example/espl/nivedh/MainActivity.kt: (214, 35): Type inference failed: Not enough information to infer parameter T in fun <T : Parcelable!> getParcelableExtra(p0: String!): T!
Please specify it explicitly.


Code with Error:
inner class MyReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val location = intent.getParcelableExtra(LocationUpdatesService.EXTRA_LOCATION)
            if (location != null) {
                Toast.makeText(
                    this@MainActivity, Utils.getLocationText(location),
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

Solution:
val location = intent.getParcelableExtra(LocationUpdatesService.EXTRA_LOCATION)

Replaced above line in code with below line, you can find change heighlighted in blue:
val location=intent.getParcelableExtra<Location>(LocationUpdatesService.EXTRA_LOCATION)

Fatal Exception: java.lang.AssertionError No NameTypeIndex match for SHORT_STANDARD

Fatal Exception: java.lang.AssertionError
No NameTypeIndex match for SHORT_STANDARD
android.icu.impl.TimeZoneNamesImpl$ZNames.getNameTypeIndex (TimeZoneNamesImpl.java:724)
android.icu.impl.TimeZoneNamesImpl$ZNames.getName (TimeZoneNamesImpl.java:790)
android.icu.impl.TimeZoneNamesImpl.getTimeZoneDisplayName (TimeZoneNamesImpl.java:183)
android.icu.text.TimeZoneNames.getDisplayName (TimeZoneNames.java:261)
java.util.TimeZone.getDisplayName (TimeZone.java:405)
java.util.Date.toString (Date.java:1066)
java.util.Properties.store0 (Properties.java:828)
java.util.Properties.store (Properties.java:817)
com.google.firebase.iid.zzy.zza (Unknown Source:88)
com.google.firebase.iid.zzy.zzc (Unknown Source:9)
com.google.firebase.iid.zzaw.com.google.firebase.iid.zzy.zzb (Unknown Source:2005)
com.google.firebase.iid.FirebaseInstanceId.zzi (Unknown Source:60)
com.google.firebase.iid.FirebaseInstanceId.zza (Unknown Source:147)
com.google.firebase.iid.zzn.run (Unknown Source:10)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1162)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:636)
java.lang.Thread.run (Thread.java:764)

java.lang.illegalstateexception: unable to create layer for linearlayout

Found most of this crashes on Android 6.0 around 75% of the total crashes and 25% from Android 5.0. According to google Crashlytics  report, these devices areOnePlus One, OnePlus X, Xperia Z, Xperia Z1, Xperia Z2, Xperia Z3, Samsung Galaxy Note, Galaxy S4 and Galaxy S5 devices, covering 80% of total crashes. Not able to recreate this crash on my HTC, Mi devices. How to reproduce this issue or find where exact the issue in app? Below is the detailed log from Crashlytics.
Fatal Exception: java.lang.IllegalStateException: Unable to create layer for LinearLayout, size 1080x4160 exceeds max size 4096
       at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
       at android.os.MessageQueue.next(MessageQueue.java:323)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:6186)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

Rectify and solve issue:

I have Nested scroll view which contains vertical recycler view and other elements in the main screen.

What is the exact issue :
Cause is that RecyclerView inside scrollview or nestedscrollview try to load all items at once. As RecyclerView loads all items, also those items whcih are not visible. If you put log inside onBindViewHolder of RecylerView you find that all items loads at once in the start instead of based on the visibility of the item. This causes the parent LinearLayout (inside the ScrollView) to throw exception. I had also issue related to same with Recyclerview inside NestedScrollView.

Issue cause related  to above discussion:
If UI load more than 1-1.5 screens of content at a time.
Means you are loading unncessasary content which are not visible to user at a time on mobile screen. (While vertical RecyclerView inside NestedScrollView loads all its items at once in starting, instead of based on Recyclerview Visibility)

In the below answer @ivan V found issue related to the same and also given suggestions.
https://stackoverflow.com/questions/26664922/crash-material-design-android-5-0/27906489#answer-32966150

If you have crashlytics log for this issue then you can rectify the android Device and Android OS for which this type of Crashnalytics log detected.

Ho to detect issue:
1. Find all such activities whcih contains layout with LinearLayout.
2. Run you application and search your log and open each of your activity. Try to find during which activity log similar to below is detected.
W/OpenGLRenderer(18137): Layer exceeds max. dimensions supported by the GPU (1080x4628, max=4096x4096)
E/AndroidRuntime(18137): java.lang.IllegalStateException: Unable to create layer for  RelativeLayout
OR
Unable to create layer for v
OR
Fatal Exception: java.lang.IllegalStateException: Unable to create layer for LinearLayout, size 1080x4160 exceeds max size 4096
Reference: https://stackoverflow.com/questions/26626344/scene-transition-with-hero-elements-throws-layer-exceeds-max-dimensions-support#answer-27995911
Reference 2: https://stackoverflow.com/questions/26664922/crash-material-design-android-5-0/27906489#answer-27906489

Reference 3: Scene transition with hero elements throws Layer exceeds max. dimensions supported by the GPU

Solution :
Paginate your content and load no more than 1-1.5 screens of content at a time.

This will significantly decrease the screen size that is being pre-rendered during shared elements transition (it's being pre-rendered every time so the system knows where to place your transition object after transition)

Best Reference for solution:  https://stackoverflow.com/questions/26664922/crash-material-design-android-5-0/27906489#answer-32966150

I had the same issue & How I solved it:

My Layout was like below mentioned:
I have refactored my code such that there is only one main recycler view and all other layout are child of it. Using the RecycleView with multiple types of child views

Here I have replaced the logic part and UI part of Nested Scroll View items to different type of Recycler View holders inside Main RecyclerView.

You can see in bottom table I have replaced the Nested Scroll View and Linear-layout with a New Main RecyclerView.


BeforeAfter Refactoring from Scroll View to Recycler View
Nested Scroll View Main layout-> RecyclerView
LinearLayout
Horizontal Recycler View Child item layout-> Horizontal Recycler View
Two text View with two Horizontal Recycler Views Child item layout-> Two text View with two Horizontal Recycler Views
ImageView Child item layout-> ImageView
Horizontal Recuycler View Child item layout-> Horizontal Recycler View
Vertical Recuycler View (with 12 items of sametype) Child item layout-> With 12 items with same type as a childs of Main RecyclerView


Feel free to comment and post your solutions and doubts. 

Operator '==' cannot be applied to Float and Int in Kotlin

Warning:
Operator '==' cannot be applied to Float and Int in Kotlin

I come accross this warning/error inside RatingBar.setOnRatingBarChangeListener() method:

ratingbar_rateusfive_screen1.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
           
            if (rating == 5) {  // Warning in this line
            }
            }

Reason is that he rating variable is of Float type and comparing it with integer 5.
So after converting 5 to 5f it worked fine>

ratingbar_rateusfive_screen1.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
           
            if (rating == 5f) {
            }
            }

getResources().getColor() is deprecated

It looks like the best approach is to use ContextCompat.getColor() method:
This method will choose the Marshmallow two parameter method or the pre-Marshmallow method appropriately.

How to use ContextCompat.getColor() method:

ContextCompat.getColor(context, R.color.color_name)

Example:
yourView.setBackgroundColor(ContextCompat.getColor(applicationContext,
                            R.color.colorAccent))



Code For all the Kotlin users out there:

context?.let {
    val color = ContextCompat.getColor(it, R.color.colorPrimary)
    // ...
}

rounded corner imageview android glide

Create rounded corner imageview using Android Glide V4:

GlideApp.with(mContext)
                                    .load(imageURL)
                                    .fitCenter()
                                    .transform(new RoundedCorners(3))  // using glide
                                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                                    .error(R.drawable.ocd_placeholder)
                                    .placeholder(R.drawable.ocd_placeholder)
                                    .into(imageView);

Popular Posts