Android Get Sim State

I am trying to print the sims state to screen. At the moment I cannot seem to get it to work. What I want to be able to do is print if a sim card is locked or not.

I have been using this code

    TelephonyManager manager = (TelephonyManager) Context.getSystemService(Context.TELEPHONY_SERVICE);
    int state = manager.getSimState();
    answer1.setText(state);

but the LogCat output is

ERROR/AndroidRuntime(213): Uncaught handler: thread main exiting due to uncaught exception ERROR/AndroidRuntime(213): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.qm/com.qmQ}: java.lang.NullPointerException ERROR/AndroidRuntime(213): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) ERROR/AndroidRuntime(213): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) ERROR/AndroidRuntime(213): at android.app.ActivityThread.access$2100(ActivityThread.java:116) ERROR/AndroidRuntime(213): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) ERROR/AndroidRuntime(213): at android.os.Handler.dispatchMessage(Handler.java:99) ERROR/AndroidRuntime(213): at android.os.Looper.loop(Looper.java:123) ERROR/AndroidRuntime(213): at android.app.ActivityThread.main(ActivityThread.java:4203) ERROR/AndroidRuntime(213): at java.lang.reflect.Method.invokeNative(Native Method) ERROR/AndroidRuntime(213): at java.lang.re flect.Method.invoke(Method.java:521) ERROR/AndroidRuntime(213): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) ERROR/AndroidRuntime(213): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) ERROR/AndroidRuntime(213): at dalvik.system.NativeStart.main(Native Method) ERROR/AndroidRuntime(213): Caused by: java.lang.NullPointerException ERROR/AndroidRuntime(213): at com.qmQonCreate(Q.java:77) ERROR/AndroidRuntime(213): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) ERROR/AndroidRuntime(213): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) ERROR/AndroidRuntime(213): ... 11 more

Line 77 is the

TelephonyManager manager = (TelephonyManager)

Line


You seem to be passing an int to a function that expects a string.

Replace

answer1.setText(state);

with

answer1.setText(Integer.toString(state));


You should check if you've set the permission READ_PHONE_STATE in your Manifest file, and also by calling

    answer.setText(state)

you are calling TextView's

    setText(int resId)

method, which is looking for a text Resource with the supplied id.

链接地址: http://www.djcxy.com/p/31424.html

上一篇: Android条件检查是否其他语句?

下一篇: Android获取Sim State