Toolbar, Toggle in fragments



  • Got it. main_activity.xmlwhich is a fragmentary container. In all the fragments, I plan to use the tabs with other fragments.

    How better initial. ToolBar and ActionBarDrawerTogglein the fragments themselves MainActivity?

    At this point, the annex is crashing (low below). If you can comment on the lines:

    initTogle(rootView, activity); 
    initTabs(rootView);
    

    the app is coming down, but it's going to be a mistake.

    MainActivity:

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        setTheme(R.style.AppDefault);
        super.onCreate(savedInstanceState);
    
    
    
        startFragment1();
    
    
    }
    
    
    
    private void startFragment1() {
    
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container, new Fragment1(getSupportFragmentManager(), this))
                .addToBackStack(null)
                .commit();
    
    }
    

    }

    Fragment1

    package ru.alexbykov.fragmentexampler;

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.design.widget.TabLayout;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.view.ViewPager;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.widget.Toolbar;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    /**

    • A simple {@link Fragment} subclass.
      */
      public class Fragment1 extends Fragment {

      ViewPager viewPager;
      TabLayout tabLayout;

      Toolbar toolbar;

      FragmentManager fm;

      DrawerLayout drawerLayout;
      Activity activity;

      public Fragment1(FragmentManager fm, Activity activity) {

       this.activity = activity;
       this.fm = fm;
      

      }

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {

       View rootView = inflater.inflate(R.layout.fragment_fragment1, container, false);
      
      
      initToolbar(rootView);
      

      initTogle(rootView, activity);

       initTabs(rootView);
      
      
       return rootView;
      

      }

    private void initToolbar(View rootView) {

    toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
    toolbar.setTitle("Fragment1");
    

    }

    private void initTogle(View rootView, Activity activity) {
    
    
    
    
        drawerLayout = (DrawerLayout) rootView.findViewById(R.id.tabLayout);
        ActionBarDrawerToggle togle = new ActionBarDrawerToggle(
                activity,
                drawerLayout,
                toolbar,
                R.string.open,
                R.string.close
        );
    
        togle.syncState();
    
    }
    
    private void initTabs(View rootView) {
    
        viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
        tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
        TabsFragmentAdapter adapter = new TabsFragmentAdapter(fm);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }
    

    }

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout 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="ru.alexbykov.fragmentexampler.MainActivity">

    &lt;android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container"
    
    
       /&gt;
    
    
    &lt;android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/navigationView"
        android:layout_gravity="start"
    
    
    
        /&gt;
    

    </android.support.v4.widget.DrawerLayout>

    fragment_fragment1.xml

    <FrameLayout 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="ru.alexbykov.fragmentexampler.Fragment1">

    &lt;android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"&gt;
    
    
      &lt;include layout="@layout/toolbar"/&gt;
    
        &lt;android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
    
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight = "6dp"
    
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@android:color/white"
            /&gt;
    
    &lt;/android.support.design.widget.AppBarLayout&gt;
    
    
    &lt;android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
    
    
        /&gt;
    

    </FrameLayout>

    toolbar.xml

        <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"

        /&gt;
    

    Log, no initialization of the Tulbar and the thug through the fragment. Annex starting)

    07-06 11:01:55.453 18604-18604/ru.alexbykov.fragmentexampler E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.alexbykov.fragmentexampler/ru.alexbykov.fragmentexampler.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ru.alexbykov.fragmentexampler.Fragment1: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
    at android.app.ActivityThread.access$700(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ru.alexbykov.fragmentexampler.Fragment1: make sure class name exists, is public, and has an empty constructor that is public
    at android.support.v4.app.Fragment.instantiate(Fragment.java:431)
    at android.support.v4.app.FragmentState.instantiate(Fragment.java:102)
    at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1952)
    at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:144)
    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:307)
    at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:81)
    at ru.alexbykov.fragmentexampler.MainActivity.onCreate(MainActivity.java:13)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692) 
    at android.app.ActivityThread.access$700(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5041) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.InstantiationException: can't instantiate class ru.alexbykov.fragmentexampler.Fragment1; no empty constructor
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1319)
    at android.support.v4.app.Fragment.instantiate(Fragment.java:420)
    at android.support.v4.app.FragmentState.instantiate(Fragment.java:102) 
    at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1952) 
    at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:144) 
    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:307) 
    at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:81) 
    at ru.alexbykov.fragmentexampler.MainActivity.onCreate(MainActivity.java:13) 
    at android.app.Activity.performCreate(Activity.java:5104) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692) 
    at android.app.ActivityThread.access$700(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5041) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    at dalvik.system.NativeStart.main(Native Method) 

    Log with initialization of the Tulbar and thickness (annex is crashing)

    07-06 11:05:47.613 19933-19933/ru.alexbykov.fragmentexampler E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.alexbykov.fragmentexampler/ru.alexbykov.fragmentexampler.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ru.alexbykov.fragmentexampler.Fragment1: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
    at android.app.ActivityThread.access$700(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ru.alexbykov.fragmentexampler.Fragment1: make sure class name exists, is public, and has an empty constructor that is public
    at android.support.v4.app.Fragment.instantiate(Fragment.java:431)
    at android.support.v4.app.FragmentState.instantiate(Fragment.java:102)
    at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1952)
    at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:144)
    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:307)
    at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:81)
    at ru.alexbykov.fragmentexampler.MainActivity.onCreate(MainActivity.java:13)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692) 
    at android.app.ActivityThread.access$700(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5041) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.InstantiationException: can't instantiate class ru.alexbykov.fragmentexampler.Fragment1; no empty constructor
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1319)
    at android.support.v4.app.Fragment.instantiate(Fragment.java:420)
    at android.support.v4.app.FragmentState.instantiate(Fragment.java:102) 
    at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1952) 
    at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:144) 
    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:307) 
    at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:81) 
    at ru.alexbykov.fragmentexampler.MainActivity.onCreate(MainActivity.java:13) 
    at android.app.Activity.performCreate(Activity.java:5104) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692) 
    at android.app.ActivityThread.access$700(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5041) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    at dalvik.system.NativeStart.main(Native Method) 



    1. In your mistake, it is clear that the fragment should have an empty, public designer. It's not empty. You don't have to transfer something to fragments/activities through the designer. It's better to think that these classes don't have a designer.

    2. Transfer values to fragments through Bundle transmitted through method setArguments(Bundle args)

    3. You need to get a reference to the activists in the method. onAttach() by getActivity() Or it's rocked. Context to Activity

    4. FragmentManager If you'll get at the right moment, just call. getSupportFragmentManager()

    5. Fragments in the fragment are a situation that leads to many bagos. You better not try that. If you need to display fragments in ViewPager If you don't, you're gonna have to act on person. ViewPager♪ If you need it, NavigationDrawerThen do the basic activate with its implementation and then activate it.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2