2011-01-31 61 views
2

我在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> 

回答

1

它看起来并不像你充气和任何地方加载screen3.xml布局,所以试图引用它包含的WebView可能会生成一个异常。听起来你真正想做的是为webview布局定义另一个Activity,并在用户按下其中一个按钮时交换它。查看记事本样品的第二部分用于描述如何添加额外的活动,它们之间的应用和交换:

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

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> 

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

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> 

有任何疑问Sαδ+

Jorgesys

+0

感谢您的回复家伙。就像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

相关问题