user5959404
user5959404

Reputation:

Android telephony manager Crashes

I'm an absolute beginner to android , I tried to create a small application that can display GSM Cellular tower details signal strength etc.. , As I mentioned above , I'm a novice to these applications ,I tried the code below using stack-overflow search results, however application crashes without starting.
i would be grateful if someone could help me ! Thanks.

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

        }

    }

}

Links 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 - AFTER MOVING initialization to On-create

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) 

Upvotes: 0

Views: 3906

Answers (1)

Drez
Drez

Reputation: 498

Move variable initialization (telephonyManager, cellLocation, cellid, celllac) into onCreate, as logcat error says.

Important lines in logcat: Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate() (what happened) and at com.example.gih.test1.MainActivity.<init>(MainActivity.java:18) (where happened) so you can see exact line where the problem lies (18). After correcting that line, you will get similar error for next line (cellLocation). Then, if you try to start your app, you will get NullPointerException for initialization of cellid and and next time for celllac. After moving everything inside your onCreate, your app should work.

Log with combination of debug tells you everything (in your case, not always :) ).

Upvotes: 1

Related Questions