gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:40:10 24 4
gpt4 key购买 nike

运行尝试显示对象的 ListView 的 fragment 时会发生此异常。我已经研究了该异常,但其他情况的解决方案似乎与“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);     

关于java - android.content.res.Resources$NotFoundException : Resource ID #0x0 java exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51833447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com