I have three Fragments and they are arranged as shown below:
"Frag A and Frag C is not connected, but Frag B and C are. The each fragment contains 1 listview so every time I triggered an event it will go to the next fragment."
Fragment A ---> (Inside Frag A)Fragment B ---> (Inside Frag B)Fragment C
Fragment A(main.xml)
Fragment B(R.id.contentFragment)
Fragment C(R.id.contentFragment)
I can display Fragment A and Fragment B and is okay, but if I call Fragment C from BI got "No view found in contentFragment=0x7f040039".
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/white"
android:id="#+id/frameLayout"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
>
</ListView>
<FrameLayout
android:id="#+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
</FrameLayout>
</FrameLayout>
FragmentA.java
Inside Fragment A to open Fragment B
Fragment fragment1 = new FragmentB();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.contentFragment, fragment1, fragMainGroups );
transaction.addToBackStack(fragMainGroups);
transaction.commit();
FragmentB.java
Inside Fragment B to open Fragment C
String fragGroups = "groups";
Fragment fragment1 = new FragmentC();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.contentFragment, fragment1, fragGroups );
transaction.addToBackStack(fragGroups);
transaction.commit();
FragmentC.java
Inside Fragment C
public class FragmentItems extends ListFragment{
public void onAttach(Activity activity) {
super.onAttach(activity);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void onCreate(Bundle e)
{
super.onCreate(e);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.load_items_activity, container, false);
return rootView;
}
}
Logcat
07-25 18:10:20.388: E/FragmentManager(24703): No view found for id 0x7f040039 (com.jinisys.restoplusordering:id/contentFragment) for fragment FragmentItems{4156f480 #0 id=0x7f040039 groups}
07-25 18:10:20.388: E/FragmentManager(24703): Activity state:
07-25 18:10:20.388: E/FragmentManager(24703): No view found for id 0x7f040039 (com.jinisys.restoplusordering:id/contentFragment) for fragment FragmentItems{4156f480 #0 id=0x7f040039 groups}
07-25 18:10:20.388: E/FragmentManager(24703): Activity state:
07-25 18:10:20.718: E/AndroidRuntime(24703): FATAL EXCEPTION: main
07-25 18:10:20.718: E/AndroidRuntime(24703): java.lang.IllegalArgumentException: No view found for id 0x7f040039 (com.jinisys.restoplusordering:id/contentFragment) for fragment FragmentItems{4156f480 #0 id=0x7f040039 groups}
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.os.Handler.handleCallback(Handler.java:615)
As your error states you are looking for a view with id contentFragmentTtems inside Fragment B, main view for which is contentFragment and not the whole main.xml. So if you want to add new fragment you should do it also from Fragment A.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/white"
android:id="#+id/frameLayout"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
>
</ListView>
<FrameLayout
android:id="#+id/contentFragmentb"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="#+id/contentFragmentc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
</FrameLayout>
</FrameLayout>
This should work.
Related
I am making an app using this tutorial. I made some changes in my code, ie I wanted to get data from server to my main activity and pass it to my fragments. Before that , when I would open the app it worked perfectly, bu now it crashes. I have tried a bunch of answers from stackoverflow (1, 2, 3, 4, 5...) before asking a question, but none of them answered my question. When I was getting restaurants from fragment directly it worked perfectly but passing the data from the activity causes this error.
Main Activity (I pass get data from server and pass it to fragment):
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_map)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_list)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_my_list)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
RestaurantsFragment restaurantsFragment = new RestaurantsFragment();
Bundle args = new Bundle();
args.putParcelableArrayList("restaurantList", (ArrayList<? extends Parcelable>) restaurantList);
restaurantsFragment.setArguments(args);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.sliding_tabs, restaurantsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Restaurant Fragment (I do get data from my main activity, I get the restaurants from the server when I debug):
public class RestaurantsFragment extends Fragment {
private static final String TAG = RestaurantsFragment.class.getSimpleName();
// Restaurants json url
private ProgressDialog pDialog;
private ArrayList restaurantList = new ArrayList<>();
private ListView listView;
private CustomListAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_restaurants, container, false);
Bundle bundle = this.getArguments();
if (bundle != null) {
restaurantList = bundle.getParcelableArrayList ("restaurantList");
}
listView = (ListView) view.findViewById(R.id.restaurants_list);
adapter = new CustomListAdapter(getActivity(), restaurantList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.show();
return view;
}
fragment_restaurants
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".sliderfragments.RestaurantsFragment">
<ListView
android:id="#+id/restaurants_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_row_selector" />
</RelativeLayout>
list_row
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_row_selector"
android:padding="8dp">
<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/thumbnail"
android:background="#drawable/default_profile"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp" />
<!-- User Name -->
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textSize="#dimen/userName"
android:textStyle="bold" />
<!-- Date -->
<TextView
android:id="#+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/userName"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:textSize="#dimen/date" />
<!-- Time -->
<TextView
android:id="#+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/date"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#color/time"
android:textSize="#dimen/time" />
</RelativeLayout>
content_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_main">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white" />
</android.support.design.widget.AppBarLayout>
Pager Adapter:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
MapsFragment tab1 = new MapsFragment();
return tab1;
case 1:
RestaurantsFragment tab2 = new RestaurantsFragment();
return tab2;
case 2:
MyRestaurantsFragment tab3 = new MyRestaurantsFragment();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
Stack trace:
10-09 12:30:12.755 32374-32374/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.kemo.restaurant, PID: 32374
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.kemo.restaurant/com.test.kemo.restaurant.MainActivity}: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:216)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
at android.app.Activity.performStart(Activity.java:5347)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
you are adding your fragment to your sliding tab view. you wrote :
fragmentTransaction.replace(R.id.sliding_tabs, restaurantsFragment);
tab layout is a child of horizontal scrollview see documentation: https://developer.android.com/reference/android/support/design/widget/TabLayout.html
i dont know how your application works but you cant add your fragment to TabLayout here cause it makes multiple children for that that is now allowed (TabLaout is actually a horizontal scrollView inside)
in fragmentTransaction.replace() method that i mentioned you must specify the container in which you want to add the fragment. from what your code says you are using the tabLayput for title of your viewpager not for replacing your fragment in it.
make a place (container) in your main activity in which you want to add your fragment in. absolutely its not in your R.id.sliding_tabs, beside your titles of viewpager.
How can I put my fab on my fragment? help please I'm getting errors such as.
android.view.InflateException: Binary XML file line #13: Error inflating class fragment
in my fragment acticity. Well here are my codes.
well the problem is in the getActivity().setContentView(R.layout.activity_ownerhome);
#ownerhome.java
public class ownerhome extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ownerhome);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
ownerhomeFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_owner, container, false);
ImageLoader.getInstance().init(UILConfig.config(getActivity()));
lvCars = (ListView) v.findViewById(R.id.lvOnwer);
pref = getActivity().getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
Log.d(TAG, pref.getString("username", ""));
Log.d(TAG, pref.getString("password", ""));
PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this);
tvUser = (TextView) v.findViewById(R.id.tvUser);
tvUser.setText("Hello! "+pref.getString("username", ""));
taskRead.execute("http://carkila.esy.es/user.php?owner="+pref.getString("username", ""));
FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in = new Intent(getActivity(), InsertActivity.class);
startActivity(in);
}
});
registerForContextMenu(lvCars);
return v;
}
content_ownerhome.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.ownerhome"
tools:showIn="#layout/activity_ownerhome">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.kun.carkila.ownerFragment"
android:id="#+id/ownerhomeFragment"/>
</RelativeLayout>
activity_ownerhome.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.example.kun.carkila.ownerhome">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_ownerhome" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/plus" />
</android.support.design.widget.CoordinatorLayout>
fragmentOwnerHome.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.ownerhome"
tools:showIn="#layout/activity_ownerhome">
<ListView
android:layout_width="match_parent"
android:layout_height="450sp"
android:id="#+id/lvOnwer"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:textSize="30sp"
android:textColor="#color/colorPrimaryDark"
android:id="#+id/tvUser" />
</RelativeLayout>
STACK TRACE
FATAL EXCEPTION: main
Process: com.example.kun.carkila, PID: 9267
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kun.carkila/com.example.kun.carkila.ownerhome}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.kun.carkila.ownerFragment.onCreateView(ownerFragment.java:127)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.kun.carkila.ownerhome.onCreate(ownerhome.java:12)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f0e00aa, tag null, or parent id 0xffffffff with another fragment for com.example.kun.carkila.ownerFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity
You have added the floating action button in your activity_ownerhome.xml and you are initializing it in ownerhomeFragment.java.
So add your FloatingActionButton in fragmentownerhome.xml itself like this
<RelativeLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.ownerhome"
tools:showIn="#layout/activity_ownerhome">
<ListView
android:layout_width="match_parent"
android:layout_height="450sp"
android:id="#+id/lvOnwer"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:textSize="30sp"
android:textColor="#color/colorPrimaryDark"
android:id="#+id/tvUser" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/plus" />
</RelativeLayout>
and remove the FloatingActionButton from your activity_ownerhome.xml file.
The cause of the crash is because your FAB xml code is in the "Activity layout", it is supposed to be in the "fragment layout" file
This is how i use FAB in a fragment
In my fragment layout file:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
In my fragment class file:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
return view;
}
And it works...
NOTE:
when i included android:backgroundTint="#android:color/transparent"
the application crashed and gave the error :
Binary XML file line #14: Error inflating class android.support.design.widget.FloatingActionButton
Hope this helps.
The problem isn't with the FAB, it's with how you inflate your view.
Replace this code in the Fragment:
View v = super.onCreateView(inflater, container, savedInstanceState);
getActivity().setContentView(R.layout.activity_ownerhome);
With this:
var v = inflater.inflate(R.layout.{your fragment xml}, container, false);
And remove this:
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.kun.carkila.ownerFragment"
android:id="#+id/ownerhomeFragment"/>
You should define how you want your fragment xml to look, and then inflate it using the code inflater.inflate... and put in your fragment xml there, like I wrote above.
Update:
Instead of <include layout="#layout/content_ownerhome" /> place in this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame" />
Then in your Main Activity write this code:
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
And you're supposed to see your fragment, if in the fragment you inflated your fragment xml correctly, as stated above.
You must implement the fragment in your main class something like...
public class ownerhome extends AppCompatActivity implement ownerhomeFragment {...
I'm doing an app that uses Google Maps APIs (with two separate maps), and I want to use a navigation drawer to switch between the two maps (fragments).
I created the main activity with the pre-compiled Navigation Drawer Activity in android studio, and here is the pieces of code:
The main activity uses this layout:
activity_main_activity2.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="it.radonmap.radonmap.MainActivity2">
<FrameLayout android:id="#+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start" android:name="it.radonmap.radonmap.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is the code which calls the .replace() method:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
//do something
break;
case 1:
fragment = new FragPunti();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager;
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.containerPunti,fragment).commit();
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
FragPunti.java
public class FragPunti extends android.support.v4.app.Fragment {
public FragPunti(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_punti, null);
return rootView;
}
}
frag_punti.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/containerPunti">
<FrameLayout
android:id="#+id/framePunti"
android:layout_width="0px"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Punti"/>
</FrameLayout>
</FrameLayout>
And here is the logcat trace:
04-30 19:17:45.823 20909-20909/it.radonmap.radonmap E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: it.radonmap.radonmap, PID: 20909
java.lang.IllegalArgumentException: No view found for id 0x7f090058 (it.radonmap.radonmap:id/containerPunti) for fragment FragPunti{36b6aaa5 #2 id=0x7f090058}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:945)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5274)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
I had already look for a solution but with no results.
Hope someone can help me.
You should pass id of container for fragment, and that's a view in which you want to show fragment. It is in activity, not in fragment you want to show. So change replace(R.id.containerPunti, fragment) to replace(R.id.container, fragment).
I'm implementing fragments in my activity ,sometimes it shows the following error.when i trying to load the appActivity fragment. please help me get rid out of this error
Java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.rff/com.example.rff.Parent}:
android.view.InflateException: Binary XML file line #68: Error inflating class fragment
here my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/frag_parent"
>
<TableLayout
android:id="#+id/tablenew"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FFF"
android:paddingTop="1dp"
android:stretchColumns="0"
>
<TableRow
android:background="#000000"
android:paddingBottom="1dp"
android:paddingRight="1dp"
android:tag="new">
<TextView
android:id="#+id/newactivity"
android:text="#string/newact"
android:gravity="center"
android:layout_marginLeft="1dp"
android:textColor="#000000"
android:background="#FFF"
android:padding="10dp"
android:textSize="50sp"
/>
</TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_width="1000dp"
android:layout_height="50dp">
<Button
android:id="#+id/app_new"
android:layout_width="420dp"
android:layout_height="match_parent"
android:text="Apps"/>
<Button
android:id="#+id/doc_new"
android:layout_width="400dp"
android:layout_height="match_parent"
android:text="Docs"/>
</LinearLayout>
<fragment
class="com.example.rff.AppActivity"
android:id="#+id/lm_fragment"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<fragment
class="com.example.rff.DocActivity"
android:id="#+id/doc_fragment"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
Here is my parent fragment activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AppActivity appActivity = new AppActivity();
fragmentTransaction.replace(android.R.id.content, appActivity);
fragmentTransaction.commit();
AppActivity
public class AppActivity extends Fragment{
Context context;
ArrayList<ViewGroup> viewsList;
UpdateAppThread UpdateThread;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_new_layout,container, false);
context = view.getContext();
...
....
return view ;
}
This is my best guess, since your layout files and rest of the code seem fine.
Depending on your minimum SDK declared in your Manifest, if you have it lower than 11 make sure you have the following imports:
In your fragment class: import android.support.v4.app.Fragment;
In your parent Activity: import android.support.v4.app.FragmentActivity;
Make your parent Activity extend FragmentActivity instead of Activity
really in need of some advice, dont know what is wrong here.
Context:
2 fragments with a Textview each and the main activity has 2 button and a edittext
Aim:
Type hello into the edittext box in the main activity and
When click on the button for fragment 1, the textview will change to hello.
Problem:
Face a runtime error when enter hello into edittext and click on button 1.
Logcat:
E/AndroidRuntime(1291): FATAL EXCEPTION: main
E/AndroidRuntime(1291): android.view.InflateException: Binary XML file line #29: Error inflating class fragment
E/AndroidRuntime(1291): android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime(1291):android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(1291):android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(1291): android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(1291): com.example.FragmentOne.onCreateView(FragmentOne.java:19)
E/AndroidRuntime(1291):android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
fragment_one.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ffff">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="This is fragment No.1"
android:textStyle="bold" />
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/easy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No 1" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No 2" />
<fragment
android:name="com.example.FragmentTwo"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view) {
Fragment fr;
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
FragmentOne.java
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_one, container, false);
TextView monthlypayment= (TextView) view.findViewById(R.id.textView1);
EditText easy = (EditText) inflater.inflate(R.layout.activity_main, container, false).findViewById(R.id.easy);
monthlypayment.setText(easy.getText().toString());
return view;
}
}
There are two ways you can add a fragment to the activity layout:
Declare the fragment inside the activity's layout file.
Programmatically add the fragment to an existing ViewGroup.
Both methods are mentioned in the docs
http://developer.android.com/guide/components/fragments.html
If you want add the fragment to a container you need to use a ViewGroup in xml. Generally FrameLayout is used. So have the below in xml
<FrameLayout
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
The Activity code is fine. On Button click you replace the appropriate fragment in the container.
In your Fragment onCreateView you inflate the fragment layout and use the view object to initialize views of that layout. But you inflate activity_main.xml which is not required.
Quoting docs
Specifically, the fragment can access the Activity instance with
getActivity() and easily perform tasks such as find a view in the
activity layout
So for EditText you can use
EditText easy = (EditText)getActivity().findViewById(R.id.easy);
Or Initialize EditText in Activity on Button click get the value from EditTextand then you can pass the value of EditText from Activity to Fragment
Send data from activity to fragment in android
show me the onclick method, and the container u have fragment one in... its one of two things,
either you are programmatically adding a view which already exists, or you are tryin to update the ui without a handler.... handlers can be worked around in fragments, so im assuming its the first one....