java 资源未找到异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5444713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:10:07  来源:igfitidea点击:

Resource Not Found Exception

javaandroidandroid-edittext

提问by Lakshay Gupta

I am getting the Resource Not Found Exception in the line where I refer to one of my class methods that maps an EditTextobject. I don't understand why I am getting this problem.

我在引用映射EditText对象的类方法之一的行中收到“Resource Not Found Exception” 。我不明白为什么我会遇到这个问题。

I have a simple java class named store.javathat just maps the data from the spinners and EditText.and a class called SpinPizza.javathat prints their value.

我有一个简单的 java 类store.java,它只是映射来自微调器的数据,EditText.还有一个称为SpinPizza.java打印它们的值的类。

Store.java

商店.java

package com.Lak;

import android.os.Parcel;
import android.os.Parcelable;

public class store implements Parcelable {

    @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public store createFromParcel(Parcel in) {
            return new store(in);
        }

        public store[] newArray(int size) {
            return new store[size];
        }
    };
    private static final long serialVersionUID = 1L;
    private String pizzaname;
    private String pizzasize;
    private int n;

    public store() {
    }

    public store(Parcel source) {
          /*
           * Reconstruct from the Parcel
           */
        n = source.readInt();
        pizzaname = source.readString();
        pizzasize = source.readString();
    }

    public void setOrder(String name, String size, int qty) {
        pizzaname = name;
        pizzasize = size;
        n = qty;
    }

    public String getPizzaName() {
        return pizzaname;
    }

    public int getQuantity() {
        return n;
    }

    public String getPizzaSize() {
        return pizzasize;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {

        dest.writeInt(n);
        dest.writeString(pizzaname);
        dest.writeString(pizzasize);
    }

    /** Called when the activity is first created. */
}

SpinPizza.java

SpinPizza.java

package com.Lak;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SpinPizza extends Activity {

    private static final long serialVersionUID = 1L;
    store B[] = new store[10];
    int n, i, num;
    Spinner s = null, s1 = null;
    EditText edittext = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drop);

        s = (Spinner) findViewById(R.id.spinner);

        ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.pizzaarray, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);

        s1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<?> adapter1 = ArrayAdapter.createFromResource(this, R.array.sizearray, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(adapter1);

        edittext = (EditText) findViewById(R.id.edittext);
        i = 0;
        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
                        (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) {
                    // Perform action on key press

                    B[i] = new store();
                    //n=Integer.parseInt(edittext.getText().toString());

                    // num = Float.valueOf(edittext.getText().toString());

                    try {
                        num = Integer.parseInt(edittext.getText().toString());
                    } catch (NumberFormatException nfe) {
                        System.out.println("Could not parse " + nfe);
                    }

                    B[i].setOrder(s.getSelectedItem().toString(), s1.getSelectedItem().toString(), num);

                    TextView objText = (TextView) findViewById(R.id.pl);
                    TextView objText1 = (TextView) findViewById(R.id.pl2);
                    TextView objText2 = (TextView) findViewById(R.id.pl3);

                    objText.setText(B[i].getPizzaName());
                    objText1.setText(B[i].getPizzaSize());
                    objText2.setText(B[i].getQuantity());  //**RESOURCE NOT FOUND EXCEPTION**
                    i++;

                    Toast.makeText(SpinPizza.this, edittext.getText(), Toast.LENGTH_SHORT).show();

                    return true;
                }
                return false;
            }
        });

        Button next1 = (Button) findViewById(R.id.bill);

        next1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Bill.class);
                // store B= new store();
                myIntent.putExtra("myclass", B);
                myIntent.putExtra("len", i);
                int j;

                for (j = 0; j < i; j++)
                //{myIntent.putExtra("my",s.getSelectedItem().toString());
                // myIntent.putExtra("my1",s1.getSelectedItem().toString());
                // }
                {
                    myIntent.putExtra("my", B[j].getPizzaName());
                    myIntent.putExtra("my1", B[j].getPizzaSize());
                    myIntent.putExtra("my2", B[j].getQuantity());
                }

                startActivityForResult(myIntent, 0);
            }
        });
    }
}

回答by Vit Khudenko

Quantity is an int:

数量是一个整数:

public int getQuantity()

So you should use this:

所以你应该使用这个:

objText2.setText(String.valueOf(B[i].getQuantity()));

Otherwise the OS tries to find a resource for that int, which is not present.

否则,操作系统会尝试为该 int 查找资源,但该资源不存在。

A detailed explanation: EditText.setText()method is overloaded so it has a version for a String(setText(CharSequence text)) and a version for a string resource id (setText(int resid)).

详细解释:EditText.setText()方法被重载,因此它有一个String( setText(CharSequence text)) 的版本和一个字符串资源 id ( setText(int resid)) 的版本。