简体   繁体   English

Android 1.6 SDK WebView问题

[英]Android 1.6 SDK WebView Issue

I've narrowed down the problem to WebView; 我已将问题缩小为WebView。 my application force closes every time. 我的申请部队每次都关闭。 If I take out the WebView and put in a color change button or something, the case switching works and the application loads. 如果我取出WebView并放入换色按钮或其他按钮,则大小写切换有效并且应用程序加载。 I'm fairly new to the plaform, but I'm (mostly) copying directly from examples here for WebViews. 我对平台还不陌生,但是(主要是)我直接从此处的示例复制WebView。

Application.java Application.java

package com.xxxx.xxxxx;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;

public class Application extends Activity {

  @Override
  public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.main);

  }

  public void myClickHandler(View view) {

   WebView engine = (WebView) findViewById(R.id.webview);
   switch (view.getId()) {

       case R.id.Button1:
        engine.loadUrl("http://digg.com");
        break;

       case R.id.Button2:
        engine.loadUrl("http://reddit.com");
        break;

    }

     }

} 

main.xml main.xml中

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:id="@+id/ScrollParent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"> 

 <LinearLayout android:id="@+id/MainMenuLayout"
  android:layout_width="310px"
   android:layout_height="fill_parent"
   android:orientation="vertical">

   <TextView 
    android:id="@+id/Application"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Application Name..." />

   <Button android:id="@+id/Button1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Digg"
         android:onClick="myClickHandler"/>

        <Button android:id="@+id/Button2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Reddit"
         android:onClick="myClickHandler"/>

 </LinearLayout>

 <LinearLayout  
  android:orientation="vertical"  
  android:layout_width="fill_parent"  
  android:layout_height="fill_parent"  >  

     <WebView
      android:id="@+id/webview"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"/>

 </LinearLayout>
</ScrollView>

AndroidManifest.xml AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.qxmd.ecgguide"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Application"
                  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> 

Your layout root is a ScrollView with two child nodes. 布局的根是带有两个子节点的ScrollView。 ScrollView only supports one child node. ScrollView仅支持一个子节点。 If you look at the ADB Log, the exception thrown when your application crashes tells you this: 如果查看ADB日志,则在应用程序崩溃时引发的异常告诉您:

Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child

One possible solution would be to create single layout that the ScrollView manages, and have your two existing LinearLayouts be children of the new layout. 一种可能的解决方案是创建由ScrollView管理的单一布局,并使您现有的两个LinearLayouts成为新布局的子级。 Here's a direct modification of your original main.xml: 这是对原始main.xml的直接修改:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:id="@+id/ScrollParent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

<!-- new container layout for the original two layouts -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

 <!-- original first child layout, now a child of the wrapper -->
 <LinearLayout android:id="@+id/MainMenuLayout"
  android:layout_width="310px"
   android:layout_height="fill_parent"
   android:orientation="vertical">

   <TextView 
    android:id="@+id/Application"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Application Name..." />

   <Button android:id="@+id/Button1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Digg"
         android:onClick="myClickHandler"/>

        <Button android:id="@+id/Button2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Reddit"
         android:onClick="myClickHandler"/>

 </LinearLayout>

 <!-- the original second child layout, now also a child of the wrapper -->
 <LinearLayout  
  android:orientation="vertical"  
  android:layout_width="fill_parent"  
  android:layout_height="fill_parent"  >  

     <WebView
      android:id="@+id/webview"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"/>

 </LinearLayout>

</LinearLayout>
</ScrollView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM