【问题标题】:edittext making the app crashing when a text is introduced instead of a number当引入文本而不是数字时,edittext 使应用程序崩溃
【发布时间】:2015-10-06 17:25:19
【问题描述】:

我有以下 xml 文件

<?xml version="1.0" encoding="utf-8"?>

<string-array name="my_array">

    <item>rome 999 3 4 5 7 9 11 17</item>

</string-array>

我用这个结构初始化一个sqlite表:

    public static abstract class FeedEntry implements BaseColumns {
    public static final String TABLE_NAME = "entry";
    public static final String COLUMN_NAME_ENTRY_ID = "entryid";
    public static final String COLUMN_NAME_TITLE = "title";
    public static final String COLUMN_NAME_SCHEDULE = "schedule";
}

这是我解析值并将它们放入表中的代码

for (String item : myArray){
        String[] split = item.split("\\s+");

        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, split[0]);
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, split[1]);
        String schedule="";

        String[] stringArray = item.split("\\s+");
        int length = stringArray.length;

        for (int i = 2; i < length; i++) {
            System.out.println(i);
            if(schedule.trim().length()>0){
                schedule += split[i]+" ";
            }else{
                schedule = split[i];
            }
        }


        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_SCHEDULE, schedule);
        db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
    }

现在,之后我对它发出一个 sqlite 请求,例如:

        Cursor cursor = db.query(
            FeedReaderContract.FeedEntry.TABLE_NAME,  
            projection,                               
            FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE+" = " + string ,                              
            null,                            
            null,                                     
            null,                                   
            sortOrder                                 
    );

我是这样用光标新建一个edittext的:

for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
            result = result + cursor.getString(iRow) + " " + cursor.getString(iName) +
                    " " + cursor.getString(iSchedule) +
                    "\n";
        }
TextView textview = new TextView(getApplicationContext());
        textview.setText(result);
        RelativeLayout myLayout;
        myLayout = (RelativeLayout) findViewById(R.id.content_main);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

TextView tv = (TextView) findViewById(R.id.button);
        textview.setTextColor(Color.RED);
        params.addRule(RelativeLayout.BELOW, tv.getId());
        myLayout.addView(textview, params);

但是,如果我更改 xml 文件并在第二个元素中引入文本,例如:

 <?xml version="1.0" encoding="utf-8"?>

<string-array name="my_array">

    <item>rome line1 3 4 5 7 9 11 17</item>

</string-array>

如果我重复一些事情并通过查找文本 line1 来发出 sqlite 请求,则应用程序崩溃。即在上面的代码中,sqlite 只需要一个数字而不是一个文本。我从这样的代码中不明白这种行为的起源。另外,我怎样才能使它也与文本一起使用?

【问题讨论】:

  • 你能贴出你实际解析字符串并将其写入数据库的代码吗?

标签: android xml sqlite android-edittext


【解决方案1】:

我的猜测是您希望罗马显示为标题而不是 999。如果是这种情况,那么最简单的解决方法是将您的字符串数组更改为读取为

&lt;item&gt;999 Rome 3 4 5 7 9 11 17&lt;/item&gt;

【讨论】:

    猜你喜欢
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多