java – 找不到资源异常

前端之家收集整理的这篇文章主要介绍了java – 找不到资源异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我引用我的一个映射EditText对象的类方法的行中获得了Resource Not Found Exception.我不明白为什么我会遇到这个问题.

我有一个名为store.java的简单java类,它只是映射来自微调器和EditText的数据.以及一个名为SpinPizza.java的类,用于打印它们的值.

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

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);
            }
        });
    }
}

解决方法

数量是一个int:
public int getQuantity()

所以你应该使用这个:

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

否则操作系统会尝试为该int找到一个不存在的资源.

详细说明:EditText.setText()方法被重载,因此它具有String的版本(setText(CharSequence文本))和字符串资源id的版本(setText(int resid)).

原文链接:https://www.f2er.com/java/127373.html

猜你在找的Java相关文章