Android 1.6 SDK WebView Проблема - PullRequest
0 голосов
/ 29 сентября 2010

Я сузил проблему до WebView; мое приложение закрывается каждый раз. Если я вынимаю WebView и вставляю кнопку смены цвета или что-то еще, переключение регистра работает и приложение загружается. Я довольно новичок в платформе, но я (в основном) копирую прямо из примеров для WebViews.

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

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

1 Ответ

2 голосов
/ 29 сентября 2010

Корнем макета является ScrollView с двумя дочерними узлами. ScrollView поддерживает только один дочерний узел. Если вы посмотрите на журнал ADB, исключение, которое выдается при сбое приложения, говорит вам следующее:

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

Одним из возможных решений было бы создание единого макета, которым управляет ScrollView, и чтобы ваши два существующих LinearLayouts были дочерними элементами нового макета. Вот прямая модификация вашего оригинального 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>
...