-1

i have a tabbed layout application and the third tab has google maps implemented, no code errors but when i start in the emulator it doesn't even start it just states its been stopped

here is my main activity class

public class MainActivity extends AppCompatActivity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    PagerAdapter pagerAdapter =
            new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
    viewPager.setAdapter(pagerAdapter);

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    // Iterate over all tabs and set the custom view
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        tab.setCustomView(pagerAdapter.getTabView(i));
    }
}

class PagerAdapter extends FragmentPagerAdapter {

    String tabTitles[] = new String[] { "Home", "Shouts", "Maps", };
    Context context;

    public PagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new HomeFragment();
            case 1:
                return new ShoutsFragment();
            case 2:
                return new MapFragment();
        }

        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

    public View getTabView(int position) {
        View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.mapfragment, null);
        TextView tv = (TextView) tab.findViewById(R.id.map);
        tv.setText(tabTitles[position]);
        return tab;
    }

    }

here is my activity main xml

<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    app:tabMode="fixed"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    app:tabTextColor="#d3d3d3"
    app:tabSelectedTextColor="#ffffff"
    app:tabIndicatorColor="#ff00ff"
    android:minHeight="?attr/actionBarSize"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

</RelativeLayout>

here is my map fragment class

public class MapFragment extends Fragment {

MapView mMapView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // inflate and return the layout
    View v = inflater.inflate(R.layout.mapfragment, container,
            false);
    mMapView = (MapView) v.findViewById(R.id.map);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume();// needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync((OnMapReadyCallback) this);
    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude)).title("Hello Maps");

    // Changing marker icon
    marker.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));



    // Perform any camera updates here
    return v;
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}
}

here is my fragment map xml file

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
xmlns:android="http://schemas.android.com/apk/res/android"
/>

here is my home fragment class

public class HomeFragment extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Get the view from fragment home.xml
    View view = inflater.inflate(R.layout.home_fragment, container, false);
    return view;

}
}

here is my home fragment xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/custom_text"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textSize="16dip"
    android:textColor="#ffffff"
    android:singleLine="true"
    />
</LinearLayout>

here is my shouts fragment class

public class ShoutsFragment extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Get the view from fragment home.xml
    View view = inflater.inflate(R.layout.shouts_fragment, container, false);
    return view;

}
}

here is my shouts xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/custom_text"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textSize="16dip"
    android:textColor="#ffffff"
    android:singleLine="true"
    />
 </LinearLayout>

here is my logcat

03-25 11:06:59.012 3016-3016/com.example.hp_user.dn_tab1 I/art: Not late-enabling -Xcheck:jni (already on)
03-25 11:06:59.483 3016-3016/com.example.hp_user.dn_tab1 W/System:     ClassLoader referenced unknown path: /data/app/com.example.hp_user.dn_tab1-    2/lib/x86
03-25 11:06:59.669 3016-3016/com.example.hp_user.dn_tab1 I/GMPM: App                 measurement is starting up, version: 8487
03-25 11:06:59.669 3016-3016/com.example.hp_user.dn_tab1 I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
03-25 11:06:59.759 3016-3016/com.example.hp_user.dn_tab1 E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services.  Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
03-25 11:06:59.813 3016-3016/com.example.hp_user.dn_tab1 E/GMPM: Scheduler not set. Not logging error/warn.
03-25 11:07:00.045 3016-3040/com.example.hp_user.dn_tab1 E/GMPM: Uploading is not possible. App measurement disabled
03-25 11:07:00.379 3016-3016/com.example.hp_user.dn_tab1 D/AndroidRuntime: Shutting down VM
03-25 11:07:00.381 3016-3016/com.example.hp_user.dn_tab1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.hp_user.dn_tab1, PID: 3016
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hp_user.dn_tab1/com.example.hp_user.dn_tab1.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                           at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                        Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197)
                                                                           at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
                                                                           at com.example.hp_user.dn_tab1.MainActivity.onCreate(MainActivity.java:27)
                                                                           at android.app.Activity.performCreate(Activity.java:6237)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:148) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
03-25 11:07:08.761 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 82.187ms
03-25 11:07:08.772 3016-3026/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 9.588ms
03-25 11:07:20.033 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 32.982ms
03-25 11:07:24.581 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 91.462ms
03-25 11:07:32.640 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 159.934ms
03-25 11:07:42.053 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 50.217ms
03-25 11:07:47.886 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 10.813ms
03-25 11:07:50.622 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 331.902ms
  • post your logcat output – Zielony Mar 25 '16 at 11:15
  • ive inserted it into the original question@Zielony – Alexander Rufus Mar 25 '16 at 11:18
  • @AlexanderRufus First try this http://stackoverflow.com/questions/26515058/this-activity-already-has-an-action-bar-supplied-by-the-window-decor add this line false in your styles.xml – Raghavendra Mar 25 '16 at 11:22
  • What is not clear in `Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.`? – Phantômaxx Mar 25 '16 at 11:23

1 Answers1

0

Your logcat output tells you exactly what happened and how to fix it:

This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

I would bet on theme settings:

Using the Toolbar to replace an ActionBar is pretty straight forward and begins with making sure that the activity extends ActionBarActivity. Also make sure that the theme for this activity is a child of either Theme.AppCompat.NoActionBar or Theme.AppCompat.Light.NoActionBar. If you are not using the Theme.AppCompat variants then you can alternatively add the following lines to your theme:

<item name="android:windowNoTitle">true</item> 
<item name="windowActionBar">false</item>

See the full article here: http://mrengineer13.com/how-to-use-toolbar-to-replace-your-actionbar/

Zielony
  • 16,239
  • 6
  • 34
  • 39