How to put FloatingActionButton to Fragment? - java

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 {...

Related

Why isn’t the viewpager being recognised on the fragment?

I’m trying to use a tabbed activity within a fragment, so a fragment with two other fragments. I keep getting the NullPointerException for the viewPager object, which has already been mapped by the right id. Whats gone wrong ?
public class BookFragment extends Fragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_books, container, false);
PageAdapter pageAdapter = new PageAdapter(getChildFragmentManager(), 1);
pageAdapter.addFragment("Pending", new PendingFragment());
pageAdapter.addFragment("Scheduled", new ScheduledFragment());
ViewPager viewPager = view.findViewById(R.id.pager_books);
viewPager.setAdapter(pageAdapter);
TabLayout tabs = view.findViewById(R.id.book_tabs);
tabs.setupWithViewPager(viewPager);
return view;
}
}
The fragment Layout (fragment_books.xml) being inflated;
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".views.bookhub_navigation.ui_fragments.books.BookFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabLayout
android:id="#+id/book_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:id="#+id/pending_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pending" />
<com.google.android.material.tabs.TabItem
android:id="#+id/scheduled_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scheduled" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager_books"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
It keeps giving a NullPointerException when the id pager_books is in the right view heirarchy which is the fragment_books.xml file.
The error log
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)' on a null object reference at com.immers_ltd.views.bookhub_navigation.ui_fragments.books.BookFragment.onCreateView(DateFragment.java:58) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7948) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

java.lang.RuntimeException: Unable to start activity android.view.InflateException: Binary XML file line #13

I'm new at learning Android. I'll be short. I'm receiving the error as mentioned at the bottom. I've added a fragment via XML. I've searched for solutions but none worked so far. Where am I going wrong?
If I can be pointed to a solution that's okay. May I get an explanation as to why this happens?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.workout, PID: 7653
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workout/com.example.workout.DetailActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2969)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3047)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Must specify unique android:id, android:tag, or have a parent with an id for com.example.workout.WorkoutDetailFragment
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3177)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:791)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.workout.DetailActivity.onCreate(DetailActivity.java:12)
at android.app.Activity.performCreate(Activity.java:7110)
at android.app.Activity.performCreate(Activity.java:7101)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2922)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3047)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
DetailActivity Xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".DetailActivity">
<fragment
class="com.example.workout.WorkoutDetailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
DetailActivity Java
package com.example.workout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class DetailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
}
}
Fragment xml
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textTitle"
android:text="#string/workout_title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textDescription"
android:text="#string/workout_description"/>
</LinearLayout>
MainActivity Java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onShowDetails(View view){
Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);
}
}
fix your detail_activity.xml replace class by android:name
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".DetailActivity">
<fragment
android:name="com.example.workout.WorkoutDetailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Modify your DetailActivity.xml tag fragment as follow
<fragment android:name="com.example.workout.WorkoutDetailFragment"
android:id="#+id/frag"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Implement your main activity with OnFragmentInteractionListener and change xml as per below code
public class MainActivity extends AppCompatActivity implements WorkoutDetailFragment.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
}
#Override
public void onFragmentInteraction(Uri uri) {
}}
And your XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<fragment
android:id="#+id/fragment"
android:name="com.example.myapplication.WorkoutDetailFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Share Fragment class file if you still can't execute
Please specify the unique id for your fragment and try.
<fragment
android:id="#+id/fragId"
class="com.example.workout.WorkoutDetailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

Java Null Pointer Exception on 'android.view.Window$Callback android.view.Window.getCallback()'

I was trying to add a collapsing Toolbar to a Fragment, but the app crashed as soon as I opened the Fragment:
This is the error I get:
2018-10-19 18:05:38.950 13301-13301/io.github.davidwickerhf.diceroller E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.github.davidwickerhf.diceroller, PID: 13301
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at io.github.davidwickerhf.diceroller.ProfileFragment.onCreateView(ProfileFragment.java:36)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:809)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7436)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
Fragment Java:
public class ProfileFragment extends Fragment {
private MainActivity mMainActivity;
private SettingsViewModel settingsViewModel;
private SettingAdapter adapter;
private CoordinatorLayout coordinatorLayout;
private View fragmentView;
private TabLayout mTabLayout;
private ViewPager mViewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragmentView = inflater.inflate(R.layout.fragment_profile, container, false);
mMainActivity = new MainActivity();
androidx.appcompat.widget.Toolbar mToolbar = fragmentView.findViewById(R.id.profile_toolbar);
mMainActivity.setSupportActionBar(mToolbar);
mViewPager = (ViewPager) fragmentView.findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(mMainActivity.getSupportFragmentManager());
adapter.AddFragment(new ProfileInfoFragment(), "Profile");
adapter.AddFragment(new ProfileMessagingFragment(), "Messages");
mViewPager.setAdapter(adapter);
mTabLayout = (TabLayout) fragmentView.findViewById(R.id.tab_layout_profile);
mTabLayout.setupWithViewPager(mViewPager);
return fragmentView;
}
}
The Fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ProfileFragment"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme"
app:toolbarId="#id/profile_toolbar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<ImageView
android:id="#+id/background_image_Toolbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:layout_marginTop="24dp"
android:id="#+id/profile_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
android:id="#+id/tab_layout_profile"
app:tabTextColor="#color/greyText"
app:tabIndicatorColor="#color/greyText"
android:background="#fafafa"
android:elevation="0dp">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewpager">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.viewpager.widget.ViewPager>
Please help?
I guess you are getting that error because you are instantiating a new MainActivity object:
mMainActivity = new MainActivity();
In case of Fragments, you can call getActivity() to get the reference of the activity that is hosting your Fragment. Something like:
private MainActivity mMainActivity;
....
mMainActivity = (MainActivity) getActivity();
Or, you don't even need to cast to MainActivity since you are invoking a common method (available on Activity)
private Activity mMainActivity;
....
mMainActivity = getActivity();

Show google map with drawer

We have different differnet activities of google map ( Some with tabs, Some simple, And some other activities), So we are are trying to implement navigation drawer in our app, For this we created a class which extend ActionBarActivity and has drawer, And we are extending this main activity in our classes for navigation drawer, But when we open our application its showing map is null.
Please help.
Here is our files
drawerlist.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FA0"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/shitstuff"
app:itemTextColor="#000"
app:menu="#layout/drawermenu"
android:layout_marginTop="-24dp"
/>
</android.support.v4.widget.DrawerLayout>
mapload.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffc17540"
android:id="#+id/maptab"
>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:gravity="bottom"
android:layout_height="fill_parent"
/>
<RelativeLayout
android:id="#+id/slidingContainer"
android:layout_width="fill_parent"
android:gravity="bottom"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/t"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffff773c"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/rg_views"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton
android:id="#+id/rb_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/str_rb_normal"
android:checked="true" />
<RadioButton
android:id="#+id/rb_satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/str_rb_satellite" />
<RadioButton
android:id="#+id/rb_terrain"
android:layout_width="161dp"
android:layout_height="wrap_content"
android:text="#string/str_rb_terrain" />
</RadioGroup>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/inhorizontalscrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#android:drawable/ic_input_add" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<View
android:id="#+id/transparentView"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="#dimen/map_height"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</RelativeLayout>
BaseActivity.java
public class BaseActivity extends ActionBarActivity
{
public DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_list);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// You were missing this setHomeAsUpIndicator
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_navigation_drawer);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView n = (NavigationView) findViewById(R.id.shitstuff);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout , R.string.drawer_open, R.string.drawer_close)
{
/*
* Called when a drawer has settled in a completely closed state
*/
public void onDrawerClosed(View view)
{
Log.d("drawerToggle", "Drawer closed");
super.onDrawerClosed(view);
// getActionBar().setTitle(R.string.app_name);
invalidateOptionsMenu(); //Creates call to onPrepareOptionsMenu()
}
/*
* Called when a drawer has settled in a completely open state
*/
public void onDrawerOpened(View drawerView)
{
Log.d("drawerToggle", "Drawer opened");
super.onDrawerOpened(drawerView);
// getActionBar().setTitle("NavigationDrawer");
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
n.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
////.......
}
mDrawerLayout.closeDrawers(); // CLOSE DRAWER
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// THIS IS YOUR DRAWER/HAMBURGER BUTTON
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}
}
Mylocation.java
public class MyLocation extends BaseActivity implements View.OnClickListener,OnMapReadyCallback {
GoogleMap googleMap;
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mapload, null, false); // line no-74
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
But everytime i run application i am getting this error
java.lang.RuntimeException: Unable to start activity ComponentInfo{MyLocation}: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at com.myapp.gps.MyLocation.onCreate(MyLocation.java:74) //error here
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7237) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at com.myapp.gps.MyLocation.onCreate(MyLocation.java:74)  //error here
at android.app.Activity.performCreate(Activity.java:6876) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
What I tried:-
inflater.from(getApplicationContext()).inflate(R.layout.mapload, pageHolder, true);
GoogleMap mapFragment =((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); //getting null pointer exception here
another thing which I tried
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapFragment).commit(); //same null pointer exception
The line with inflater.inflate is doing nothing useful there in that class. The XML layout isn't loaded and findFragmentById(R.id.map) is returning your null.
I think you wanted something like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.mapload);
// Find fragment here
}
Or maybe you maybe you just wanted setContentView from the parent class?
Basically, the error is saying your <fragment> tag is missing a class.
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
<!-- Needs a class or name attribute -->
android:gravity="bottom"
android:layout_height="fill_parent" />
For example, from the documentation
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
(Also worth mentioning that xmlns:android only is to be applied to the root xml element)
Though, I do not think you need a whole separate Activity or layout.
Load the map fragment into this FrameLayout
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
just like any regular Fragment...
SupportMapFragment mapFragment = new SupportMapFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content, mapFragment)
.commit();
mapFragment.getMapAsync(this);
add this line to your xml:
android:name="com.google.android.gms.maps.SupportMapFragment"
like this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:gravity="bottom"
android:layout_height="fill_parent"/>
and the best way to implement google map with drawer is to use fragment, that's mean change your class "MyLocation" to extends from fragment

Passing data from activity to fragment causes "ScrollView can host only one direct child" error

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.

Categories