【问题标题】:how to use simple getter and setter with android如何在android中使用简单的getter和setter
【发布时间】:2013-05-03 17:54:51
【问题描述】:

我有一个带有存储国家/地区的数组列表的国家/地区。我创建了一个 get 和 set 来从数组列表中的指定索引添加和获取项目,但我不会工作。每当我从 arraylist 调用索引时,我都会得到一个超出范围的异常,因为数组是空的或者至少看起来是空的。

public class country extends Application {

    public ArrayList<country> countryList = new ArrayList<country>();
    public String Name;
    public String Code;
    public String  ID;

    public country()
    {

    }

    public country(String name, String id, String code)
    {
        this.Name = name;
        this.ID = id;
        this.Code = code;
    }

    public void setCountry(country c)
    {
        countryList.add(c);
    }

    public country getCountry(int index)
    {   
        country aCountry = countryList.get(index);
        return aCountry;
    }

调用我使用的设置器。我在 for 循环中执行此操作,因此它添加了 200 多个元素

country ref = new country();

ref.setCountry(new country (sName, ID, Code));

那么当我想得到一个索引时

String name = ref.countryList.get(2).Name;

我做了同样的事情,但使用了本地数组列表,它填充得很好,我能够显示名称,所以数据源不是问题,无论我做错了设置并在国家类的数组列表中获取数据

【问题讨论】:

  • 与国家/地区数组列表一起创建的本地数组列表包含 200 多个对象,所以这不是问题。 resultbox.setText(countryNames.get(2).Name); // 打印一个名字 resultbox.setText(ref.getCountry(2).Name); //返回错误
  • 执行 ref.countryList.get(0).Name 因为您只在列表中添加了一项;
  • 向我们展示添加 200 多个对象的代码。您很可能会将其添加到不同的列表中。

标签: java android arraylist getter-setter indexoutofboundsexception


【解决方案1】:

您访问了一个不存在的索引。您只需添加一个您只能访问的国家/地区:

String name = ref.countryList.get(0).Name;

你真的应该重新考虑你的设计。 public 属性不是最佳实践方式。这就是为什么您应该首先编写 getter 和 setter 方法的原因。

你应该这样做:

public Country getCountry(int index)
{
    if(index < countryList.size())
    {
        return countryList.get(index);
    }
    return null;
}

【讨论】:

    【解决方案2】:

    String name = ref.countryList.get(2).Name; 中,您试图获取列表中的 第三个 元素,而您只添加了 一个...
    应该是String name = ref.countryList.get(0).Name;,你需要先检查一下没有收到空指针异常

    【讨论】:

    • 我更新了我上面的评论,但我使用了一个 for 循环,所以 arraylist 中应该有超过 3 个项目。本地版本包含 241
    【解决方案3】:

    执行ref.countryList.get(0).Name,因为您只在列表中添加了一项。

    我会建议更多像

     ref.countryList.get(0).getName()
    

    【讨论】:

    • 不,列表中应该有 200 多个项目,我使用 for 循环将它们全部添加。我为测试而制作的本地版本的 arraylist 里面有 241 个项目,我做同样的事情来添加到两者中
    • 那我不认为有任何异常的原因,你能发布你的异常堆栈跟踪
    • com.example.wsdl1.MainActivity$1$1.run(MainActivity.java:95) android.os.Handler.handleCallback(Handler.java:615) android.os.Handler.dispatchMessage(Handler. java:92) android.os.Looper.loop(Looper.java:137) android.app.ActivityThread.main(ActivityThread.java:4745) java.lang.reflect.Method.invokeNative(Native Method) java.lang.reflect .Method.invoke(Method.java:511) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) dalvik .system.NativeStart.main(Native Method)
    • 你说你遇到了出站异常。我在这里没有看到
    • 这就是出现在 logcat 中的内容。当我将消息输出到屏幕时,我越界了。我更新了丹尼尔发布的代码,现在我得到了一个空指针异常,但它是一样的
    猜你喜欢
    • 2011-11-09
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多