java - arrayList 字符串搜索 : NullPointerException

标签 java android arraylist nullpointerexception singleton

我会尽力解释这一点。 (我对 Java 和 Android 都还是新手)

问题:

我试图通过搜索 arrayList 将传入的号码字符串与 Contact 对象的号码字符串进行比较。

背景:

我能够将联系人从 arrayList 加载到不同的 View (ListView、textView 等)中,因此我知道方法和对象正在工作。我遇到问题的是这个新类 (RingerService)。

设计

我在名为 contactStorage 的类中有一个联系人数组列表。 它按预期工作,用于显示不同的 View :

//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;还是让程序运行?

最佳答案

您正在尝试将字符串与 c.getNumber() == number 中的 == 进行匹配,这将检查两个对象引用是否相等

使用c.getNumber().equals(number)

关于java - arrayList 字符串搜索 : NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24319333/

相关文章:

java - 分割字符串并存储在 HashMap Java 8 中的模式

java - Java 桌面应用程序中的广告集成

java - 用 Canvas 绘制人物并设置其角度的最佳方法是什么?

android - 移除 ViewPager2 滚动动画

android - 三星特定的 webview 问题

Java 2 昏暗数组类

java - 行索引在 JTable 中始终显示为 -1

java - 根据元素值的继承获取 XML 节点

android - 更改 Android 项目中每种风格的启动器图标和名称

java - ArrayList 初始化为 null?