gpt4 book ai didi

android - android 中的 GSMCellLocation 以异常结束

转载 作者:行者123 更新时间:2023-11-29 00:41:33 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 GsmCellLocation 类来查找特定实例的小区 ID 和 lac。

我简单地定义了类并通过 TextView 将相应的属性显示到 Activity 。

我使用的代码部分是:

TextView textCID;
TextView textLAC;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textCID= (TextView)findViewById(R.id.tv1);
textLAC=(TextView)findViewById(R.id.tv2);

TelephonyManager telph= (TelephonyManager)getSystemService(Testing2Activity.TELEPHONY_SERVICE);
GsmCellLocation cellLocation= (GsmCellLocation)telph.getCellLocation();

if (cellLocation != null) {

int cid1=cellLocation.getCid();
int lac1=cellLocation.getLac();
Log.v("%%%%GGGGF", "Hello");
String s=String.valueOf(cid1);
String s1=String.valueOf(lac1);

textCID.setText(s);
textLAC.setText(s1);

}

代码部分以 NullPointer 异常结束...

请大家帮忙...

错误日志列为:

01-27 12:33:57.802: E/AndroidRuntime(393): FATAL EXCEPTION: main 01-27 12:33:57.802: E/AndroidRuntime(393): java.lang.RuntimeException: Unable to start activity ComponentInfo{sbc.testing.prjct/sbc.testing.prjct.Testing2Activity}: java.lang.NullPointerException 01-27 12:33:57.802: E/AndroidRuntime(393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 01-27 12:33:57.802: E/AndroidRuntime(393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 01-27 12:33:57.802: E/AndroidRuntime(393): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-27 12:33:57.802: E/AndroidRuntime(393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 01-27 12:33:57.802: E/AndroidRuntime(393): at android.os.Handler.dispatchMessage(Handler.java:99) 01-27 12:33:57.802: E/AndroidRuntime(393): at android.os.Looper.loop(Looper.java:123) 01-27 12:33:57.802: E/AndroidRuntime(393): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-27 12:33:57.802: E/AndroidRuntime(393): at java.lang.reflect.Method.invokeNative(Native Method) 01-27 12:33:57.802: E/AndroidRuntime(393): at java.lang.reflect.Method.invoke(Method.java:521) 01-27 12:33:57.802: E/AndroidRuntime(393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-27 12:33:57.802: E/AndroidRuntime(393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-27 12:33:57.802: E/AndroidRuntime(393): at dalvik.system.NativeStart.main(Native Method) 01-27 12:33:57.802: E/AndroidRuntime(393): Caused by: java.lang.NullPointerException 01-27 12:33:57.802: E/AndroidRuntime(393): at sbc.testing.prjct.Testing2Activity.onCreate(Testing2Activity.java:27) 01-27 12:33:57.802: E/AndroidRuntime(393): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-27 12:33:57.802: E/AndroidRuntime(393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

最佳答案

我遇到了同样的问题。这背后的原因是模拟器没有任何真正的 GSM 或 CDMA 网络,因此 GsmCellLocation 的对象可能被初始化为 null。然而,当我在具有 GSM 网络的实时设备上尝试相同的操作时,它运行正常。

关于android - android 中的 GSMCellLocation 以异常结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9030055/

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