这是一个调用getDetails类的程序。当它调用getDetails时会出现此错误。我尝试了getDetails代码,它很有效。但是当我从这个类中调用它时,它会向我显示此错误。我上传了错误图片:
它认为这与这一行有关:
Cursor names = context.getContentResolver().query(uri, projection, null, null, null);
代码
public class SmsFilter extends BroadcastReceiver {
SharedPreferences preferences = null ;
Context context = null;
@Override
public void onReceive(Context context, Intent intent) {
preferences = context.getSharedPreferences("modes",Context.MODE_PRIVATE);
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[])extras.get("pdus");
if (pdus.length < 1) return; // Invalid SMS. Not sure that it's possible.
StringBuilder sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (sender == null) sender = message.getOriginatingAddress();
String text = message.getMessageBody();
if (text != null) sb.append(text);
}
if (preferences.getBoolean("enable", true)==false) {
if (preferences.getBoolean("all", true)){ abortBroadcast();}
else if (preferences.getBoolean("blacklist", true)){
boolean str=Search.search(sender);
if (sender != null && str ==true) {
abortBroadcast();
}
}
else if (preferences.getBoolean("contact", true)&& getDetails(sender)){
abortBroadcast();
}
else {}}
return;
}
}
// ...
}
boolean getDetails(String Phonumber){
Boolean answer=null;
Log.i("sss", "to here");
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
// ContentResolver cr = getContentResolver();
// Cursor cur = context.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Log.i("sss", "to here1");
Cursor names = context.getContentResolver().query(uri, projection, null, null, null);
//int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
Log.i("sss", "to here n"+indexNumber);
names.moveToFirst();
do {
Log.i("sss", names.getString(indexNumber));
String number = names.getString(indexNumber);
number.replace(" ", "");
if(number.startsWith("0")){
number="+98"+context.getString(1,number.length());}
if(Phonumber.startsWith("0")){
Phonumber="+98"+Phonumber.substring(1,Phonumber.length());}
if (number.equals(Phonumber))
{answer=true;}
} while (names.moveToNext());
return answer;
}
}
答案 0 :(得分:1)
您在类的顶部声明了一个成员,但从未为其分配有效的引用:
Context context = null;
因此,当您致电时,context
仍为null
:
Cursor names = context.getContentResolver().query(uri, projection, null, null, null);
因此,NullPointerException
。
选项1
确保您要么指定有效的引用(在onReceive()
中):
this.context = context;
选项2
或者将引用作为方法参数传递给getDetails()
:
else if (preferences.getBoolean("contact", true) && getDetails(context, sender)) { ... }
该方法本身将如下所示:
boolean getDetails(Context context, String Phonumber) { ... }
如果您使用第二个选项,则可以删除显示Context context = null;
的行,因为它已不再使用。您的IDE可能已经暗示过这一点(我知道Android Studio确实存在,但不确定Eclipse)。