简体   繁体   English

android.content.res.Resources $ NotFoundException:资源ID#0x0 Android错误

[英]android.content.res.Resources$NotFoundException: Resource ID #0x0 Android error

I am Creating an ArrayAdapter's Super class in which I am binding the data I receive from the object and put them into the TextViews. 我正在创建ArrayAdapter的Super类,在其中绑定从对象接收的数据,并将它们放入TextViews中。
The code is not any complicated and I have double checked the ids of all the TextViews in my XML. 代码并不复杂,我已经仔细检查了XML中所有TextView的ID。

And I have checked all the other questions with the same title. 我已经检查了所有其他标题相同的问题。
None of them seem to work in this case. 在这种情况下,它们似乎都不起作用。

Attaching my code below. 在下面附加我的代码。

public class EarthquakeAdapter extends ArrayAdapter<Earthquake> 
    {
        public EarthquakeAdapter(Context context, int resource) {
            super(context, resource);
        }
        public EarthquakeAdapter(Activity activity, ArrayList<Earthquake> Earthquakes)
        {
            super(activity,0, (List<Earthquake>) Earthquakes);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            super.getView(position, convertView, parent);
            View ListItemView = convertView;
            if(ListItemView == null)
            {
                ListItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.earthquake_list_item, parent, false);
            }

            Earthquake currentItem = getItem(position);

            TextView magnitude = (TextView) 
            ListItemView.findViewById(R.id.magnitude);
            //Double m = ;
            magnitude.setText(""+currentItem.getmMag());

            TextView location = (TextView) ListItemView.findViewById(R.id.Location);
            location.setText(currentItem.getmLocation());

            TextView date = (TextView) ListItemView.findViewById(R.id.date);
            date.setText(currentItem.getmDate());

            return ListItemView;
        }

and then there is the xml file. 然后是xml文件。

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:id="@+id/list_view_container">

    <TextView
        android:id="@+id/magnitude"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="1"
        tools:text="7.6"/>
    <TextView
        android:id="@+id/Location"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="1"
        tools:text="San Fransisco"/>
    <TextView
        android:id="@+id/date"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="1"
        tools:text="Date"/>

</LinearLayout>

This is my Earthquake Class. 这是我的地震班。

public class Earthquake 
     {
        private double mMag;
        private String mLocation;
        private String mDate;

        public Earthquake(double Mag, String Location, String Date){
            mMag = Mag;
            mLocation = Location;
            mDate = Date;
        }

        public String getmDate() {
            return mDate;
        }

        public double getmMag() {
            return mMag;
        }

        public String getmLocation() {

            return mLocation;
        }
    }

and This is the Error LOG. 这是错误日志。

06-11 20:38:36.570 9359-9359/com.example.android.quakereport W/ResourceType: No package identifier when getting value for resource number 0x00000000 06-11 20:38:36.571 9359-9359/com.example.android.quakereport D/AndroidRuntime: Shutting down VM 06-11 20:38:36.581 9359-9359/com.example.android.quakereport E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.quakereport, PID: 9359
   android.content.res.Resources$NotFoundException: Resource ID #0x0
       at android.content.res.Resources.getValue(Resources.java:1369)
       at android.content.res.MiuiResources.getValue(MiuiResources.java:145)
       at android.content.res.Resources.loadXmlResourceParser(Resources.java:2824)
       at android.content.res.Resources.getLayout(Resources.java:1183)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:425)
       at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:378)
       at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
       at com.example.android.quakereport.EarthquakeAdapter.getView(EarthquakeAdapter.java:28)
       at android.widget.AbsListView.obtainView(AbsListView.java:2346)
       at android.widget.ListView.makeAndAddView(ListView.java:1876)
       at android.widget.ListView.fillDown(ListView.java:702)
       at android.widget.ListView.fillFromTop(ListView.java:763)
       at android.widget.ListView.layoutChildren(ListView.java:1685)
       at android.widget.AbsListView.onLayout(AbsListView.java:2148)
       at android.view.View.layout(View.java:16653)
       at android.view.ViewGroup.layout(ViewGroup.java:5438)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
       at android.view.View.layout(View.java:16653)
       at android.view.ViewGroup.layout(ViewGroup.java:5438)
       at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
       at android.view.View.layout(View.java:16653)
       at android.view.ViewGroup.layout(ViewGroup.java:5438)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
       at android.view.View.layout(View.java:16653)
       at android.view.ViewGroup.layout(ViewGroup.java:5438)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
       at android.view.View.layout(View.java:16653)
       at android.view.ViewGroup.layout(ViewGroup.java:5438)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
       at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2680)
       at android.view.View.layout(View.java:16653)
       at android.view.ViewGroup.layout(ViewGroup.java:5438)
       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2198)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1958)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6045)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
       at android.view.Choreographer.doCallbacks(Choreographer.java:672)
       at android.view.Choreographer.doFrame(Choreographer.java:608)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5441)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

The problem is in the getView() method of EarthquakeAdapter class the statement super.getView() was causing some issue. 问题出在EarthquakeAdapter类的getView()方法中,语句super.getView()引起了一些问题。 I removed it and its working fine now. 我删除了它,现在可以正常工作了。

Refer to the comment of @MikeM in the question itself. 请参阅问题本身中的@MikeM注释。

Remove the super call in getView(). 在getView()中删除超级调用。 You've passed an invalid layout resource ID in the three-argument constructor - which is leading to that Exception when ArrayAdapter tries to inflate it - and you're not using the super return anyway, since you're inflating the layout yourself 您在三参数构造函数中传递了无效的布局资源ID-当ArrayAdapter尝试对其进行充气时,这会导致该异常-而且您也不会使用超级返回,因为您自己在对布局进行充气

I think you need to fix your constructors. 我认为您需要修复构造函数。 You only need one. 您只需要一个。

public class EarthquakeAdapter extends ArrayAdapter<Earthquake> {
    public EarthquakeAdapter(Context context, ArrayList<Earthquake> earthquakes) {
        super(context, 0, earthquakes);
    }

And you should not need to call super.getView 而且您不需要调用super.getView

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Earthquake earthquake = getItem(position);

        View v = convertView;
        if(v == null) {
            v = LayoutInflater.from(getContext()).inflate(
                R.layout.earthquake_list_item, parent, false);
        }

        return v;
    }

Otherwise, you need to determine what is happening at line 28 and what is zero valued there. 否则,您需要确定第28行发生了什么,那里的值为零。

You may follow this example to define custom adapter 您可以按照以下示例来定义自定义适配器

You may need to clean and rebuild the project to regenerate your resources. 您可能需要清理并重建项目以重新生成资源。

Try this 尝试这个

 Earthquake currentItem = new Earthquake(3.33,"someplace","05-05-05")
     currentItem=getItem(position);

in place of 代替

 Earthquake currentItem = get(position);        `//getItem(position);`

if it work you need to set values of the variables declared in Earthquake class 如果有效,则需要设置在Earthquake类中声明的变量的值

This might solve your problem Declare member variables 这可能会解决您的问题声明成员变量

Context ctx;
ArrayList<Earthquake> Earthquake;
    public EarthquakeAdapter(Activity activity, ArrayList<Earthquake> Earthquakes)
            {
               super(activity,0, (List<Earthquake>) Earthquakes);
               Ctx=activity;
               Earthquake=this.Earthquake; 
            }

now where you are doing Earthquake currentItem = getItem(position); 现在您在哪里Earthquake currentItem = getItem(position); do this 做这个

Earthquake currentItem=Earthquake.get(position);     `//getItem(position);`

basically you are not passing values received by EarthquakeAdapter. 基本上,您不是在传递EarthquakeAdapter接收的值。

Also check in this 还要检查一下

    public Object getItem(int position) {
            return Earthquake.get(position);
        }
public int getCount() {
        return Earthquake.size();
    }
public long getItemId(int position) {
        return position;
    }

If this does not work show the code where you are setting the ArrayList<Earthquake> and calling adapter public EarthquakeAdapter(Activity activity, ArrayList<Earthquake> Earthquakes) 如果这样做不起作用,请显示代码,在其中设置ArrayList<Earthquake>并调用适配器public EarthquakeAdapter(Activity activity, ArrayList<Earthquake> Earthquakes)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.content.res.Resources $ NotFoundException:资源ID#0x0 - android.content.res.Resources$NotFoundException: Resource ID #0x0 CheckedTextView 错误:android.content.res.Resources$NotFoundException:资源 ID #0x0 - CheckedTextView error :android.content.res.Resources$NotFoundException: Resource ID #0x0 GridView.FATAL例外:main android.content.res.Resources $ NotFoundException:字符串资源ID#0x0 - GridView.FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: String resource ID #0x0 android.content.res.Resources$NotFoundException:使用 RecyclerView 时出现资源 ID #0x0 异常 - android.content.res.Resources$NotFoundException: Resource ID #0x0 exception while using RecyclerView android.content.res.Resources$NotFoundException: 字符串资源 ID #0x0 - android.content.res.Resources$NotFoundException: String resource ID #0x0 android.content.res.Resources $ NotFoundException:Spinner中的资源ID#0x0 - android.content.res.Resources$NotFoundException: Resource ID #0x0 in Spinner 适配器有问题。 android.content.res.Resources $ NotFoundException:资源ID#0x0 - Having trouble with adapters. android.content.res.Resources$NotFoundException: Resource ID #0x0 android.content.res.Resources $ NotFoundException:资源ID#0x0可绘制 - android.content.res.Resources$NotFoundException: Resource ID #0x0 drawable android.content.res.Resources $ NotFoundException:资源ID#0x0 java异常 - android.content.res.Resources$NotFoundException: Resource ID #0x0 java exception 尝试在 Android Studio 中打开 Unity 活动会产生 android.content.res.Resources$NotFoundException: String resource ID #0x0 - Trying to open an Unity activity inside Android Studio yields android.content.res.Resources$NotFoundException: String resource ID #0x0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM