Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I've been trying to find a way to get this to work but nothing seems to work.

When I run my app, I get a FATAL EXCEPTION on ivIcon.setImage(...)
I fixed the deprecated issue that android complained about but I still get the FATAL EXCEPTION on ivIcon.setImage(). Specifically;

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


I've looked around for a solution but none seem to fix the issue.
Has anyone had this problem before?

Here's my code;

Java
        import android.os.Bundle;
        import android.view.View;
        import android.app.Fragment;
        import android.view.ViewGroup;
        import android.widget.TextView;
        import android.widget.ImageView;
        import android.view.LayoutInflater;
        import android.graphics.drawable.Drawable;
        import android.support.v4.content.res.ResourcesCompat;

        import static android.content.res.Resources.*;

public class FragmentOne extends Fragment {

    ImageView ivIcon;
    TextView tvItemName;

    public static final String IMAGE_RESOURCE_ID = "iconResourceID";
    public static final String ITEM_NAME = "itemName";

    public FragmentOne() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_layout_one, container, false);

        ivIcon = (ImageView) view.findViewById(R.id.frag1_icon);
        tvItemName = (TextView) view.findViewById(R.id.frag1_text);
        tvItemName.setText(getArguments().getString(ITEM_NAME));
        ivIcon.setImageDrawable(view.getResources().getDrawable(getArguments().getInt(IMAGE_RESOURCE_ID),view.getContext().getTheme()));        
        return view;
    }
}



Thanks
Posted
Comments
Richard MacCutchan 23-May-15 12:56pm    
The error message is quite clear. The resource you are trying to use does not exist.
Dadecki 23-May-15 16:59pm    
Hello,
You must have your image in drawable folder.
Now it seems like it does not exists.

Hello,

IMAGE_RESOURCE_ID is a String variable that has a String data-type value.

It should be an int pointing to an int data-type value and in this case, a drawable resources e.g. R.drawable.iconResource.

Quote:

Open for correction, anytime.

 
Share this answer
 
Comments
pyler 27-May-15 16:27pm    
The code for FragmentOne class is the same for FragmentTwo and FragmentThree, the only difference between them are the class names.


Something strange that happens when I comment out this line only in FragmentOne.java

ivIcon.setImageDrawable(view.getResources().getDrawable(getArguments().getInt(IMAGE_RESOURCE_ID), view.getContext().getTheme()));



What happens is that the apps runs perfectly without any errors. The problem I have now is some icons don't show up in the layout when touched.
Figured out what the problem was.
I was importing unwanted external resources and that was causing conflicts with accessing right resources so I removed those import statements and replaced the problematic line with,

ivIcon.setImageDrawable(view.getResources().getDrawable(getArguments().getInt(IMAGE_RESOURCE_ID)));

Which worked.


The actual problem problem was that I was using another version of DrawerItem constructor which didn't take in any icons.

Thanks for the help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900