2016-10-28 51 views
0

我是一个绝对的初学者,我试图创建一个小的应用程序,可以显示GSM蜂窝塔细节信号强度等。,正如我上面提到的,我是这些应用程序的新手,我使用堆栈溢出搜索结果尝试下面的代码,但是应用程序崩溃而没有启动。
如果有人能帮助我,我将不胜感激!谢谢。Android电话管理器崩溃

import android.content.Context; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.telephony.PhoneStateListener; 
import android.telephony.SignalStrength; 
import android.telephony.TelephonyManager; 
import android.telephony.gsm.GsmCellLocation; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 
    // TelephonyManager Tel; 
    MyPhoneStateListener MyListener; 

    private TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
    private GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation(); 
    private Button btnRefresh,btnClose,btnReset; 
    private TextView CellLocation,CellID,GSMCode,signalPow; 
    int cellid= cellLocation.getCid(); 
    int celllac = cellLocation.getLac(); 
    int signalST ; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     MyListener = new MyPhoneStateListener(); 
     telephonyManager  = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 
     btnRefresh = (Button)findViewById(R.id.btnRefresh); 
     btnClose = (Button)findViewById(R.id.btnClose); 
     btnReset = (Button)findViewById(R.id.btnReset); 

     CellLocation = (TextView)findViewById(R.id.txtCellLocation); 
     CellID = (TextView)findViewById(R.id.txtBaseStation); 
     GSMCode = (TextView)findViewById(R.id.txtGSMLocation); 
     signalPow = (TextView)findViewById(R.id.txtSignalStr); 

     btnRefresh.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       signalPow.setText(Integer.toString(signalST)); 
       CellLocation.setText(cellLocation.toString()); 
       CellID.setText(Integer.toString(cellid)); 
       // GSMCode.setText(Integer.toString(celllac)); 
      } 
     }); 

    } 



    @Override 
    protected void onPause() 
    { 
     super.onPause(); 
     telephonyManager.listen(MyListener, PhoneStateListener.LISTEN_NONE); 
    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     telephonyManager.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 
    } 

    public class MyPhoneStateListener extends PhoneStateListener 
    { 
     /* Get the Signal strength from the provider, each tiome there is an update */ 
     @Override 
     public void onSignalStrengthsChanged(SignalStrength signalStrength) 
     { 
      super.onSignalStrengthsChanged(signalStrength); 
//   Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = " 
//     + String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show(); 
      signalST = signalStrength.getGsmSignalStrength(); 
      //I have tried to assign signal strength to an integer and display onClik of a button 

     } 

    } 

} 

链接 https://stackoverflow.com/a/33936721

logcat的

10-28 04:22:34.594 1781-1781/com.example.gih.test1 I/art: Late-enabling -Xcheck:jni 
10-28 04:22:34.615 1781-1786/com.example.gih.test1 I/art: Debugger is no longer active 
10-28 04:22:34.628 1781-1781/com.example.gih.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.gih.test1-1/lib/x86 
10-28 04:22:36.584 1781-1781/com.example.gih.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.gih.test1-1/lib/x86 
10-28 04:22:36.685 1781-1791/com.example.gih.test1 W/art: Suspending all threads took: 8.202ms 
10-28 04:22:36.723 1781-1781/com.example.gih.test1 E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.gih.test1, PID: 1781 
                     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.gih.test1/com.example.gih.test1.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate() 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                     Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate() 
                      at android.app.Activity.getSystemService(Activity.java:5253) 
                      at com.example.gih.test1.MainActivity.<init>(MainActivity.java:18) 
                      at java.lang.Class.newInstance(Native Method) 
                      at android.app.Instrumentation.newActivity(Instrumentation.java:1067) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                      at android.os.Handler.dispatchMessage(Handler.java:102)  
                      at android.os.Looper.loop(Looper.java:148)  
                      at android.app.ActivityThread.main(ActivityThread.java:5417)  
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
10-28 04:22:41.885 1781-1781/com.example.gih.test1 I/Process: Sending signal. PID: 1781 SIG: 9 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.gih.test1"> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

logcat的 - 移动之后初始化在创建

10-28 04:39:28.339 2343-2343/com.example.gih.test1 I/Process: Sending signal. PID: 2343 SIG: 9 
10-28 04:41:46.462 2505-2505/com.example.gih.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.gih.test1-2/lib/x86 
10-28 04:41:46.634 2505-2515/com.example.gih.test1 I/art: Background sticky concurrent mark sweep GC freed 10950(703KB) AllocSpace objects, 5(116KB) LOS objects, 63% free, 828KB/2MB, paused 441us total 117.451ms 
10-28 04:41:47.113 2505-2505/com.example.gih.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.gih.test1-2/lib/x86 
10-28 04:41:47.353 2505-2505/com.example.gih.test1 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
10-28 04:41:47.601 2505-2505/com.example.gih.test1 E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.gih.test1, PID: 2505 
                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gih.test1/com.example.gih.test1.MainActivity}: java.lang.SecurityException: getCellLocation: Neither user 10062 nor current process has android.permission.ACCESS_COARSE_LOCATION. 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                     Caused by: java.lang.SecurityException: getCellLocation: Neither user 10062 nor current process has android.permission.ACCESS_COARSE_LOCATION. 
                      at android.os.Parcel.readException(Parcel.java:1599) 
                      at android.os.Parcel.readException(Parcel.java:1552) 
                      at com.android.internal.telephony.ITelephony$Stub$Proxy.getCellLocation(ITelephony.java:2292) 
                      at android.telephony.TelephonyManager.getCellLocation(TelephonyManager.java:826) 
                      at com.example.gih.test1.MainActivity.onCreate(MainActivity.java:29) 
                      at android.app.Activity.performCreate(Activity.java:6237) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                      at android.os.Handler.dispatchMessage(Handler.java:102)  
                      at android.os.Looper.loop(Looper.java:148)  
                      at android.app.ActivityThread.main(ActivityThread.java:5417)  
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+0

你能分享错误日志吗? – EKN

+0

请发布错误日志 –

+0

如果您的设备上运行android棉花糖6及以上,那么它需要权限使用READ_PHONE_STATE。 –

回答

1

替换此线

private TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
    private GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation(); 
int cellid= cellLocation.getCid(); 
    int celllac = cellLocation.getLac(); 

private TelephonyManager telephonyManager; 
    private GsmCellLocation cellLocation ; 
int cellid; 
    int celllac; 

写入此检查位置允许在API> = 23

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      if (ContextCompat.checkSelfPermission(MainActivity.this, 
        Manifest.permission.ACCESS_FINE_LOCATION) 
        != PackageManager.PERMISSION_GRANTED) { 
       if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
         Manifest.permission.ACCESS_FINE_LOCATION)) { 
       } else { 
        ActivityCompat.requestPermissions(MainActivity.this, 
          new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
          111); 
       } 
      } 
      return; 
     } 

和覆盖此方法来检查许可结果

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case 111: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

然后onCreate方法里面写这些作为

telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
     cellLocation = (GsmCellLocation)telephonyManager.getCellLocation(); 
    cellid= cellLocation.getCid(); 
    celllac = cellLocation.getLac(); 
+0

感谢先生奥马尔阿拉木图,我想你的代码,但仍没有运气,还随信附上我的manifest.xml文件,感谢您的帮助! – Gihan

+0

干净和重建你的项目,然后共享logcat,错误必须是不同的然后或您的问题得到解决 –

+0

干净重建后与您更新的答案,ive发布logcat,它似乎cellLocation =(GsmCellLocation)telephonyManager.getCellLocation();行是问题 – Gihan

1

移动变量初始化(telephonyManagercellLocationcellidcelllac)插入onCreate,作为logcat的误差表示。

重要线路中的logcat: Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()(发生了什么) 和 at com.example.gih.test1.MainActivity.<init>(MainActivity.java:18)(发生的地点) 所以你可以看到确切的行问题出在哪里(18)。纠正该行后,下一行(cellLocation)会出现类似的错误。然后,如果您尝试启动应用程序,则将获得NullPointerException以初始化cellid,并且下一次为celllac。移动onCreate之后的所有内容后,您的应用应该可以正常工作。

日志结合调试告诉你一切(在你的情况,并不总是:))。

+0

很好的解释 –

+0

谢谢! ive移动这些声明,但仍然没有运气,它似乎是cellLocation =(GsmCellLocation)telephonyManager.getCellLocation();是问题 – Gihan

+0

移动初始化,而不是声明(如欧麦尔阿塔告诉你)。 – Drez