- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 PhoneStateLister
,我从中获取大量数据并将其发送到服务器。
如果我在 fragment 中调用监听器,一切都很好,但是当我在服务中调用它时,我得到一个 NullPointerException
。这是我的代码:
电话状态监听器:
public class CarrierStateListener extends PhoneStateListener {
private Context context;
private TelephonyManager telephonyManager;
private String carrierName;
private int mSignalStrength = 0;
private int mCellTowerID = 0;
private int mCellTowerAreCode = 0;
private int mcc = 0;
private int mnc = 0;
public CarrierStateListener(Context context) {
this.context = context;
}
/**
* Get the Signal strength from the provider, each time there is an update
*/
@Override
public void onSignalStrengthsChanged(final SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation loc = (GsmCellLocation) telephonyManager.getCellLocation();
String networkOperator = telephonyManager.getNetworkOperator();
/** Retrieving the carrier name **/
carrierName = telephonyManager.getNetworkOperatorName();
UserInformation.getSingleton(context).setNetwork(carrierName);
/** Retrieving the RSSI value **/
mSignalStrength = signalStrength.getGsmSignalStrength();
mSignalStrength = (2 * mSignalStrength) - 113; // -> dBm
UserInformation.getSingleton(context).setRssi(String.valueOf(mSignalStrength));
/** Retrieve the cell tower id (CID) and the LAC (location area code) **/
mCellTowerID = loc.getCid();
mCellTowerAreCode = loc.getLac();
UserInformation.getSingleton(context).setCellId(String.valueOf(loc.getCid()));
/**tower mobile network code (mnc) and mobile country code (mcc) **/
if (networkOperator != null) {
mcc = Integer.parseInt(networkOperator.substring(0, 3));
mnc = Integer.parseInt(networkOperator.substring(3));
UserInformation.getSingleton(context).setMnc(String.valueOf(mnc));
UserInformation.getSingleton(context).setMcc(String.valueOf(mcc));
} else {
Toast.makeText(context, "Couldn't retrieve mnc and mcc because network operator is "
+ networkOperator, Toast.LENGTH_SHORT).show();
}
}
和我的服务:
public class SendDataService extends Service {
private static final String TAG = "SendDataService";
private Thread t;
private TelephonyManager telephonyManager;
private CarrierStateListener pslistener;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SendDataService");
wl.acquire();
t = new Thread() {
@Override
public void run() {
while (true) {
try {
Log.e(TAG, "sending data...");
//post request to send data
Thread.sleep(6000);
} catch (InterruptedException ie) {
}
}
}
};
t.start();
Log.e(TAG, "the SendDataService started");
return START_STICKY;
}
然后我在 fragment 的 onResume() 中启动服务:
getActivity().startService(new Intent(getActivity(), SendDataService.class));
当 fragment 开始时...这是我得到的异常:
09-14 16:11:05.034 21697-21697/? E/CallDataService﹕ the SendDataService started
09-14 16:11:05.036 21697-21729/? E/CallDataService﹕ sending data...
09-14 16:11:05.037 21697-21729/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-2445
Process: xxxxxxxx, PID: 21697
java.lang.NullPointerException: Attempt to read from field
'android.os.MessageQueue android.os.Looper.mQueue' on a null object reference
at android.os.Handler.<init>(Handler.java:229)
at android.os.Handler.<init>(Handler.java:137)
at android.telephony.PhoneStateListener$1.<init>(PhoneStateListener.java:270)
at android.telephony.PhoneStateListener.<init>(PhoneStateListener.java:270)
at android.telephony.PhoneStateListener.<init>(PhoneStateListener.java:240)
at xxxxxxxxxxxxx.CarierStateListener.<init>(CarierStateListener.java:27)
at xxxxxxxxx.SendDataService.listenForEvents(SendDataService.java:96)
at xxxxxx.util.SendDataService.access$000(SendDataService.java:25)
at xxxxxxxxxx.util.SendDataService$1.run(SendDataService.java:53)
最佳答案
这是一个消息队列问题,您必须在调用 telephonyManager.listen 之前初始化循环器并在之后循环。
new Thread(new Runnable() {
@Override
public void run() {
quitLooper = false;
Looper.prepare();
tManager.listen(new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
lastSignalStrength = signalStrength;
if(quitLooper)
Looper.myLooper().quit();
}
}, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
Looper.loop();
}
}).start();
关于android - 在服务中调用 PhoneStateListener 时出现 NullPointerExecption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32568328/
嗨,我正在尝试制作一个聊天应用程序。在此过程中,我尝试检索存储在数据库中的消息,但我收到空对象引用异常。 我用来向数据库添加消息的代码是: img_btn.setOnClickListener
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 我正在尝试使用 IntentSe
我有一个 PhoneStateLister,我从中获取大量数据并将其发送到服务器。 如果我在 fragment 中调用监听器,一切都很好,但是当我在服务中调用它时,我得到一个 NullPointerE
我对 Mockito 和 JUnit 非常陌生。我正在努力为“忘记密码”工作流程创建一个测试用例。以下是 Controller 和测试的代码。测试用例因 NullPointerException 而失
我是一名优秀的程序员,十分优秀!