I was changing a Activity to a Fragment to use inside a Scrollable Tab Activity. But I getting this exception I load this fragment:
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.zhensydow.demo.MainMenuFragment.onCreateView(MainMenuFragment.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
....
And the used (simplified) xml loaded is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<fragment <<---- LINE #17
android:id="#+id/mlist"
android:name="com.zhensydow.demo.MListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#android:layout/mcontent" />
<FrameLayout ... />
</LinearLayout>
The error is caused in this code:
public class MainMenuFragment extends Fragment implements
MenuListFragment.Callbacks {
// ...
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
LinearLayout ll = (LinearLayout) inflater.inflate(
R.layout.activity_main_menu, container, false);
return ll;
}
// ...
}
The error is on a fragment, I think it's caused because the old activity loaded two fragments inside him.
How can I solve it?
UPDATE: added full fragmen xml data
I suspect your Fragment contains some code like this one, requiring your Activity to implement a callback interface, and your Activity does not implement that interface.
A fragment tag is required to have both a name and an id.
Related
I'm trying to use TwitterLoginButton in DialogFragment.
I got error :
android.view.InflateException: Binary XML file line #5: Binary XML file line #5: Error inflating class com.twitter.sdk.android.core.identity.TwitterLoginButton
Using TwitterLoginButton in Activity class, I got no error.
Please help me.
dialog_auth.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#color/white" android:id="#+id/dialog_auth">
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="#+id/twitter_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</LinearLayout>
AuthDialog.java
public class AuthDialog extends DialogFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_auth, null);
return view
}
https://dev.twitter.com/web/sign-in
You can not create Login Button In Dialog .
TwitterLoginButton NOT SUPPORTED IN DIALOG VIEW
I have a Fragment, which contains two other Fragments, namely a ListFragment and a regular Fragment. When I inflate the parent Fragment I receive an InflateException runtime error.
This is due to like #11 in my XML layout code which refers to this:
<fragment
android:id="#+id/fragment1"
android:name="com.nanospark.TMS.fragment_profile_list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/new_profile_button" />
Keep in mind this XML above refers a nested Fragment.
Is nesting Fragments a supported feature in Android? Are there any alternatives or fixes to this issue?
Logcat:
04-17 15:00:58.198: E/AndroidRuntime(1400): FATAL EXCEPTION: main
04-17 15:00:58.198: E/AndroidRuntime(1400): android.view.InflateException: Binary XML file line #11: Error inflating class fragment
EDIT 1:
From what I've discovered it is supported
http://developer.android.com/about/versions/android-4.2.html#NestedFragments
However I cannot find any good examples of nested Fragments and filling parent fragments with them.
You can add it in your fragment code:
public class YourFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, null);
if (getChildFragmentManager().findFragmentByTag("main") == null)
{
if (getActivity() == null)
return null;
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
NewsMainFragment nf = (YourNested) YourNestedFragementClass.instantiate(getActivity(), YourNestedFragementClass.class.getName(),savedInstanceState);
ft.add(R.id.fragment_container, nf,"main");
ft.commit();
}
return v;
}
}
I have dynamically added my google map fragment in framelayout. Now I want to put markers and do other stuff with google map. but i am not able to access that through get fragment manager. The code shows no error but when it executes it returns null pointer exception.
I thinks its logical error. Something to do with type casting. plz help
Here is my "nearby_places.xml" layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.localiteproximus.CustomAutoCompleteTextView
android:id="#+id/atv_places"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="#string/str_atv_places"
android:singleLine="true" />
<FrameLayout
android:id="#+id/mapView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="#id/atv_places">
</FrameLayout>
</RelativeLayout>
and this is my class
public class NearByPlaceFragment extends Fragment {
public NearByPlaceFragment(){}
View rootView;
GoogleMap googleMap;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MapFragment fragment = new MapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapView, fragment).commit();
rootView = inflater.inflate(R.layout.nearby_places, container, false);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
return rootView
}
and LogCat is showing this
58.608: E/AndroidRuntime(12654): FATAL EXCEPTION: main
03-22 03:00:58.608: E/AndroidRuntime(12654): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.localiteproximus/com.localiteproximus.NearbyPlaces}: android.view.InflateException: Binary XML file line #21: Class is not a View com.google.android.gms.maps.MapFragment
03-22 03:00:58.608: E/AndroidRuntime(12654): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
03-22 03:00:58.608: E/AndroidRuntime(12654): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.view.View
03-22 03:00:58.608: E/AndroidRuntime(12654): at java.lang.Class.asSubclass(Class.java:1182)
03-22 03:00:58.608: E/AndroidRuntime(12654): at android.view.LayoutInflater.createView(LayoutInflater.java:565)
03-22 03:00:58.608: E/AndroidRuntime(12654): ... 23 more
I got answer on my own.
So it was really silly mistake.. after 6 hours i found that the error was on this line
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap();
which was getting its parent view instead of child so replaced it with this line
googleMap = ((MapFragment) getChildFragmentManager().findFragmentById(R.id.mapView)).getMap();
and everything is fine :).
I'm ripping my hair out trying to display a thumbnail sized map in either an AlertDialog or preferably in a SherlockDialogFragment.
I have this in my xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.MapFragment" />
And with just this in my SherlockDialogFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setCanceledOnTouchOutside(true);
getDialog().setTitle(offer.retailer);
View v = inflater.inflate(R.layout.businesstmpalert, container, false);
return v
}
I get an Error inflating class fragment error
What am I doing wrong?
This doesn't work because you are nesting fragment (MapFragment inside a DialogFragment) AND specifying the MapFragment in the <layout>.
From http://developer.android.com/about/versions/android-4.2.html#NestedFragments
Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.
You could either nest the fragment programmatically:
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.map_fragment_host, MapFragment.newInstance()).commit();
in DialogFragment's onActivityCreated(), or (preferably, IMHO) switching to MapView, which you can safely use as a <layout>. In this case, remember to propagate fragment lifecycle events to the MapView, as specified in https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
1-Add these lines a the end of the within manifest file.
if this not work then...
2-extends android.support.v4.app.FragmentActivity
public class MainActivity extends android.support.v4.app.FragmentActivity
I am working on Fragments in Android and would like to programmatically inflate my view - without the LayoutInflater. In my class, which extends Fragment, I would like to do something basic, like this:
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return new View(inflater.getContext());
}
However in my FragmentActivity with this onCreate method:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
and this main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<fragment class="edu.self.myFragment"
android:id="#+id/titles"
android:tag="foo"
android:layout_width="50px"
android:layout_height="50px" />
</LinearLayout>
I am getting an error with the layout:
11-11 23:25:18.142: E/AndroidRuntime(1162): Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f050000, tag foo, or parent id 0x0 with another fragment for edu.self.myFragment
What do I need to do to fix this error?
You can't inflate an xml containing a fragment inside another fragment. You have to add the sub-fragment manually.