TelephonyManager 객체를 생성하여 Mcc Mnc 정보를 가져오는 테스트 앱을 실행중 ClassCastException 에러 발생.
1. 원인
- 아래 TelephonyManager 서비스를 생성하기 위해 파라미터로 전달해야 할 이름을 Context.TELECOM_SERVICE 추가 되어 발생되는 문제.
> TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELECOM_SERVICE);
2. 수정
- 아래와 같이 TELEPHONY Service로 변경
> TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
3. 결론
- Android 서비스 생성시 클래스명을 잘못 전달되어 사용하고자 하는 클래스와 다른 클래스를 리턴되어 발생.
package com.json.client.teleonyinfo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private String LOG_TAG = "MainActivity: ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELECOM_SERVICE);
if (telephonyManager != null) {
Log.i(LOG_TAG, "onCreate: telephonyManager.getNetworkOperator() = " + telephonyManager.getNetworkOperator());
Log.i(LOG_TAG, "onCreate: telephonyManager.getSimOperator() = " + telephonyManager.getSimOperator());
}
}
}
06-04 09:48:31.883 16595-16595/com.json.client.teleonyinfo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.json.client.teleonyinfo, PID: 16595
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.json.client.teleonyinfo/com.json.client.teleonyinfo.MainActivity}: java.lang.ClassCastException: android.telecom.TelecomManager cannot be cast to android.telephony.TelephonyManager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.ClassCastException: android.telecom.TelecomManager cannot be cast to android.telephony.TelephonyManager
at com.json.client.teleonyinfo.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
반응형