Android:SMSManager - 尝试获取null数组的长度

时间:2015-08-03 08:07:04

标签: java android smsmanager

当我尝试使用方法from scrapy.spider import BaseSpider from scrapy.selector import Selector from selenium import webdriver from selenium.webdriver.common.keys import Keys from scrapy.selector import HtmlXPathSelector import time import requests import csv class insiderSpider(BaseSpider): name = 'insider' allowed_domains = ["insider.in"] start_urls = ["http://www.insider.in/mumbai/"] def parse(self,response): driver = webdriver.Firefox() print response.url driver.get(response.url) s = Selector(response) #hxs = HtmlXPathSelector(response) source_link = [] temp = [] title ="" Price = "" Venue_name = "" Venue_address = "" description = "" event_details = [] alllinks = s.xpath('//div[@class="bottom-details-right"]//a/@href').extract() print alllinks length_of_alllinks = len(alllinks) for single_event in range(1,length_of_alllinks): if "https://insider.in/event" in alllinks[single_event]: source_link.append(alllinks[single_event]) driver.get(alllinks[single_event]) s = Selector(response) #hxs = HtmlXPathSelector(response) time.sleep(3) title = s.xpath('//div[@class = "cell-title in-headerTitle"]/h1//text()').extract() print title temp = s.xpath('//div[@class = "cell-caption centered in-header"]//h3//text()').extract() print temp time.sleep(2) a = len(s.xpath('//div[@class = "bold-caption price"]//text()').extract()) if a > 0: Price = s.xpath('//div[@class = "bold-caption price"]//text()').extract() time.sleep(2) else: Price = "RSVP" time.sleep(2) print Price Venue_name = s.xpath('//div[@class = "address"]//div[@class = "section-title"]//text()').extract() print Venue_name Venue_address = s.xpath('//div[@class ="address"]//div//text()[preceding-sibling::br]').extract() print Venue_address description = s.xpath('//div[@class="cell-caption accordion-padding"]//text()').extract() print description time.sleep(5) event_details.append([title,temp,Price,Venue_name,Venue_address,description]) else: print "Other part" 发送消息时,我有一个来自SMSManager的错误。它返回[u'https://insider.in/weekender-music-festival-2015', u'https://insider.in/event/east-india-comedy-presents-back-benchers#', u'https://insider.in/event/art-of-story-telling', u'https://insider.in/feelings-in-india-with-kanan-gill', u'https://insider.in/event/the-tall-tales-workshop-capture-your-story', u'https://insider.in/halloween-by-the-pier-2015', u'https://insider.in/event/whats-your-story', u'https://insider.in/event/beyond-contemporary-art'] 2015-08-03 12:53:29 [selenium.webdriver.remote.remote_connection] DEBUG: POST http://127.0.0.1:60924/hub/session/f675b909-5515-41d4-a89e-d197c296023d/url {"url": "https://insider.in/event/east-india-comedy-presents-back-benchers#", "sessionId": "f675b909-5515-41d4-a89e-d197c296023d"} 2015-08-03 12:53:29 [selenium.webdriver.remote.remote_connection] DEBUG: Finished Request [] [] RSVP [] [] [] [[[], [], 'RSVP', [], [], []]]

有我的代码:

sendTextMesasge

LogCat收到此消息:

java.lang.NullPointerException: Attempt to get length of null array

3 个答案:

答案 0 :(得分:4)

发现我的destNumber为空且不正确(不是电话号码)。这就是为什么我有一个java.lang.NullPointerException。

答案 1 :(得分:1)

我已经在我的应用程序中发现了这几次(但从未在我的测试中),并试图找到解决方案数月。到目前为止,我找不到可以重现它的测试用例。

使用Sms.sendMultipartTextMessage方法:

SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(msg);
sms.sendMultipartTextMessage(number, null, parts, null, null);

没有任何帮助 - 因为如果消息只有一个部分,它在内部最终会调用sendTextMessage - 但无论如何,如果你切换到它以防万一你有更长的消息可能是个好主意。

我最终将代码包装在try-catch中,并在服务器发生时向服务器报告。从我所看到的,看似“正常”的消息似乎触发了它。

有一个类似的情况here,但在这种情况下,邮件有一个PendingIntent for sent。你可能想尝试一下。

答案 2 :(得分:1)

首先,您需要AndroidManifest.xml中的权限才能读取电话状态

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

然后尝试在Try Catch Block中获取移动设备数量。

getLine1Number()可能会返回null值。 所以你需要处理异常

try 
{
    myPhoneNumber = mTelephonyMgr.getLine1Number();
}
catch(NullPointerException ex)
{
}