Frage

Ich habe unten das Problem zu WebView verengt; meine Anwendung Kraft schließt jedes Mal. Wenn ich die WebView herausnehmen und in einer Farbwechsel-Taste oder etwas setzen, bei Arbeiten und die Anwendung Schalten von Lasten. Ich bin ziemlich neu in dem plaform, aber ich bin (meistens) das Kopieren direkt aus den Beispielen hier für 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> 
War es hilfreich?

Lösung

Ihr Layout Wurzel ist ein Scrollview mit zwei untergeordneten Knoten. Scrollview unterstützt nur einen untergeordneten Knoten. Wenn man sich die ADB Log schauen, geworfen die Ausnahme, wenn die Anwendung abstürzt, sagt Ihnen diese:

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

Eine mögliche Lösung wäre, einzelne Layout zu erstellen, dass die Scrollview verwaltet und haben Ihre beiden bestehenden LinearLayouts Kinder des neuen Layouts sein. Hier ist eine direkte Änderung Ihrer ursprünglichen 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>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top