当前位置: 首页 > 面试题库 >

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

夏长卿
2023-03-14
问题内容

运行片段尝试显示对象的ListView时会发生此异常。我已经研究了该异常,但针对其他情况的解决方案似乎与“
setText”函数有关,或者其他任何尝试将除字符串以外的任何内容显示为字符串的问题。我真的不确定还有什么要说的。提前致谢!

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布局

<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>

编辑(日志)

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'

问题答案:

发现您的错误。它在您的IngAdapter中的以下行中

super(context, 0, ings);

在此,第二个参数应该是资源名称,并且绝不能使用任何int值对其进行硬编码。这是布局的标识符,将用于创建列表中的每一行。如果您只想显示名称列表,则可以使用
-android.R.layout.simple_list_item_1 ,这是android中仅显示字符串值的可用标准。

因此,将您的代码更改为

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


 类似资料: