0

Greetings fellows Stackoverflow Community,

I'm developing Android Application that displaying Google Maps V2 in Fragment and ViewPager. And i facing an issue that regarding inflating the fragment layout that containing Google Maps V2. I have searching and looking in this link Android, google maps fragment and viewpager - Error inflating class fragment for the answers because the method has the same that i've looking for, inflating Google Maps V2 in Fragment but still doesn't work. I've searching in this link Android Google Maps in Fragment too but the result it's the same.

This is my fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" 
    >
    <LinearLayout 
        android:id="@+id/layout_detail_fragment_secondlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#ffffff" 
        >
        <ImageView
            android:id="@+id/detail_item_imv_secondlist"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:contentDescription="@null"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" 
            />
        <TextView
            android:id="@+id/detail_item_tv_title_secondlist"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#cae0e0e0"
            android:gravity="left|center_vertical"
            android:textSize="15sp"
            android:textStyle="bold" 
            />
        <TextView
            android:id="@+id/detail_item_tv_content_secondlist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="fill"
            android:padding="10dp"
            android:textSize="12sp"
            android:lineSpacingMultiplier="1.5" 
            />
        <fragment
            android:id="@+id/detail_item_map_content_secondlist"
            android:layout_width="match_parent"
            android:layout_height="150dp" 
            class="com.google.android.gms.maps.SupportMapFragment"
            />
    </LinearLayout>
</ScrollView>

And this is my fragment_class:

package com.gbu.app.template.fragments;

import java.util.ArrayList;

import com.gbu.app.template.DetailSecondListActivity;
import com.gbu.app.template.R;
import com.gbu.app.template.data.StoreObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DetailSecondListViewPagerFragment extends Fragment {
    private ImageView detail_item_imv_secondlist;
    private LinearLayout layout_detail_fragment_secondlist;
    private TextView detail_item_tv_title_secondlist, detail_item_tv_content_secondlist;
    private DisplayMetrics detail_metrics_secondlist;
    private ArrayList<StoreObject> detail_store;

    @Override
    public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflator, container, savedInstanceState);
        View view_detail_secondlist = inflator.inflate(R.layout.item_viewpager_detail_secondlist, container, false);
        savedInstanceState = getArguments();
        detail_metrics_secondlist = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(detail_metrics_secondlist);
        layout_detail_fragment_secondlist = (LinearLayout) view_detail_secondlist.findViewById(R.id.layout_detail_fragment_secondlist);
        detail_item_imv_secondlist = (ImageView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_imv_secondlist);
        detail_item_tv_title_secondlist = (TextView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_tv_title_secondlist);
        detail_item_tv_content_secondlist = (TextView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_tv_content_secondlist);
        detail_store = new ArrayList<StoreObject>();
        detail_store = DetailSecondListActivity.detail_store;
        startApp(savedInstanceState);
        return view_detail_secondlist;
    }

    private void startApp(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        detail_item_imv_secondlist.getLayoutParams().height = (int) (detail_metrics_secondlist.heightPixels * 0.38);
        detail_item_imv_secondlist.setImageResource(R.drawable.default_images);
        detail_item_tv_title_secondlist.setText(detail_store.get(savedInstanceState.getInt("current_item")).getTitle());
        detail_item_tv_content_secondlist.setText(detail_store.get(savedInstanceState.getInt("current_item")).getAddress());
    }

    public void onDestroyView() {
        super.onDestroyView();
        Log.d("message", "onDestroyView");
    }
}

And this is the pager_adapter:

package com.gbu.app.template.adapters;

import com.gbu.app.template.DetailSecondListActivity;
import com.gbu.app.template.fragments.DetailSecondListViewPagerFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class DetailSecondListPagerAdapter extends FragmentPagerAdapter {
    DetailSecondListViewPagerFragment dslvpf;
    Bundle args;

    public DetailSecondListPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        dslvpf = new DetailSecondListViewPagerFragment();
        args = new Bundle();
        args.putInt("current_item", arg0);
        dslvpf.setArguments(args);
        return dslvpf;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return DetailSecondListActivity.detail_store.size();
    }

}

And this is layout Main_Activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.v4.view.ViewPager
        android:id="@+id/detail_view_pager_second_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        />
</RelativeLayout>

And this is the code of my Main_Activity:

package com.gbu.app.template;

import java.util.ArrayList;

import com.gbu.app.template.adapters.DetailSecondListPagerAdapter;
import com.gbu.app.template.data.StoreObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class DetailSecondListActivity extends FragmentActivity {
    private Intent i;
    public static ArrayList<StoreObject> detail_store;
    private ViewPager detail_viewpager_secondlist;
    private DetailSecondListPagerAdapter detail_secondlist_pager_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_secondlist);
        detail_viewpager_secondlist = (ViewPager) findViewById(R.id.detail_view_pager_second_list);
        detail_store = new ArrayList<StoreObject>();
        detail_store = SecondListActivity.item_store;
    }

    @Override
    protected void onStart() {
        super.onStart();
        detail_secondlist_pager_adapter = new DetailSecondListPagerAdapter(getSupportFragmentManager());
        detail_viewpager_secondlist.setAdapter(detail_secondlist_pager_adapter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        TabGroupActivity parentActivity = (TabGroupActivity) getParent();
        parentActivity.onActivityResult(3, RESULT_OK, i);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 4 && resultCode == RESULT_OK) {
            i = data;
            startApp(data);
        }
    }

    private void startApp(Intent data) {
        // TODO Auto-generated method stub
        detail_viewpager_secondlist.setCurrentItem(data.getIntExtra("position", 0));
    }
}

And this is the AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gbu.app.template"
    android:versionCode="1"
    android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18"/>

    <permission
        android:name="com.gbu.app.template.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.gbu.app.template.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gbu.app.template.MainActivity"
            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="com.gbu.app.template.TemplateTabActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.FirstListActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.DetailFirstListActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.SecondTabActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.SecondListActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.DetailSecondListActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.gbu.app.template.MapviewActivity"
            android:label="@string/app_name" >
        </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCYbyZhouA71LgSsWbZxnFEyQrt_Q_xdHQ"/>   
    </application>

</manifest>

My question is, if i placing Google Maps V2 in Main_Activity, the maps works just fine and showing normal. But if i placing in Fragments, the result will trigger exception: error inflating class fragment. This is the logcat that showing the exception and i have no idea what's causing it because it's not showing the cause like ClassNotFoundException or NullException or something that causing it:

01-11 12:05:44.879: E/AndroidRuntime(7711): android.view.InflateException: Binary XML file line #40: Error inflating class fragment
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at com.gbu.app.template.fragments.DetailSecondListViewPagerFragment.onCreateView(DetailSecondListViewPagerFragment.java:32)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2549)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2549)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.View.measure(View.java:16420)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
01-11 12:05:44.879: E/AndroidRuntime(7711):     at android.widget.FrameLayout.onM

it's just saying from my code in my fragment_class, the error pointing only on inflating the layout at:

View view_detail_secondlist = inflator.inflate(R.layout.item_viewpager_detail_secondlist, container, false);

So, what am i missing in here? It is some code that i haven't addedd? Or perhaps i haven't really understanding the concept of developing fragments, Google Maps V2? Any helps will be apprecciate it, and thank you.

2
  • you havent shown map initialization in this. please show me. Commented Jan 11, 2014 at 6:08
  • thank you for replying.. even i'm not initialize it, normally it will showing the map.. i've tried in my Main_Activity before and working normally even i haven't initialize it.. Commented Jan 11, 2014 at 6:41

3 Answers 3

1

Try using try catch block while inflating.
Declare View view_detail_secondlist; Globally

       @Override
    public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflator, container, savedInstanceState);

    if (container == null) {
        return null;
    }

    if (view_detail_secondlist != null) {
        ViewGroup parent = (ViewGroup) view_detail_secondlist.getParent();
        if (parent != null)
            parent.removeView(view_detail_secondlist);
    }

    try {
        view_detail_secondlist = inflator.inflate(R.layout.item_viewpager_detail_secondlist, container, false);
    } catch (InflateException e) {
        // Log.wtf("S*****", e.getMessage());
    }
    savedInstanceState = getArguments();
    detail_metrics_secondlist = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(detail_metrics_secondlist);
    layout_detail_fragment_secondlist = (LinearLayout) view_detail_secondlist.findViewById(R.id.layout_detail_fragment_secondlist);
    detail_item_imv_secondlist = (ImageView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_imv_secondlist);
    detail_item_tv_title_secondlist = (TextView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_tv_title_secondlist);
    detail_item_tv_content_secondlist = (TextView) layout_detail_fragment_secondlist.findViewById(R.id.detail_item_tv_content_secondlist);
    detail_store = new ArrayList<StoreObject>();
    detail_store = DetailSecondListActivity.detail_store;
    startApp(savedInstanceState);
    return view_detail_secondlist;
}
Sign up to request clarification or add additional context in comments.

2 Comments

How stupid of me.. forgot adding try catch block.. thank you.. i've tried your method and it is working normally now.. really appreciate it..
I've used another method to display the map in fragment, now because this is just bypassing the exception.. in my layout_fragment, i'm placing the map inside FrameLayout, then i add the maps programmatically.. it's work without any problems..
0

Remove super.onCreateView(inflator, container, savedInstanceState); from OnCreate in DetailSecondListViewPagerFragment

1 Comment

Thank you for the answer.. but i have tried before but the result it is the same..
0

You are using the support fragment class, if you are not targeting old Sdk change it to MapFragment

2 Comments

This should be placed in comment section... If you dont have reputation to comment then please dont comment in answer section..
Thank you for replying.. but in the AndroidManifest it is clear that i'm targeting the old Sdk too..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.