1

在 for 循环和打印日志之后调用插入函数时抛出 UnknownFormatConversionException,我捕获了该异常但如何删除它?不知道是什么原因发生的

try {
        List<Status> timeline = 
            ((YambaApp) getApplication()).getTwitter().getPublicTimeline();
        for (Status status : timeline) {
            Log.d(TAG, String.format("%s: %s", status.user.name,
                    status.text));
            statusData.insert(status);

            }
    } catch (TwitterException e) {
        Log.d(TAG,"Failed to access twitter service  ",e);
    } catch(NullPointerException a){
        Log.d(TAG,"Pref had been changed and twitter object is null",a);
    } catch (UnknownFormatConversionException u){
        Log.d(TAG,"exception "+ u);
    }

下面是插入函数的代码:

public void insert(Status status){
    db=dbHelper.getWritableDatabase();

    ContentValues value= new ContentValues();
    //converting BigInteger to String
    BigInteger bi=status.id;
    String status_id= new String(bi.toByteArray());


    value.put(C_ID, status_id);
    value.put(C_CREATED_AT, status.createdAt.getTime());
    value.put(C_TEXT, status.user.name);
    value.put(C_TEXT, status.text);

    db.insert(TABLE, null, value);
}

这是抛出异常的完整堆栈跟踪

10-10 06:05:22.648: E/AndroidRuntime(627): FATAL EXCEPTION: IntentService[RefreshService]
10-10 06:05:22.648: E/AndroidRuntime(627): java.util.UnknownFormatConversionException: Conversion: i
10-10 06:05:22.648: E/AndroidRuntime(627):  at java.util.Formatter$FormatToken.unknownFormatConversionException(Formatter.java:1397)
10-10 06:05:22.648: E/AndroidRuntime(627):  at     java.util.Formatter$FormatToken.checkFlags(Formatter.java:1334)
10-10 06:05:22.648: E/AndroidRuntime(627):  at java.util.Formatter.transform(Formatter.java:1440)
10-10 06:05:22.648: E/AndroidRuntime(627):  at java.util.Formatter.doFormat(Formatter.java:1079)
10-10 06:05:22.648: E/AndroidRuntime(627):  at java.util.Formatter.format(Formatter.java:1040)
10-10 06:05:22.648: E/AndroidRuntime(627):  at java.util.Formatter.format(Formatter.java:1009)
10-10 06:05:22.648: E/AndroidRuntime(627):  at java.lang.String.format(String.java:1998)
10-10 06:05:22.648: E/AndroidRuntime(627):  at java.lang.String.format(String.java:1972)
10-10 06:05:22.648: E/AndroidRuntime(627):  at com.example.yamba.StatusData$DbHelper.onCreate(StatusData.java:59)
10-10 06:05:22.648: E/AndroidRuntime(627):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
10-10 06:05:22.648: E/AndroidRuntime(627):  at com.example.yamba.StatusData.insert(StatusData.java:32)
10-10 06:05:22.648: E/AndroidRuntime(627):  at com.example.yamba.RefreshService.onHandleIntent(RefreshService.java:32)
10-10 06:05:22.648: E/AndroidRuntime(627):  at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-10 06:05:22.648: E/AndroidRuntime(627):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 06:05:22.648: E/AndroidRuntime(627):  at android.os.Looper.loop(Looper.java:137)
10-10 06:05:22.648: E/AndroidRuntime(627):  at android.os.HandlerThread.run(HandlerThread.java:60)
10-10 06:05:22.968: W/IInputConnectionWrapper(627): showStatusIcon on inactive InputConnection
4

1 回答 1

1

在你的insert()方法改变

String status_id= new String(bi.toByteArray());

String status_id = bi.toString();

调用bi.toByteArray()将返回一个字节数组,其中包含BigInteger. 您无法String按照您的操作方式将其正确转换。只需使用提供的toString()方法BigInteger

于 2012-10-09T19:27:30.790 回答