android Webview App在启动时崩溃

时间:2017-07-27 16:18:01

标签: android

构建一个通过webview访问页面的应用程序。 但它在开始时崩溃,给出了以下运行时错误..

E / AndroidRuntime:致命异常:主要                   过程:com.aaryansoftwares.moopl.moopl,PID:2371                   java.lang.RuntimeException:无法启动活动ComponentInfo {com.aaryansoftwares.moopl.moopl / com.aaryansoftwares.moopl.moopl.MainActivity}:android.os.NetworkOnMainThreadException                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                       在android.app.ActivityThread.-wrap12(ActivityThread.java)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:154)                       在android.app.ActivityThread.main(ActivityThread.java:6077)                       at java.lang.reflect.Method.invoke(Native Method)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                    引起:android.os.NetworkOnMainThreadException                       在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)                       at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)                       在java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)                       在java.net.InetAddress.getByName(InetAddress.java:708)                       在com.aaryansoftwares.moopl.moopl.MainActivity.isNetworkConnected(MainActivity.java:41)                       在com.aaryansoftwares.moopl.moopl.MainActivity.onCreate(MainActivity.java:20)                       在android.app.Activity.performCreate(Activity.java:6662)                       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                       在android.app.ActivityThread.-wrap12(ActivityThread.java)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:154)                       在android.app.ActivityThread.main(ActivityThread.java:6077)                       at java.lang.reflect.Method.invoke(Native Method)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 申请已终止。

主要活动:

public class MainActivity extends AppCompatActivity {

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

    if(isNetworkConnected()==true)
    {
        WebView start = (WebView) findViewById(R.id.activity_main);
        start.loadUrl("url here");
    }

    else
    {
        Intent i= new Intent(this,activityFail.class);
        startActivity(i);
    }

}

private boolean isNetworkConnected() {
    /*ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo() != null;*/


    try {
        final InetAddress address = InetAddress.getByName("www.google.com");
        return !address.equals("");
    } catch (UnknownHostException e) {
        // Log error
    }
    return false;
}

}

activityfail:

public class activityFail extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connection_fail);

    ImageView img =(ImageView) findViewById(R.id.imageView);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent j=new Intent(getApplicationContext(), MainActivity.class);

        }
    });

}

}

清单:

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

1 个答案:

答案 0 :(得分:0)

您无法通过

在主线程上进行网络呼叫
  

的onCreate()

您必须将网络调用包装在一个线程中。我建议使用AsyncTask。

public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        // do your network things here
        return null;
    }
}

您也可以覆盖

  

onPostExecute()

一旦doInBackground回调主线程,就可以使用UI完成任何工作。