例子如下:
public class sendInformation{
public void test() throws Exception {
Uri uri = SuspiciousActivityTable.CONTENT_URI;
getContentResolver().update(uri, values2, where,new String[]{"Null"});
}
}
这样系统会说getContentResolver()不存在,需要activity或者service来实现。
这个类的test()方法肯定是有activity或者service调用的,所以可以写一个有参构造函数
public class sendInformation{private Context context;
public sendInformation(Context context){
this.context = context;
}
public void test() throws Exception {
Uri uri = SuspiciousActivityTable.CONTENT_URI;
context.getContentResolver().update(uri, values2, where,new String[]{"Null"});
}
}
这样就可以了。

本文讨论了在Android应用开发中遇到的使用ContentResolver更新数据时出现的错误,并提供了一个通过构造函数注入Context的方法来解决该问题的解决方案。
1077

被折叠的 条评论
为什么被折叠?



