【发布时间】:2014-06-20 03:06:32
【问题描述】:
我会尽力解释这一点。 (我还是 Java 和 Android 的新手)
问题:
我正在尝试通过搜索 arrayList 将传入的数字字符串与联系人对象的数字字符串进行比较。
背景:
我能够将数组列表中的联系人加载到不同的视图(ListView、textView 等)中,因此我知道方法和对象正在工作。我遇到的问题是这个新类 (RingerService)。
设计
我在一个名为 contactStorage 的类中有一个联系人数组列表。 它可以按预期工作以显示不同的视图:
//constructor with context to access project resources and instantiate from JSONfile to arrayList
private ContactStorage(Context appContext){
mAppContext = appContext;
mSerializer = new ContactJSONer(mAppContext, FILENAME);
try{
mContacts = mSerializer.loadContacts();
}catch (Exception e){
mContacts = new ArrayList<Contact>();
Log.e(TAG, "No contacts available, creating new list: ", e);
}
}
//get method to only return one instance from the constructor
public static ContactStorage get(Context c){
if (sContactStorage == null){
sContactStorage = new ContactStorage(c.getApplicationContext());
}
return sContactStorage;
}
//for ringer service to find matching number
public Contact getContactNumber(String number){
for (Contact c: mContacts){
if(c.getNumber().replaceAll("[^0-9]", "").equals(number))
return c;
}
return null;
}
当我在下面的 RingerService 类中调用上面的 get 方法时,就会出现问题。具体来说,我在 onCallStateChanged 上收到 NullPointerException:
private Contact mContact;
private String number;
private Context mContext;
@Override
public void onCreate(){
mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mPhoneStateListener = new PhoneStateListener(){
// state change
@Override
public void onCallStateChanged(int state, String incomingNumber){
if (state == 1 ){
try{
mContact = ContactStorage.get(mContext).getContactNumber(incomingNumber);
number = mContact.getNumber();
Log.d(TAG, state+" received an incoming number: " + number);
}catch(Exception e){
Log.d(TAG, " exception: " + e);
}
} else {
Log.d(TAG, state+" number not found" + incomingNumber);
}
}
};
super.onCreate();
}
疑难解答:
1. 我删除了对数字的引用 (number = mContact.getNumber();) - 在这种情况下程序运行良好。我可以向模拟器发送一个测试调用,并且日志消息正确显示,测试编号为 arg。我认为这可能是数组搜索在 getContactNumber 类中的工作方式。是不是一直找不到匹配的值,导致null?
2. 我还认为,由于这是一项服务,所以在调用 ContactStorage.get(Context c) 方法时,我无法获得正确的上下文。
3. 如果我设置了我的 mContact 引用并且没有找到数字匹配,mContact = null;还是让程序运行?
【问题讨论】:
-
因为
getContactNumber可能会返回null,然后你右转并调用mContact.getNumber(),其中mContact是getContactNumber的返回......这是一个等待发生的NullPointerException。 -
您是否在任何地方初始化了您的上下文 (
mContext)?我觉得你的上下文是空的。调试和检查。 -
我在 ContactStorage 中使用 getApplicationContext() 的原因是我希望在应用程序范围内的多个领域(活动、片段、服务)中使用模型数据。既然服务是一个上下文,我可以简单地设置
Context mContext = RingerService(this)直接访问它吗?
标签: java android arraylist nullpointerexception singleton