简体   繁体   English

android.content.res.Resources $ NotFoundException:资源ID#0x0 java异常

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

This exception occurs while running a fragment trying to display a ListView of objects. 运行片段尝试显示对象的ListView时会发生此异常。 I have researched the exception but the solution for other cases seems to have to do with the "setText" function, or anything else trying to display anything else than a string as a string. 我已经研究了该异常,但是针对其他情况的解决方案似乎与“ setText”函数有关,或者其他任何尝试将除字符串以外的任何内容显示为字符串的问题。 I'm really not sure what else to say. 我真的不确定还有什么要说的。 Thanks in advance! 提前致谢!

Java Class Java类

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import in.galaxyofandroid.spinerdialog.OnSpinerItemClick;
import in.galaxyofandroid.spinerdialog.SpinnerDialog;

import static android.R.attr.name;
import static android.R.attr.value;
import static com.company.ingredismartv2.R.id.add;
import static com.company.ingredismartv2.R.id.fragment;
import static com.company.ingredismartv2.R.id.nav_kitchen;

public class Kitchen extends Fragment {

public Kitchen() {
    // Required empty public constructor
}

SpinnerDialog spinnerDialog;
Button add;
ArrayList<String> listOfIngs = new ArrayList<>();



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

    View root = inflater.inflate(R.layout.fragment_kitchen, container, false);
    add = (Button) root.findViewById(R.id.add);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            spinnerDialog.showSpinerDialog();
        }
    });

    IngAdapter adapter = new IngAdapter(getContext(), listOfIngs);
    ListView listView = (ListView) root.findViewById(R.id.list);
    listView.setAdapter(adapter);
    return root;

}

public class IngAdapter extends ArrayAdapter<String> {
    IngAdapter(Context context, ArrayList<String> ings) {
        super(context, 0, ings);
    }


}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    spinnerDialog = new SpinnerDialog(getActivity(), ingredients, "Select An Ingredient");

    final Ing tomatoSauce = new Ing("Tomato Sauce", 0 ,0);
    listOfIngs.add(tomatoSauce.name);
    final Ing chicken = new Ing("Chicken", 0 ,0);
    listOfIngs.add(chicken.name);
    final Ing olives = new Ing("Olives", 0, 0);
    listOfIngs.add(olives.name);

    ingredients.add("Tomato Sauce");
    ingredients.add("Chicken");
    ingredients.add("Olives");

    ArrayList<Ing> ing = new ArrayList<>();



    spinnerDialog.bindOnSpinerListener(new OnSpinerItemClick() {

        @Override
        public void onClick(String Ingredient, int i) {

            Toast.makeText(Kitchen.super.getContext(), "Selected "+Ingredient, Toast.LENGTH_SHORT).show();

            if(Ingredient.equals(tomatoSauce.name)) {

                if (tomatoSauce.init == 0){

                    tomatoSauce.init = 1;

                }

            } else if(Ingredient.equals(chicken.name)) {

                if (chicken.init == 0){

                    chicken.init = 1;

                }

            } else if(Ingredient.equals(olives.name)){

                if (olives.init == 0){

                    olives.init = 1;

                }

            }

        }
    });


}

ArrayList<String> ingredients = new ArrayList<>();



public class Ing {

    int init;

    int value;

    String name;

    Ing(String name, int value, int init) {
        this.name = name;
        this.value = value;
        this.init = init;
    }


}

} }

XML Layout XML布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.company.ingredismartv2.Kitchen">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:text="Add An Ingredient"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/add"
        android:textSize="30sp"
        android:layout_gravity="center" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">


        <ListView
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:id="@+id/list">

        </ListView>

        <ListView
            android:layout_width="60dp"
            android:layout_height="match_parent">

        </ListView>

    </LinearLayout>





</LinearLayout>

Edit (logs) 编辑(日志)

I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build:  ()
          OpenGL ES Shader Compiler Version: E031.25.03.04
          Build Date: 04/06/15 Mon
          Local Branch: 
          Remote Branch: 
          Local Patches: 
          Reconstruct Branch: 
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
`enter code here`W/ResourceType: No package identifier when getting value for resource number 0x00000000
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.comapany.ingredismartv2, PID: 9504
              android.content.res.Resources$NotFoundException: Resource ID #0x0
                  at android.content.res.Resources.getValue(Resources.java:1266)
                  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2652)
                  at android.content.res.Resources.getLayout(Resources.java:1082)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
                  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
                  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
                  at android.widget.AbsListView.obtainView(AbsListView.java:2347)
                  at android.widget.ListView.makeAndAddView(ListView.java:1864)
                  at android.widget.ListView.fillDown(ListView.java:698)
                  at android.widget.ListView.fillFromTop(ListView.java:759)
                  at android.widget.ListView.layoutChildren(ListView.java:1673)
                  at android.widget.AbsListView.onLayout(AbsListView.java:2151)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                  at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                  at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                  at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1391)
                  at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                  at android.view.View.layout(View.java:15689)
                  at android.view.ViewGroup.layout(ViewGroup.java:5040)
                at android.widget.FrameLay
I/Process: Sending signal. PID: 9504 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

Found your error. 发现您的错误。 Its in your line below in your IngAdapter 它在您的IngAdapter中的以下行中

super(context, 0, ings);

In this the 2nd parameter should be the resource name and should never be hardcoded by any int value. 在此,第二个参数应该是资源名称,并且绝不能使用任何int值对其进行硬编码。 This is the identifier of the layout which will be used for creating each row in your list. 这是布局的标识符,将用于创建列表中的每一行。 If you just want to display the list of names you can use - android.R.layout.simple_list_item_1 which is a standard available in android for displaying just String values. 如果您只想显示名称列表,则可以使用-android.R.layout.simple_list_item_1 ,这是android中仅显示字符串值的可用标准。

So change your code to 因此,将您的代码更改为

super(context, android.R.layout.simple_list_item_1, ings);     

暂无
暂无

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

相关问题 android.content.res.Resources $ NotFoundException:资源ID#0x0 - 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错误 - android.content.res.Resources$NotFoundException: Resource ID #0x0 Android error 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 CheckedTextView 错误:android.content.res.Resources$NotFoundException:资源 ID #0x0 - CheckedTextView error :android.content.res.Resources$NotFoundException: Resource ID #0x0 无法在 textview 中设置文本。错误:-android.content.res.Resources$NotFoundException:字符串资源 ID #0x0 - Faild to settext in textview.Error:-android.content.res.Resources$NotFoundException: String resource ID #0x0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM