android中的GSMCellLocation以异常结束

时间:2012-01-27 07:09:37

标签: android android-emulator gsm

在我的应用程序中,我使用GsmCellLocation类在特定实例中查找单元格id和lac。

我简单地定义了类,并通过TextView的方式向活动显示了相应的属性。

我使用的代码部分是:

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):致命异乎寻常:主要   01-27 12:33:57.802:E / AndroidRuntime(393):java.lang.RuntimeException:无法启动活动ComponentInfo {sbc.testing.prjct / sbc.testing.prjct.Testing2Activity}:java.lang.NullPointerException   01-27 12:33:57.802:E / AndroidRuntime(393):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)   01-27 12:33:57.802:E / AndroidRuntime(393):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)   01-27 12:33:57.802:E / AndroidRuntime(393):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)   01-27 12:33:57.802:E / AndroidRuntime(393):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)   01-27 12:33:57.802:E / AndroidRuntime(393):在android.os.Handler.dispatchMessage(Handler.java:99)   01-27 12:33:57.802:E / AndroidRuntime(393):在android.os.Looper.loop(Looper.java:123)   01-27 12:33:57.802:E / AndroidRuntime(393):在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):引起: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):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   01-27 12:33:57.802:E / AndroidRuntime(393):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。这背后的原因是仿真器没有任何真正的GSM或CDMA网络,因此GsmCellLocation的对象可能用null初始化。但是,当我在带有GSM网络的带电设备上尝试相同的操作时,它可以正常工作。

答案 1 :(得分:0)

确保您请求权限

     if (
            ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED
            ) {
        //ask for permission
        askPermission();
    } 
     private void askPermission() {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQUEST);
}

然后,您当然需要覆盖onRequest权限方法

     @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == LOCATION_REQUEST) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // do what need to be done

            Toast.makeText(this, "We have Permission onRequest", Toast.LENGTH_SHORT).show();
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED) {
                return;
            }


        }
    }
}