Issue
I have problem with initializing TabHost. I need to have in View several Tabs which show different activities: one must show google map, second - log in form. In created code there is error "The method getTabHost() is undefined for the type MapViewDemo"
package com.example.android.apis.view;
import java.util.List;
import android.app.*;
import com.example.android.google.apis.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.widget.TabHost;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.*;
import com.example.android.apis.view.GMapManager;
public class MapViewDemo extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
MapView mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setBuiltInZoomControls(true);
MapController mMapController = mMapView.getController();
double x, y;
x = 59.434034;
y = 24.757687;
double[] xy = {x,y};
GeoPoint p = coordinatesToGeoPoint(xy);
mMapController.animateTo(p);
mMapController.setZoom(18);
mMapView.invalidate();
mMapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mMapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.marker_green);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
GeoPoint point = p;
OverlayItem overlayitem = new OverlayItem(point, "123!", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Do the same for the other tabs
intent = new Intent().setClass(this, GMapManager.class);
spec = tabHost
.newTabSpec("manager")
.setIndicator("Manager",
res.getDrawable(R.drawable.ic_tabs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MapViewDemo.class);
spec = tabHost
.newTabSpec("map")
.setIndicator("Map", res.getDrawable(R.layout.mapview))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
@Override
protected boolean isRouteDisplayed() { return false; }
/**
* Converts a pair of coordinates to a GeoPoint
*
* @param coords double containing latitude and longitude
* @return GeoPoint for the same coords
*/
public static GeoPoint coordinatesToGeoPoint(double[] coords) {
if (coords.length > 2) {
return null;
}
if (coords[0] == Double.NaN || coords[1] == Double.NaN) {
return null;
}
final int latitude = (int) (coords[0] * 1E6);
final int longitude = (int) (coords[1] * 1E6);
return new GeoPoint(latitude, longitude);
}
}
Eclipse offered me to create method getTabHost(). I accepted. Eclipse created it, but I don't see it in my current class any changes. Now it can compile but produces RuntimeException. Here is stack trace:
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1955
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1980
ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 122
ActivityThread$H.handleMessage(Message) line: 1146
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4340
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 784
ZygoteInit.main(String[]) line: 551
NativeStart.main(String[]) line: not available [native method]
And here is my layout.xml
<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
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="03xx7gYjYkcIs5nDM_44v02HgLCmO3Bcega19yA"
android:clickable="true"
android:enabled="true" />
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
<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:padding="5dp" />
</LinearLayout>
</TabHost>
Solution
getTabHost()
is method only found in TabActivity
and if you want to add get Tabs other than TabActivity
, put TabHost in layout.xml and then try this way:
TabHost tabHost = (TabHost)this.findViewById(R.id.tab_host_id);
tabHost.setup(); // dont forget to call this line.
For more detail go here
Answered By - Adil Soomro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.