开发者

WebView Inside TabView fail

I have been trying to add a WebView inside one of the tabs in TabView but it isn't working. When I go to the other tabs i just have TextViews and they work fine, but as soon as I click the tab with the web in it, error it closes unexpectedly. here is my search_result.java that has the tabview

package supa.mack.doppler;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;

public class search_result extends TabActivity {
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.search_result);

     TabHost mTabHost;
     mTabHost = getTabHost();

     mTabHost.addTab(mTabHost.newTabSpec("Results").setIndicator("Results").setContent(R.id.textview1));
     mTabHost.addTab(mTabHost.newTabSpec("Location").setIndicator("Location").setContent(R.id.textview2));
     mTabHost.addTab(mTabHost.newTabSpec("Facebook")
       .setIndicator("Facebook")
       .setContent(new Intent(this, BioActivity.class)));

     mTabHost.setCurrentTab(0);


 Button searchButton=(Button) findViewById(R.id.return_button);
    searchButton.setOnClickListener(new View.OnClickListener(){
   public void onClick(View v) {
   Intent intent=new Intent(
     search_result.this,
     doppler_test.class
     );

    startActivity(intent);
   }
});  
}
}

Now here is the search_result.xml where the tabView is defined

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_gravity="center_horizontal">
        <TableLayout 
   android:id="@+id/TableLayout01" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:gravity="center_horizontal">

         <Button
      android:id="@+id/return_button"
      android:layout_height="wrap_content" 
      android:text="Return" 
      android:layout_width="fill_parent"/>  

          <TableRow>
           <TextView 
                android:i开发者_开发知识库d="@+id/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Here is the list of people in your location" />
            <TextView 
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="Here you will see your exact location to bookmark it" />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="Reference Facebook To Check The One You Seek" />
        </TableRow>
      </TableLayout>
    </FrameLayout>
   </LinearLayout>
</TabHost>

Here is BioActivity.java where the webView is defined

package supa.mack.doppler;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class BioActivity extends Activity {
 WebView browser;

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.web_view);

     browser = (WebView) findViewById(R.id.webview);
     browser.getSettings().setJavaScriptEnabled(true);
     browser.loadUrl("http://www.google.com");
 }
}

And finally this is the web_view.xml where the webView is placed

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

O and almost forgot to add my manifest....

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="supa.mack.doppler"
      android:versionCode="1"
      android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".doppler_test"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  <activity android:name=".search_result"> 
  <activity android:name=".search_result" android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"/>
        <activity android:name=".BioActivity" android:label="@string/app_name"
      android:theme="@android:style/Theme.NoTitleBar"/>
  </activity>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.INTERNET" />
    </application>
        <uses-sdk android:minSdkVersion="7" />


</manifest> 

Thank you for looking over my code it is really ruining my day not being able to get this to work...


I have a feeling your web_view.xml is not properly formatted and needs a linearlayout or relativelayout. I have almost an exact same program working and my web_view.xml file looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView android:id="@+id/webview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</RelativeLayout>

You can also try creating a new webview via code, and not bother with the XML layout. You could use that if all you plan on having is a webview and don't need to reuse the file.

EDIT: I just tested your method of XML. It appears to be valid and work just fine. I am however noticing that perhaps the method of calling the BioActivity is incorrect. For example I use something like this:

intent = new Intent().setClass(this, BioActivity.class);  
mTabHost.addTab(mTabHost.newTabSpec("Facebook") 
       .setIndicator("Facebook") 
       .setContent(intent)); 

Give that a try and let us know how it works. I also believe that your button listener should be outside of the onCreate method.


Your manifest may also be incorrect. Your application tag is not closed, you have duplicate activities, and the labels and theme on each activity is not neccessary. In order for the internet to work you will also need to add this permission:

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

The lack of the permission won't ofcourse cause your program to crash, it would just be somthing to make sure you have implimented along with cleaning and fomatting your manifest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜