我在screen1.xml中有四个图像按钮。 我已经在screen3.xml中定义了webview。 如果我点击ImageButton1或其他按钮,相应的URL(在web视图中)应该加载到screen3.xml中。Android:如何在新屏幕中打开webview
我试图做到这一点,但它会抛出forceclose错误。但是,如果我在screen1.xml中定义的webview中使用它,它就可以工作。但问题是图像按钮和webview出现在同一页面,它们重叠,看起来很糟糕!
请帮我出去在新的屏幕上打开webview。
顺便说一句,可以将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>
感谢您的回复家伙。就像Jorgesys说的那样,我试图找到我的代码中有什么问题。我必须添加此行“webView =(WebView)findViewById(R.id.web_view);”in to openbrowser1()method。此外,我必须从screen1.xml中删除“webview”属性。这就是为什么,当我尝试在屏幕3中加载webview时,我正在获得FC。 – emmarbe 2011-02-03 13:13:55