Android:如何在新屏幕中打开webview

时间:2011-01-31 23:26:31

标签: android webview android-webview

我在screen1.xml中有四个图像按钮。 我在screen3.xml中定义了webview。 如果我单击ImageButton1或其他按钮,则应在screen3.xml中加载相应的URL(在webview中)。

我试图这样做,但它会抛出forceclose错误。但是,如果我使用screen1.xml中定义的webview尝试相同的操作,则可以正常工作。但问题是图像按钮和webview出现在同一页面中,它们重叠,看起来很糟糕!

请帮我在新屏幕中打开网页视图。

BTW,可以将addView用于此目的吗?

public class click extends Activity 
{
 private WebView webView;
    public void onCreate(Bundle savedInstanceState) 
    {
     super.onCreate(savedInstanceState);
        setContentView(R.layout.screen1);

        webView = (WebView) findViewById(R.id.web_view);
        ImageButton i1 = (ImageButton) findViewById(R.id.imagebutton1);
        ImageButton i2 = (ImageButton) findViewById(R.id.imagebutton2);
        ImageButton i3 = (ImageButton) findViewById(R.id.imagebutton3);
        ImageButton i4 = (ImageButton) findViewById(R.id.imagebutton4);
 }
private void openBrowser1() 
     {

      webView.getSettings().setJavaScriptEnabled(true);

      webView.loadUrl("myurl");
     }

public void myClickHandler1(View view) 
        {

         openBrowser1();
        }

Screen1.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  <WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />



 <ImageButton android:id="@+id/imagebutton1" android:clickable="true" android:onClick="myClickHandler1" 
 android:layout_width="130dip" android:background="@null" android:layout_height="130dip" android:layout_x="21dip" 
 android:layout_y="61dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton2" android:clickable="true" android:onClick="myClickHandler2" 
 android:layout_width="130dip" android:background="@null"  android:layout_height="130dip" android:layout_x="171dip" 
 android:layout_y="61dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton3" android:clickable="true" android:onClick="myClickHandler3" 
 android:layout_width="130dip" android:background="@null" android:layout_height="130dip" android:layout_x="21dip" 
 android:layout_y="241dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton4" android:clickable="true" android:onClick="myClickHandler4" 
 android:layout_width="130dip" android:background="@null"  android:layout_height="130dip" android:layout_x="171dip" 
 android:layout_y="241dip" ></ImageButton>
</AbsoluteLayout> 

screen3.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >                 

<WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />

</AbsoluteLayout> 

2 个答案:

答案 0 :(得分:4)

我认为您需要对代码进行一些修复...

public class click extends Activity {

    private WebView webView;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen1);

        webView = (WebView) findViewById(R.id.web_view);        
        ImageButton i1 = (ImageButton) findViewById(R.id.imagebutton1);
        ImageButton i2 = (ImageButton) findViewById(R.id.imagebutton2);
        ImageButton i3 = (ImageButton) findViewById(R.id.imagebutton3);
        ImageButton i4 = (ImageButton) findViewById(R.id.imagebutton4);

        WebSettings webSettings = webView.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webView.loadUrl("http://www.reforma.com");

        //Add a listener to ImageButton1
        i1.setOnClickListener(new OnClickListener() {                       
            @Override
            public void onClick(View v) {
                openBrowser1();
            }           
        });                             

    }

    private void openBrowser1() 
    {       
       //this intent is used to open other activity wich contains another webView
        Intent intent = new Intent(click.this,SecondActivity.class);
        startActivity(intent); 
    }

}

screen1.xml

    

  <WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />

 <ImageButton android:id="@+id/imagebutton1" android:clickable="true"  
 android:layout_width="130dip" android:layout_height="130dip" android:layout_x="21dip" 
 android:layout_y="61dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton2" android:clickable="true"  
 android:layout_width="130dip" android:layout_height="130dip" android:layout_x="171dip" 
 android:layout_y="61dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton3" android:clickable="true"  
 android:layout_width="130dip" android:layout_height="130dip" android:layout_x="21dip" 
 android:layout_y="241dip" ></ImageButton>

 <ImageButton android:id="@+id/imagebutton4" android:clickable="true"  
 android:layout_width="130dip"  android:layout_height="130dip" android:layout_x="171dip" 
 android:layout_y="241dip" ></ImageButton>
</AbsoluteLayout> 

添加一个新活动,其中包含您应该加载网页的网页浏览!。

public class SecondActivity extends Activity {

    private WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen3);

        webView = (WebView) findViewById(R.id.web_view);
        WebSettings webSettings = webView.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webView.loadUrl("http://www.reforma.com");         

    }

 }

screen3.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >                 

<WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />

</AbsoluteLayout> 

将第二个活动

添加到AndroidManifest.xml中
   <activity android:name=".SecondActivity"
          android:label="SecondActivity"/> 

并且您必须具有Internet权限才能加载网页...

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

任何疑惑???

Jorgesys

答案 1 :(得分:1)

看起来你并没有在任何地方膨胀和加载screen3.xml布局,因此尝试引用它包含的WebView可能会产生异常。听起来你真正想要做的是为webview布局定义另一个Activity,并在用户按下其中一个按钮时交换它。有关如何向应用程序添加其他活动并在它们之间进行交换的说明,请参阅记事本示例的第2部分:

http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html