gpt4 book ai didi

java - Android 1.6 SDK WebView 问题

转载 作者:行者123 更新时间:2023-12-02 08:29:17 24 4
gpt4 key购买 nike

我已将问题范围缩小到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com