【问题标题】:Parsing error in AndroidAndroid中的解析错误
【发布时间】:2013-03-02 08:10:03
【问题描述】:

我将一个数字数组存储为字符串(我从共享首选项中获取字符串),然后尝试解析它。 但是当我使用 parseInt 时,我的应用程序崩溃了。活动 Second 由 Main 类调用。

    public class Second extends Activity {

public int[] x = new int[50];
public int[] y = new int[50];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    SharedPreferences data= getSharedPreferences("data",0);
    SharedPreferences.Editor editor= data.edit();

    StringBuilder str = new StringBuilder();
    str.append(data.getString("val", "0")).append(",").append(getIntent().getExtras().getString("thetext"));
    String end = str.toString();
    editor.putString("val", end);
    editor.commit();

    //EditText et1= (EditText) findViewById(R.id.editText2);

    //et1.
    String savedString = data.getString("val", "0");
    savedString.replaceAll("\\s","");
    String[] st = savedString.split(",");
    int i;
    for(i=0;i<st.length;i++){
        st[i].trim();
        Log.d("Debug" , "st["+i+"] = "+st[i]);
        x[i] = Integer.valueOf(st[i]);
       y[i]=i;}
}



public void lineGraphHandler (View view)
{
    LineGraph line = new LineGraph();
    Intent lineIntent = line.getIntent(this);
    startActivity(lineIntent);
}

}

哪里出错了?

【问题讨论】:

  • 什么是 logcat 错误?
  • 还共享您保存在 SharedPreferences 中的最终数字数组
  • 我已经更新了有问题的代码。这是 logcat imgur.com/m87QMry。这里有问题。无论我在应用程序中输入什么,它总是在 st(0,1,1,2,2,2) 中显示这 6 个值
  • 知道了。现在我直接从 TextView 字符串中删除空格

标签: android integer android-activity


【解决方案1】:

在这些之间添加一条线

    st[i].replaceAll("\\s","");

    Log.d("Debug" , "st["+i+"] = "+st[i])

    x[i] = Integer.parseInt(st[i]);

看看这是不是一个可转换的字符串,或者有一些不小心插入的字母不能转换成int?

【讨论】:

  • 注意:需要导入android.util.log,然后查看logcat。
  • 试试Integer.valueOf(st[i])
  • 谢谢 :) 但这也行不通。我想这里还有其他问题。我已经在问题中更新了我的课程,请看一下。
  • 对不起,你能不能再试试short
  • 知道了。现在我直接从 TextView 字符串中删除空格
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
相关资源
最近更新 更多