0

我尝试自定义 SimpleCursorAdapter 来解码一个值并设置为 textview 但不工作

我的自定义 simplecursoraapter 类:

public class simpledecode extends SimpleCursorAdapter{

private NotesDbAdapter mDbHelper;
public simpledecode(Context context, int layout, Cursor c, String[] from,
        int[] to) {
    super(context, layout, c, from, to);
}
@Override
public void setViewText(TextView v, String text) {
    // TODO Auto-generated method stub
    super.setViewText(v, text);
    v.setText(mDbHelper.base64toString(text));
}

我的解码方法:

public String base64toString(String text) 
{
    byte[] data1 = Base64.decode(text, Base64.DEFAULT);
    String text1 = null;
    try {
        return text1 = new String(data1, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}
4

1 回答 1

0

这是因为该行中的文本为空byte[] data1 = Base64.decode(text, Base64.DEFAULT);

您可以在方法的开头添加空检查

public String base64toString(String text) 
{
    if (text == null) return null;

    byte[] data1 = Base64.decode(text, Base64.DEFAULT);
    String text1 = null;
    try {
        return text1 = new String(data1, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}
于 2013-09-15T12:11:01.487 回答