Resource Not Found Exception

14,675

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.

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

Share:
14,675
Lakshay Gupta
Author by

Lakshay Gupta

Updated on June 11, 2022

Comments

  • Lakshay Gupta
    Lakshay Gupta almost 2 years

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

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

    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);
                }
            });
        }
    }
    
    • Admin
      Admin about 13 years
      This is a LOT of code. Could you please edit this to include only the relevant sections and point out which line is causing the error?
    • asenovm
      asenovm about 13 years
      do you properly define the editText in your xml?
    • Lakshay Gupta
      Lakshay Gupta about 13 years
      @Phoenix-> I know this is quite some code..I have mentioned the line where I get the exception so pls look till there and ignore the further code...You can also ignore the first class since I dont think there is a problem in that...Just see SpinPizza .java...n m sorry for the long code....
    • Lakshay Gupta
      Lakshay Gupta about 13 years
      @iLate: Ya i dont see any problem in that...Herz the xml.. <EditText android:id="@+id/edittext" android:layout_width="fill_parent" android:hint="@string/numberHint" android:layout_height="wrap_content" android:numeric="decimal"/>
  • Lakshay Gupta
    Lakshay Gupta about 13 years
    @Arhimed->Thanx a lot..It worked..:-) But I have one more problem...I am not able to send B[i] to another activity. I am using Parcelable and I am following the correct syntax...But it gives shows Null Pointer exception when I try to access elements of B[i]...I have a feeling that the line B[i].setOrder(s.getSelectedItem().toString(),s1.getSelectedI‌​tem().toString(),num ); isnt right...I think there is another problem with Edittext as the value doesnt get stored properly and so I am unable to access it through B[i] in the other activity..I ll b really obliged if u could fix this one too...
  • Vit Khudenko
    Vit Khudenko about 13 years
    @user677124: You are messing things a bit, don't you? :) Please, accept the answer as a solution for "I am getting the Resource Not Found Exception" and ask ANOTHER question about passing Parcelable to another Activity.
  • mjj1409
    mjj1409 almost 10 years
    Seems to violate the principle of least surprise in my opinion.