java - Android 1.6 SDK WebView 问题

标签 java android android-webview android-sdk-1.6

我已将问题范围缩小到 WebView;我的申请力量每次都会关闭。如果我取出 WebView 并放入颜色更改按钮或其他东西,则大小写切换将起作用并加载应用程序。我对该平台相当陌生,但我(大部分)直接从此处的 WebView 示例复制。

应用程序.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

<?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

<?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> 

最佳答案

您的布局根是一个带有两个子节点的 ScrollView。 ScrollView只支持一个子节点。如果您查看 ADB 日志,您的应用程序崩溃时抛出的异常会告诉您:

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

一种可能的解决方案是创建 ScrollView 管理的单个布局,并让两个现有的 LinearLayout 成为新布局的子级。这是对原始 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>

关于java - Android 1.6 SDK WebView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3817886/

相关文章:

android - 在浏览器中单击 URL 时如何打开 android 应用程序

JavaScript 接口(interface)可以工作,但需要调用该函数两次

java - 为什么我不能从我的 Activity 中调用此服务?

java - 在 JPA/Play 中提交事务之前,实体如何获取 ID?

java - Android [LocationManager] requestLocationUpdates(), java.lang.RuntimeException

android - 如何将数组列表与移动联系人进行比较

java - Eclipse buildpath 自动获取内部目录的所有 JAR

java - 哪个部署的 jar 包含 android.os.SystemClock?

java - listview onclick toast 在 fragment 中不起作用

java - 无法在 webview 中加载 url - 黑屏