gpt4 book ai didi

java - 尝试在空对象引用上调用虚拟方法 'int android.telephony.gsm.GsmCellLocation.getCid()'

转载 作者:太空狗 更新时间:2023-10-29 13:13:05 25 4
gpt4 key购买 nike

我遇到了这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main                                                          
Process: com.kotak.pck.jaimaasaraswati, PID: 8321
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kotak.pck.jaimaasaraswati/com.kotak.pck.jaimaasaraswati.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.telephony.gsm.GsmCellLocation.getCid()' on a null object reference

主要 Activity :

import android.content.Context;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textGsmCellLocation = (TextView) findViewById(R.id.gsmcelllocation);
TextView textCID = (TextView) findViewById(R.id.cid);
TextView textLAC = (TextView) findViewById(R.id.lac);

//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = new GsmCellLocation();
cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();

int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText("GSM Cell Location: " + cellLocation.toString());
textCID.setText("GSM Cell id: " + String.valueOf(cid));
textLAC.setText("GSM Location area code: " + String.valueOf(lac));
}
}

感谢您的帮助。

最佳答案

根据 documentation , getCellLocation() 将返回

Current location of the device or null if not available.

它还需要以下权限。

Manifest.permission.ACCESS_FINE_LOCATION

另外,可能是 LTE 引起的问题。根据文档,

If there is only one radio in the device and that radio has an LTE connection, this method will return null.

此外,您需要使用 getAllCellInfo(),因为另一个在 API 级别 26 中已弃用。

关于java - 尝试在空对象引用上调用虚拟方法 'int android.telephony.gsm.GsmCellLocation.getCid()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37909383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com