Skip to content
This repository has been archived by the owner on Apr 19, 2018. It is now read-only.

ABS4RC: I tryed to made SherlockMapActivity into Tab #271

Closed
xWOLKx opened this issue Feb 15, 2012 · 1 comment
Closed

ABS4RC: I tryed to made SherlockMapActivity into Tab #271

xWOLKx opened this issue Feb 15, 2012 · 1 comment

Comments

@xWOLKx
Copy link

xWOLKx commented Feb 15, 2012

I dont know if this is the correct way, so I try to ask you because it make error when I change the tab 2 times.

I'm working on 2.3.7 device with abs4rc (14.2) and map plugin.

I try to make an activity with 2 tab, one show map:

import android.os.Bundle;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockMapActivity;

public class RunActivity extends SherlockMapActivity implements ActionBar.TabListener {

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

            getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            ActionBar.Tab tab = getSupportActionBar().newTab();
            tab.setText("Text");
            tab.setTabListener(this);
            getSupportActionBar().addTab(tab);

            ActionBar.Tab tabx = getSupportActionBar().newTab();
            tabx.setText("Map");
            tabx.setTabListener(this);
            getSupportActionBar().addTab(tabx);

        }

    @Override
    public void onTabSelected(Tab tab) {
        // TODO Auto-generated method stub
        if(tab.getPosition()==1)
            this.setContentView(R.layout.runmap);
        else
            this.setContentView(R.layout.empty);
    }

    @Override
    public void onTabUnselected(Tab tab) {
        // TODO Auto-generated method stub


    }

    @Override
    public void onTabReselected(Tab tab) {
        // TODO Auto-generated method stub

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

In runmap.xml I have only

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

    <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="" />
</LinearLayout>

So this make this logcat when touch for the second time the map tab:

02-15 13:07:47.049: W/dalvikvm(26022): threadid=1: thread exiting with uncaught exception (group=0x40018560)
02-15 13:07:47.079: E/AndroidRuntime(26022): FATAL EXCEPTION: main
02-15 13:07:47.079: E/AndroidRuntime(26022): android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:659)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.actionbarsherlock.app.SherlockMapActivity.setContentView(SherlockMapActivity.java:218)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at it.zonde.android.easytracks.RunActivity.onTabSelected(RunActivity.java:33)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.actionbarsherlock.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:523)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.actionbarsherlock.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:906)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.actionbarsherlock.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:505)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.view.View.performClick(View.java:2506)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.view.View$PerformClick.run(View.java:9112)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.os.Handler.handleCallback(Handler.java:587)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.os.Looper.loop(Looper.java:130)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.app.ActivityThread.main(ActivityThread.java:3835)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at java.lang.reflect.Method.invokeNative(Native Method)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at java.lang.reflect.Method.invoke(Method.java:507)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at dalvik.system.NativeStart.main(Native Method)
02-15 13:07:47.079: E/AndroidRuntime(26022): Caused by: java.lang.reflect.InvocationTargetException
02-15 13:07:47.079: E/AndroidRuntime(26022):    at java.lang.reflect.Constructor.constructNative(Native Method)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
02-15 13:07:47.079: E/AndroidRuntime(26022):    ... 22 more
02-15 13:07:47.079: E/AndroidRuntime(26022): Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:397)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.google.android.maps.MapView.<init>(MapView.java:289)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.google.android.maps.MapView.<init>(MapView.java:264)
02-15 13:07:47.079: E/AndroidRuntime(26022):    at com.google.android.maps.MapView.<init>(MapView.java:247)
02-15 13:07:47.079: E/AndroidRuntime(26022):    ... 25 more
02-15 13:07:48.869: I/Process(26022): Sending signal. PID: 26022 SIG: 9
@JakeWharton
Copy link
Owner

The problem here is that you are calling setContentView more than once which is not recommended (nor doing what you want). Maps are a fickle beast--especially when they have to be displayed and hidden.

What I would recommend doing is including both the map view and the other tab's view in a single layout XML and just changing the visibility of each depending on which tab was selected. This will be a lot easier to manage.

If you wanted to still have separate layouts, you need to remove all of the existing views when changing tabs like this:

ViewGroup content = (ViewGroup)findViewById(android.R.id.content);
content.removeAllViews();
getLayoutInflater().inflate(content, R.layout.runmap);

Inflating from XML is an expensive operation though so you may want to only perform the inflation once and store it in a variable.

The Google Maps library imposes this restriction of having only one map view in the view hierarchy at once so you need to play by its rules.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants