如何摆脱未找到的资源/ FATAL EXCEPTION ERROR [英] How do I get rid of a resources not found/FATAL EXCEPTION ERROR
问题描述
我一直试图找到一种方法让它工作,但似乎没有任何效果。
当我运行我的应用程序时,我在 ivIcon.setImage(...) 上获得致命异常
我修复了android抱怨的弃用问题,但我仍然在ivIcon.setImage()上获得了FATAL EXCEPTION。具体来说;
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;
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
推荐答案
NotFoundException:资源ID#0x0
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;
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
您好,
IMAGE_RESOURCE_ID 是一个String
变量,其中包含字符串
数据类型值。
它应该是int
指向int
数据类型值,在这种情况下,是可绘制的资源,例如R.drawable.iconResource
。
Hello,
IMAGE_RESOURCE_ID is aString
variable that has aString
data-type value.
It should be anint
pointing to anint
data-type value and in this case, a drawable resources e.g.R.drawable.iconResource
.
随时打开进行更正。
找出问题所在。
我导入了不需要的外部资源,这导致了与访问正确资源的冲突,因此我删除了那些导入语句并用有问题的行替换了,
ivIcon.setImageDrawable(view.getResources()。getDrawable(getArguments()。getInt(IMAGE_RESOURCE_ID)));
哪个有效。
实际问题是我使用的是另一个版本的DrawerItem构造函数,它没有带任何图标。
感谢您的帮助。
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.
这篇关于如何摆脱未找到的资源/ FATAL EXCEPTION ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!