FATAL EXCEPTION java.lang.Runtimeexception无法启动活动componentinfo

时间:2016-03-14 15:00:41

标签: android logcat

我试图运行这个简单的JSON解析程序,但它向我显示以下错误。我是Android Studio新手。

我不知道该怎么做以及如何解决它,我读了很多答案,但我找不到解决方案。

Process: com.dropouts.jsondemo, PID: 1126
 java.lang.RuntimeException: Unabl``e to start activity ComponentInfo{com.dropouts.jsondemo/com.dropouts.jsondemo.MainActivity}: java.lang.SecurityException: ConnectivityService: Neither user 10053 nor current process has android.permission.ACCESS_NETWORK_STATE.
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
     at android.app.ActivityThread.access$800(ActivityThread.java:135)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:5001)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
     at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10053 nor current process has android.permission.ACCESS_NETWORK_STATE.
     at android.os.Parcel.readException(Parcel.java:1472)
     at android.os.Parcel.readException(Parcel.java:1426)
     at android.net.IConnectivityManager$Stub$Proxy.getActiveNetworkInfo(IConnectivityManager.java:817)
     at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:562)
     at com.dropouts.jsondemo.MainActivity.isConnected(MainActivity.java:88)
     at com.dropouts.jsondemo.MainActivity.onCreate(MainActivity.java:35)
     at android.app.Activity.performCreate(Activity.java:5231)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5001) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
     at dalvik.system.NativeStart.main(Native Method) 

这是我的mainactivity.java

MAINACTIVITY.JAVA

    package com.dropouts.jsondemo;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.app.Activity;

    public class MainActivity extends Activity {

        EditText etResponse;
        TextView tvIsConnected;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // get reference to the views
            etResponse = (EditText) findViewById(R.id.etResponse);
            tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);

            // check if you are connected or not
            if(isConnected()){
                tvIsConnected.setBackgroundColor(0xFF00CC00);
                tvIsConnected.setText("You are conncted");
            }
            else{
                tvIsConnected.setText("You are NOT conncted");
            }

            // call AsynTask to perform network operation on separate thread
            new HttpAsyncTask().execute("http://hmkcode.appspot.com/rest/controller/get.json");
        }

        public static String GET(String url){
            InputStream inputStream = null;
            String result = "";
            try {

                // create HttpClient
                HttpClient httpclient = new DefaultHttpClient();

                // make GET request to the given URL
                HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

                // receive response as inputStream
                inputStream = httpResponse.getEntity().getContent();

                // convert inputstream to string
                if(inputStream != null)
                    result = convertInputStreamToString(inputStream);
                else
                    result = "Did not work!";

            } catch (Exception e) {
                Log.d("InputStream", e.getLocalizedMessage());
            }

            return result;
        }

        private static String convertInputStreamToString(InputStream inputStream) throws IOException{
            BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
            String line = "";
            String result = "";
            while((line = bufferedReader.readLine()) != null)
                result += line;

            inputStream.close();
            return result;

        }

        public boolean isConnected(){
            ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected())
                return true;
            else
                return false;
        }
        private class HttpAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... urls) {

                return GET(urls[0]);
            }
            // onPostExecute displays the results of the AsyncTask.
            @Override
            protected void onPostExecute(String result) {
                Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
                etResponse.setText(result);
            }
        }
    }

以下是我的manifest.xml文件

的Manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.dropouts.jsondemo">
    <uses-permission android:name="android.permission.INTERNET"/>
        <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>
下面的

是我的activitymain xml文件

activitymain.xml

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvIsConnected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="#FF0000"
            android:textColor="#FFF"
            android:textSize="18dp"
            android:layout_marginBottom="5dp"
            android:text="is connected? " />

        <EditText
            android:id="@+id/etResponse"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:ems="10"
            android:layout_marginTop="10dp"
            android:inputType="textMultiLine" >\
            <requestFocus />
        </EditText>
    </LinearLayout>

2 个答案:

答案 0 :(得分:1)

根据堆栈跟踪,您需要请求android.permission.ACCESS_NETWORK_STATE权限。

此链接应说明您的需求: http://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:0)

您应该两次添加权限。

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