1

我有一个应用程序,它记录了前台活动的几个属性。我使用本地数据库来存储数据,当用户请求显示时,我从该数据库中获取数据并将其显示在列表视图中。它在 GB、ICS 上运行良好。但是 JellyBean 中的数据库事务非常不规则。空的被退回。我无法知道这其中的原因。我已经在 Samsung young、HTC、索尼爱立信 Xperia 和三星 S3 上对此进行了测试。问题在于已更新为 JB 的 S3。以下是 logCat 跟踪。请帮忙。

try {
        db.open();
        Cursor cursor = db.getStartTime(lastAppName); ;
        if(cursor.moveToFirst()) {
        do {
            DiffStart = cursor.getString(0);
            DiffBright = cursor.getString(1);
            Log.d("wifiRun","brightness: " +DiffBright);

            }while(cursor.moveToNext());
           }
           SimpleDateFormat dateFormat = new SimpleDateFormat(
                                        "HH:mm:ss");
            try {
               Date startTime = dateFormat.parse(DiffStart);
               Date endTime = dateFormat.parse(EndingTime);
               diff = (endTime.getTime() - startTime.getTime()) / 1000;
               Difference = String.valueOf(diff);
           try {
               double brightnessDouble = Double.parseDouble(DiffBright);
               Log.d("wifiRun","brightnessDouble: " +brightnessDouble);
               //Do my energy calculation.
               } catch (NullPointerException npe) {
                npe.printStackTrace();
            }

                            } catch (ParseException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (NullPointerException npe) {
                                npe.printStackTrace();
                            }



                            db.update(lastAppName, EndingTime, Difference, AppEnergy);

Cursor cur = db.Select(lastAppName);
if(cursor.moveToFirst()) {
                                    do {
                                    testBright = cursor.getString(0);
Log.d("wifiRun","testBright: " +testBright);
                                    testEnergy = cursor.getString(1);
                                    Log.d("wifiRun","testEnergy: " +testEnergy);
                                    }while(cursor.moveToNext());
                                }
       db.close();
    } catch (SQLException sql) {
      sql.printStackTrace();
    }

LogCat如下:

06-11 12:57:31.258: D/wifiRun(32469): brightness: 171
06-11 12:57:31.258: D/wifiRun(32469): brightness: null
06-11 12:57:31.258: D/wifiRun(32469): brightness: null
06-11 12:57:31.258: D/wifiRun(32469): brightness: 171
06-11 12:57:31.258: D/wifiRun(32469): brightness: 171
06-11 12:57:31.258: D/wifiRun(32469): brightness: 171
06-11 12:57:31.258: D/wifiRun(32469): brightness: 171
06-11 12:57:31.258: D/wifiRun(32469): brightness: 171
06-11 12:57:31.258: D/wifiRun(32469): brightness: 171
06-11 12:57:31.258: D/wifiRun(32469): brightness: 171
06-11 12:57:31.263: D/wifiRun(32469): brightnessDouble: 171.0
06-11 12:57:31.263: D/wifiRun(32469): AppEnergy: 13.977
06-11 12:57:31.268: D/wifiRun(32469): testEnergy: null
06-11 12:57:31.268: D/wifiRun(32469): testBright: 171
06-11 12:57:31.268: D/wifiRun(32469): testEnergy: null
06-11 12:57:31.268: D/wifiRun(32469): testBright: null
06-11 12:57:31.268: D/wifiRun(32469): testEnergy: null
06-11 12:57:31.268: D/wifiRun(32469): testBright: null
06-11 12:57:31.268: D/wifiRun(32469): testEnergy: 313.776
06-11 12:57:31.268: D/wifiRun(32469): testBright: 171
06-11 12:57:31.268: D/wifiRun(32469): testEnergy: 313.776
06-11 12:57:31.268: D/wifiRun(32469): testBright: 171
06-11 12:57:31.268: D/wifiRun(32469): testEnergy: 2.795
06-11 12:57:31.268: D/wifiRun(32469): testBright: 171
06-11 12:57:31.268: D/wifiRun(32469): testEnergy: 36.339
06-11 12:57:31.268: D/wifiRun(32469): testBright: 171
06-11 12:57:31.268: D/wifiRun(32469): testEnergy: 6.289
06-11 12:57:31.268: D/wifiRun(32469): testBright: 171
06-11 12:57:31.273: D/wifiRun(32469): testEnergy: 0.699
06-11 12:57:31.273: D/wifiRun(32469): testBright: 171
06-11 12:57:31.273: D/wifiRun(32469): testEnergy: 13.977
06-11 12:57:31.273: D/wifiRun(32469): testBright: 171
4

1 回答 1

0

你可以用不同的方式来做这件事,这也很常见。

只需在一个单独的类中创建所有数据库内容并在那里添加所有类型的方法。为了从表中获取数据,您可以执行以下操作:

  // put all column names in a string array
  String[] columns = new String[] { ROW_ID, ROW_NAME };

  // create the cursor
  Cursor c = ourDatabase.query(your_table_name, columns, null, null, null, null, null);
  String result = "";
  //fetch the column indexes through the column name..
  int iRow = c.getColumnIndex(ROW_ID);
  int iName= c.getColumnIndex(ROW_NAME);

  //move the cursor from start to after last one by one and put the data in ur string..
  for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        result += c.getString(iRow) + "\n" + c.getString(iName) + "\n";

  // at last return the result for using in your activity..
  return result;

这很容易,希望它能保护您免受您现在面临的错误的影响。

于 2013-09-10T14:17:02.240 回答