Android - FATAL EXCEPTION:main java.lang.RuntimeException:无法启动活动ComponentInfo

时间:2015-12-23 11:01:48

标签: android

我尝试获取设备的电话号码,收到以下错误:

致命的例外:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.myapplication/com.example.dell.myapplication.MainActivity}: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.

我是android开发的新手。我查看了LogCat生成的错误,但不知道问题以及根据收到的错误在哪里找到它。

感谢您的帮助,并对此感到抱歉!

这是我的代码:

MainActivity.java

package com.example.dell.myapplication;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends ActionBarActivity {

    public MainActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        String imei = tm.getDeviceId();
        String phone = tm.getLine1Number();
        Toast.makeText(MainActivity.this, "my IMEI: "+imei, Toast.LENGTH_LONG).show();
        Toast.makeText(MainActivity.this,"my phone: "+phone,Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            return true;
//        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="121dp" />

</RelativeLayout>

logcat的

12-23 12:50:35.013    8789-8789/? D/dalvikvm﹕ Late-enabling CheckJNI
12-23 12:50:35.303    8789-8789/com.example.dell.myapplication I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
12-23 12:50:35.303    8789-8789/com.example.dell.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 13332: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
12-23 12:50:35.303    8789-8789/com.example.dell.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
12-23 12:50:35.308    8789-8789/com.example.dell.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-23 12:50:35.308    8789-8789/com.example.dell.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-23 12:50:35.308    8789-8789/com.example.dell.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
12-23 12:50:35.308    8789-8789/com.example.dell.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-23 12:50:35.308    8789-8789/com.example.dell.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
12-23 12:50:35.308    8789-8789/com.example.dell.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
12-23 12:50:35.443    8789-8789/com.example.dell.myapplication D/AndroidRuntime﹕ Shutting down VM
12-23 12:50:35.443    8789-8789/com.example.dell.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416f02a0)
12-23 12:50:35.468    8789-8789/com.example.dell.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.myapplication/com.example.dell.myapplication.MainActivity}: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
            at android.os.Parcel.readException(Parcel.java:1425)
            at android.os.Parcel.readException(Parcel.java:1379)
            at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getDeviceId(IPhoneSubInfo.java:222)
            at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:249)
            at com.example.dell.myapplication.MainActivity.onCreate(MainActivity.java:37)
            at android.app.Activity.performCreate(Activity.java:5206)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
            at dalvik.system.NativeStart.main(Native Method)
12-23 12:50:35.513    8789-8793/com.example.dell.myapplication D/dalvikvm﹕ GC_CONCURRENT freed 234K, 14% free 9604K/11143K, paused 3ms+3ms, total 49ms
12-23 12:50:48.168    8789-8789/com.example.dell.myapplication I/Process﹕ Sending signal. PID: 8789 SIG: 9

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dell.myapplication" >

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

3 个答案:

答案 0 :(得分:2)

添加READ_PHONE_STATE权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
清单文件中的

<强> READ_PHONE_STATE

  

允许只读访问手机状态。

     

注意:如果同时设置了minSdkVersion和targetSdkVersion值   为了3或更低,系统会隐式授予您的应用此权限。   如果您不需要此权限,请确保您的targetSdkVersion为4   或更高。

     

保护等级:危险   常数值:   “android.permission.READ_PHONE_STATE”

答案 1 :(得分:2)

在您的清单中,您需要添加以下权限才能阅读电话号码:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

在你的logCat中,系统告诉你这一行出错的原因:

Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.

答案 2 :(得分:1)

添加权限READ_PHONE_STATE

<uses-permission android:name="android.permission.READ_PHONE_STATE" />