【问题标题】:How modify array inside a method?如何修改方法内的数组?
【发布时间】:2015-07-19 12:10:21
【问题描述】:

我有一堂课:

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class workingOneWayAdapter extends BaseAdapter {

    private Context mContext;

    public workingOneWayAdapter(Context c) {
        mContext = c;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView workingLabel;
        if (convertView == null) {
            workingLabel = new TextView(mContext);
            workingLabel.setLayoutParams(new MyGridView.LayoutParams(85, 85));
            workingLabel.setPadding(10, 5, 5, 5);
            workingLabel.setTextColor(Color.parseColor("#000000"));
            workingLabel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
            workingLabel.setSingleLine();

            setTimes();

        } else {
            workingLabel = (TextView) convertView;
        }

        workingLabel.setText(workingOneWayArray[position]);
        return workingLabel;
    }

    String[] workingOneWayArray;

    void setTimes() {

        workingOneWayArray = new String[] { "00:00" };    
    }

    public int getCount() {
        return workingOneWayArray.length;
    }
}

但这会使我的应用程序崩溃。我需要编辑方法内的数组,因为然后可以从类的其他部分访问该数组。你能告诉我有什么问题吗?谢谢!

【问题讨论】:

  • 你能提供更多这个类的代码吗,因为看起来没什么问题,也尝试调试你的android应用程序
  • 应用在getCount()方法上崩溃,但我不知道为什么
  • 也许您在发出 getCount 之前没有调用 setTimes 方法。向我们展示您的完整代码
  • 我会建议创建这个类的构造函数并在那里初始化你的 workingOneWayArray =new String[0];并确保在调用 setTimes() 之后调用 getCount
  • 通过使用 Log.d("APP","EXECUTED"); 发现 setTimes() 实际执行与否。在这个函数内部并在 Logcat 中检查它

标签: java android arrays


【解决方案1】:

这段代码工作正常,但是如果我以相反的顺序调用你的两个方法,它会崩溃。

还可以考虑将 setTimes 调用放入构造函数中

public test() {
    setTimes();
}

或者简单地创建你的数组内联...

String[] workingOneWayArray = new String[] { "00:00" };

这是工作代码

public class test {


  String[] workingOneWayArray;

   void setTimes() {

     // THIS DOESN'T WORK

      workingOneWayArray = new String[] { "00:00" };

  }

  public int getCount() {
      return workingOneWayArray.length;
  }

  public static void main(String[] args) {
    test t = new test();

    t.setTimes();
    t.getCount();

  }

}

【讨论】:

  • 如果我这样做,我会得到:“无法应用 workingOneWayAdapter 中的(上下文)”
  • 如果你不能运行这个自包含的代码,你有比你的数组更大的问题
【解决方案2】:

如果数组从未被初始化,那么getCount() 方法将抛出异常,因为workingOneWayArray 还不是数组,所以没有.length 属性。确保在setTimes() 之前从未调用过getCount()

【讨论】:

    【解决方案3】:

    解决了这个问题:

    String[] workingOneWayArray;
    

    通过这个:

    String[] workingOneWayArray = new String[1];
    

    谢谢大家。

    【讨论】:

      猜你喜欢
      • 2013-10-24
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 2019-05-28
      • 1970-01-01
      • 2012-09-08
      • 2012-11-30
      相关资源
      最近更新 更多