2015-12-23 64 views
2

我試圖讓我的設備的電話號碼,收到以下錯誤:Android的 - 致命異常:主要了java.lang.RuntimeException:無法啓動活動ComponentInfo

致命異常:

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> 
+0

您是否在清單中添加權限? –

回答

2

清單檔案中的添加READ_PHONE_STATE許可

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

READ_PHONE_STATE

Allows read only access to phone state.

Note: If both your minSdkVersion and targetSdkVersion values are set to 3 or lower, the system implicitly grants your app this permission. If you don't need this permission, be sure your targetSdkVersion is 4 or higher.

Protection level: dangerous Constant Value: "android.permission.READ_PHONE_STATE"

1

添加權限READ_PHONE_STATE

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

在你的清單,你需要爲了增加followong讀取手機號碼:

<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. 
相關問題