简体   繁体   English

arrayList字符串搜索:NullPointerException

[英]arrayList String search: NullPointerException

I will try to explain this as best I can. 我将尽力解释这一点。 (I'm still new with both Java and Android) (我还是Java和Android的新手)

Issue : 问题

I am trying to compare an incoming number string to a Contact object's number string by searching the arrayList. 我正在尝试通过搜索arrayList将传入的数字字符串与Contact对象的数字字符串进行比较。

Background : 背景

I am able to load Contacts from an arrayList into different views (ListView, textView, etc) so I know the methods and objects are working. 我能够将联系人从arrayList加载到不同的视图(ListView,textView等)中,因此我知道方法和对象都在工作。 It's this new class ( RingerService ) that I'm having the issue with. 我遇到的是这个新类( RingerService )。

Design 设计

I have an arrayList of contacts in a class named contactStorage . 我在名为contactStorage的类中具有联系人的arrayList It works as intended for displaying different views: 它按预期用于显示不同的视图:

//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;
}

when I call the get method above in the RingerService class below, that's when things break. 当我在下面的RingerService类中调用上面的get方法时,事情就此中断了。 To be specific, I am getting a NullPointerException on onCallStateChanged : 具体来说,我在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();
   }

Troubeshooting: Troubeshooting:

1. I've removed the reference to number ( number = mContact.getNumber(); ) - the program runs fine in that case. 1.我删除了对数字的引用( number = mContact.getNumber(); )-在这种情况下,程序可以正常运行。 I can send a test call to the emulator and the log message displays correctly with the test number arg. 我可以向仿真器发送测试调用,日志消息正确显示,并带有测试号arg。 I considered it could be the way in which the array searching works in getContactNumber class. 我认为这可能是数组在getContactNumber类中进行搜索的方式。 Is it never finding a matching value, resulting in null? 是否永远找不到匹配的值并导致null?

2. I also thought that since this is a service, I'm somehow not getting the right context when calling the ContactStorage.get(Context c) method. 2.我还认为,由于这是一项服务,因此我在调用ContactStorage.get(Context c)方法时无法获得正确的上下文。

3. if I set my mContact reference and no number match was found, would mContact = null; 3.如果我设置了我的mContact引用,但没有找到数字匹配,则mContact = null; still let the program run? 还是让程序运行?

You are trying to match strings with == in c.getNumber() == number which will check if two objects reference are equals 您正在尝试使用c.getNumber() == number ==匹配字符串,该字符串将检查两个对象引用是否相等

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

Thanks for the suggestions. 感谢您的建议。 It turns out that when testing an incoming phone call in Eclipse to an emulator device, the GUI field only takes numerics without spaces or hyphens: (1112221122) 事实证明,在Eclipse中测试到模拟器设备的传入电话时,GUI字段仅采用不带空格或连字符的数字: (1112221122)

Since my contact objects' number field is getting assigned through android's CONTACT_URI, the format is saved as a string (###) ###-#### . 由于我的联系人对象的数字字段是通过android的CONTACT_URI分配的,因此格式保存为字符串(###)###-#### This will never match, causing the nullPointerException error. 这将永远不会匹配,从而导致nullPointerException错误。 I updated the getContactNumber method to replace this formatting for the long string of numbers for any potential match. 我更新了getContactNumber方法,以将这种格式替换为任何可能匹配的数字的长字符串。

I then added a catch for any RuntimeException on the RingerService method. 然后,我在RingerService方法上添加了任何RuntimeException的捕获 All is working now. 现在一切正常。

Out of curiosity, does anyone know the format for real incoming phone numbers? 出于好奇,有人知道真实的电话号码格式吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM