草庐IT

java - 未找到资源异常

coder 2023-11-28 原文

我在引用映射 EditText 的类方法之一的行中遇到资源未找到异常目的。我不明白为什么我会遇到这个问题。

我有一个名为 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);
            }
        });
    }
}

最佳答案

数量是一个整数:

public int getQuantity()

所以你应该使用这个:

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

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

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

关于java - 未找到资源异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5444713/

有关java - 未找到资源异常的更多相关文章

  1. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  2. ruby-on-rails - Rails - 乐观锁定总是触发 StaleObjectError 异常 - 2

    我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd

  3. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  4. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  5. ruby - 在 Ruby 中重新分配常量时抛出异常? - 2

    我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案

  6. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  7. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  8. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  9. ruby-on-rails - capybara ::ElementNotFound:无法找到 xpath "/html" - 2

    我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'

  10. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

随机推荐