WebView causing errors on second activity

2019-06-27 10:32发布

问题:

I have the following code which works fine when I have it in my main Activity:

    WebView webView = (WebView)findViewById(R.id.iQuestionWebView);
    webView.loadDataWithBaseURL(null, "test", "text/html", "utf-8", null);

Where the WebView layout simply looks like this:

<WebView
    android:id="@+id/iQuestionWebView"
    android:layout_width="match_parent"
    android:layout_height="200dp"/>

But if I put the layout and the code to my second Activity (a simple LinearLayout), I get the following errors:

netstack: LIB_MGR - Error loading lib libdnshostprio.so

netstack: STAT_HUB - Failed to load plugin: libdnshostprio.so

netstack: LIB_MGR - Error loading lib spl_proc_plugin.so

netstack: STAT_HUB - Failed to load plugin: spl_proc_plugin.so

netstack: LIB_MGR - Error loading lib pp_proc_plugin.so

netstack: STAT_HUB - Failed to load plugin: pp_proc_plugin.so

netstack: STAT_HUB - App com.ms.wvtest isn't supported

E/cutils-trace(11142): Error opening trace file: Permission denied (13)

As you can see, the code is very basic - a simple button that starts the second Activity:

MainActivity.java

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button startQuiz = (Button)findViewById(R.id.iMainStartQuizButton);
        startQuiz.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Intent intent = new Intent(MainActivity.this, Quiz.class);
                startActivity(intent);
            }
        });
    }
}

Quiz.java

public class Quiz extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz);

        WebView webView = (WebView)findViewById(R.id.iQuestionWebView);
        webView.loadDataWithBaseURL(null, "test", "text/html", "utf-8", null);
    }
}

I would appreciate it if someone could point me to the cause of the error. Thank you in advance for your help!

Edit: As requested quiz.xml and the manifest:

quiz.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" >

    <WebView
        android:id="@+id/iQuestionWebView"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ms.studycards"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/l_app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ms.studycards.MainActivity"
            android:label="@string/l_setup_title" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.ms.studycards.Quiz"
            android:label="@string/l_quiz_title"
            android:parentActivityName="com.ms.studycards.MainActivity">
        </activity>
    </application>

</manifest>

回答1:

Here's how I solved this issue, although I only test my apps on a device (the simulator is too slow).

  1. In the manifest file, add the internet permission:

    uses-permission android:name="android.permission.INTERNET"
    
  2. In your webview (so in Quiz.java), you need to enable javascript:

    webView.getSettings().setJavaScriptEnabled(true);
    

In my case, these 2 things get rid of the problem and display the content (youtube video in my case) in the webview in the device.