1

我将一组数字存储为字符串(我从共享首选项中获取字符串),然后尝试解析它。但是当我使用 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);
}

}

它哪里出错了?

4

1 回答 1

0

在这些之间添加一条线

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

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

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

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

于 2013-03-14T04:54:44.653 回答