找不到片段HomeFragment的ID的视图

时间:2020-02-29 14:34:47

标签: java android xml android-fragments

我有一段时间这个问题了,看来我无法解决

HomeFragment.java


public class HomeFragment extends Fragment {
    /*- 01 Class Variables -------------------------------------------------------------- */
    private View mainView;
    private Cursor listCursor;
    // Action buttons on toolbar
    private MenuItem menuItemAddFood;
    // Holding variables
    private String currentDateYear = "";
    private String currentDateMonth = "";
    private String currentDateDay = "";

    private String currentFoodId;
    private String currentFoodName;
    private String currentFdId;
    private boolean lockPortionSizeByPcs;
    private boolean lockPortionSizeByGram;

    /*- 02 Fragment Variables ----------------------------------------------------------- */
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    /*- 03 Constructur ------------------------------------------------------------------ */
    public HomeFragment() {
        // Required empty public constructor
    }

    /*- 04 Creating Fragment ------------------------------------------------------------- */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
//OnCREATEEEEEE
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        //---------Set Title----------
       ((MainActivity)getActivity()).getSupportActionBar().setTitle("Home");
    }



    /*- 05 on Activity Created ---------------------------------------------------------- */
    // Run methods when started
    // Set toolbar menu items
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        /* Set title */
        ((MainActivity)getActivity()).getSupportActionBar().setTitle("Home");

        // getDataFromDbAndDisplay
        initializeHome();


        // Create menu
        setHasOptionsMenu(true);
    } // onActivityCreated


    /*- 06 On create view ---------------------------------------------------------------- */
    // Sets main View variable to the view, so we can change views in fragment
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mainView = inflater.inflate(R.layout.fragment_home, container, false);
        return mainView;

    }


    /*- 07 set main view ----------------------------------------------------------------- */
    // Changing view method in fragmetn
    private void setMainView(int id){
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mainView = inflater.inflate(id, null);
        ViewGroup rootView = (ViewGroup) getView();
        rootView.removeAllViews();
        rootView.addView(mainView);
    }

    /*- 08 on Create Options Menu -------------------------------------------------------- */
    // Creating action icon on toolbar
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        // Inflate menu
        MenuInflater menuInflater = ((MainActivity)getActivity()).getMenuInflater();
        inflater.inflate(R.menu.menu_home, menu);

      //  ((MainActivity)getActivity()).getMenuInflater().inflate(R.menu.menu_home, menu);

        // Assign menu items to variables
        menuItemAddFood = menu.findItem(R.id.menu_action_add_food);

    }

    /*- 09 on Options Item Selected ------------------------------------------------------ */
    // Action icon clicked on
    // Menu
    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {

        int id = menuItem.getItemId();
        if (id == R.id.menu_action_add_food) {
            AddFoodToDiarySelectedMealNumber();
        }
        return super.onOptionsItemSelected(menuItem);
    }


MainActivty

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener,
        AddFoodToDiaryFragment.OnFragmentInteractionListener,
        CategoriesFragment.OnFragmentInteractionListener,
        FoodFragment.OnFragmentInteractionListener,
        GoalFragment.OnFragmentInteractionListener,
        HomeFragment.OnFragmentInteractionListener,
        ProfileFragment.OnFragmentInteractionListener{

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* Toolbar */
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        /* Inialize fragmet */
        Fragment fragment = null;
        Class fragmentClass = null;
        fragmentClass = HomeFragment.class;
        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

        /* Navigation */
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        // Navigation items
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        //stetho---
        Stetho.initializeWithDefaults(this);
        new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build();

        /* Database */
        DBAdapter db = new DBAdapter(this);
        db.open();

        /* Setup for food */
        // Count rows in food
        int numberRows = db.count("food");

        if(numberRows < 1){
            // Run setup
            Toast.makeText(this, "Loading setup...", Toast.LENGTH_LONG).show();
            DBSetupInsert setupInsert = new DBSetupInsert(this);
            setupInsert.insertAllCategories();
            setupInsert.insertAllFood();
            Toast.makeText(this, "Setup completed!", Toast.LENGTH_LONG).show();

        }
        //check if there users in user table
        //count rows in user table

        numberRows = db.count("users");
        Intent intent;

        if(numberRows <1 ){

            //sign Up
            Toast.makeText(this, " you only few steps away from Signing up ...!", Toast.LENGTH_LONG).show();

            intent = new Intent(MainActivity.this, SignUp.class);
            startActivity(intent);
        }else{

            intent = new Intent(MainActivity.this, EditProfileGoal.class);
            startActivity(intent);

        }
        db.close();
    }

home_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp"
        android:layout_marginRight="18dp"
        android:layout_marginBottom="18dp"
        android:layout_marginLeft="18dp">


        <TextView
            android:id="@+id/text_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />


        <!-- Breakfast -->
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TableRow
                android:id="@+id/tableRowHeadlineBreakfast"
                android:layout_width="fill_parent"
                android:background="#A9A9A9 "
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imageViewAddBreakfast"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_menu_add" />

                <TextView
                    android:id="@+id/textViewHeadlineBreakfast"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="80"
                    android:layout_marginBottom="10dp"
                    android:text="@string/break_fast"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewEnergyBreakfast"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="10"
                    android:text="@string/energy"

                    android:textSize="18sp" />


            </TableRow>

        </TableLayout>
        <TableLayout
            android:id="@+id/tableLayoutBreakfastItems"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        </TableLayout>
        <!-- //Breakfast -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:id="@+id/textView"
            android:background="#000"></TextView>

        <!-- Lunch -->
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TableRow
                android:id="@+id/tableRowHeadlineLunch"
                android:layout_width="fill_parent"
                android:background="#A9A9A9 "
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imageViewAddLunch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_menu_add" />

                <TextView
                    android:id="@+id/textViewHeadlineLunch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="80"
                    android:layout_marginBottom="10dp"

                    android:text="@string/lunch"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewEnergyLunch"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="10"
                    android:text="@string/energy"
                    android:textSize="18sp" />


            </TableRow>
        </TableLayout>
        <TableLayout
            android:id="@+id/tableLayoutLunchItems"
            android:layout_width="fill_parent"


            android:layout_height="wrap_content">

        </TableLayout>
        <!-- //Lunch -->


        <TextView
            android:layout_width="match_parent"
            android:layout_height="3dp"

            android:background="#000"></TextView>




        <!-- Before training -->
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TableRow
                android:id="@+id/tableRowHeadlineBeforeTraining"
                android:layout_width="fill_parent"
                android:background="#A9A9A9 "
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imageViewAddBeforeTraining"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_menu_add" />

                <TextView
                    android:id="@+id/textViewHeadlineBeforeTraining"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="80"
                    android:layout_marginBottom="10dp"

                    android:text="@string/before_training"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewEnergyBeforeTraining"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="10"

                    android:text="@string/energy"
                    android:textSize="18sp" />



            </TableRow>
        </TableLayout>
        <TableLayout
            android:id="@+id/tableLayoutBeforeTrainingItems"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        </TableLayout>
        <!-- //BeforeT raining -->

        <TextView
            android:layout_width="match_parent"
            android:layout_height="3dp"

            android:background="#000"></TextView>


        <!-- After training -->
        <TableLayout
            android:layout_width="fill_parent"
            android:background="#A9A9A9 "
            android:layout_height="wrap_content">

            <TableRow
                android:id="@+id/tableRowHeadlineAfterTraining"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imageViewAddAfterTraining"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_menu_add" />

                <TextView
                    android:id="@+id/textViewHeadlineAfterTraining"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="80"
                    android:layout_marginBottom="10dp"

                    android:text="@string/after_training"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewEnergyAfterTraining"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="10"
                    android:text="@string/energy"
                    android:textSize="18sp" />



            </TableRow>
        </TableLayout>
        <TableLayout
            android:id="@+id/tableLayoutAfterTrainingItems"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        </TableLayout>
        <!-- //After Training -->


        <TextView
            android:layout_width="match_parent"
            android:layout_height="3dp"

            android:background="#000"></TextView>


        <!-- Dinner -->
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TableRow
                android:id="@+id/tableRowHeadlineDinner"
                android:layout_width="fill_parent"
                android:background="#A9A9A9 "
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imageViewAddDinner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_menu_add" />

                <TextView
                    android:id="@+id/textViewHeadlineDinner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="80"
                    android:layout_marginBottom="10dp"

                    android:text="@string/dinnner"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewEnergyDinner"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="10"
                    android:text="@string/energy"
                    android:textSize="18sp" />

            </TableRow>
        </TableLayout>
        <TableLayout
            android:id="@+id/tableLayoutDinnerItems"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        </TableLayout>
        <!-- //Dinner -->

        <TextView
            android:layout_width="match_parent"
            android:layout_height="3dp"

            android:background="#000"></TextView>


        <!-- Snacks -->
            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <TableRow
                    android:id="@+id/tableRowHeadlineSnacks"
                    android:layout_width="fill_parent"
                    android:background="#A9A9A9 "
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/imageViewAddSnacks"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:srcCompat="@drawable/ic_menu_add" />

                    <TextView
                        android:id="@+id/textViewHeadlineSnacks"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="80"
                        android:layout_marginBottom="10dp"

                        android:text="@string/snacks"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/textViewEnergySnacks"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:layout_weight="10"
                        android:text="@string/energy"
                        android:textSize="18sp" />


                </TableRow>
            </TableLayout>
            <TableLayout
                android:id="@+id/tableLayoutSnacksItems"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

            </TableLayout>
        <!-- //Snacks -->

        <TextView
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:background="#000"></TextView>

        <!-- Supper -->
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TableRow
                android:id="@+id/tableRowHeadlineSupper"
                android:layout_width="fill_parent"
                android:background="#A9A9A9 "
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imageViewAddSupper"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_menu_add" />

                <TextView
                    android:id="@+id/textViewHeadlineSupper"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="80"
                    android:layout_marginTop="10dp"

                    android:text="@string/supper"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewEnergySupper"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="10"
                    android:text="@string/energy"
                    android:textSize="18sp" />
            </TableRow>
        </TableLayout>
        <TableLayout
            android:id="@+id/tableLayoutSupperItems"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        </TableLayout>
        <!-- //Supper -->

        <TextView
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:background="#000"></TextView>
        <!-- todays reuslt -->
        <!-- total -->
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TableRow
                android:id="@+id/tableRowHeadlineTotal"
                android:layout_width="fill_parent"
                android:background=" #D3D3D3    "
                android:layout_height="wrap_content">



                <TextView
                    android:id="@+id/textViewHeadlineGoalWithActivity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginTop="10dp"

                    android:text="@string/goal_with_activity"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewFood"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="@string/food_calories"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewSum"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="@string/remaining"
                    android:textSize="18sp" />

            </TableRow>
            <!---->

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/headcell"

                >

                <TextView
                    android:id="@+id/textViewBodyGoalWithActivity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginTop="10dp"

                    android:text="@string/goal_with_activity"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewBodyFood"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="@string/food_calories"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textViewBodyRemain"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="@string/remaining"
                    android:textSize="18sp" />

            </TableRow>

        </TableLayout>

        <!-- end todays reuslt -->


    </LinearLayout>

</ScrollView>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_fragment"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.appdiet33, PID: 6829
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appdiet33/com.example.appdiet33.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f09009d (com.example.appdiet33:id/flContent) for fragment HomeFragment{35b333b} (1260b40b-35da-49bd-9060-13848371c9d8) id=0x7f09009d}
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f09009d (com.example.appdiet33:id/flContent) for fragment HomeFragment{35b333b} (1260b40b-35da-49bd-9060-13848371c9d8) id=0x7f09009d}
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:305)


我曾尝试使用getChildFragmentManager()而不是getSupportFragmentManager(),类似询问类似问题 在HomeFragment中 问了一个问题,但这对我不起作用

有什么建议吗?

编辑: 我解决了原来的问题,我app_bar_main.xml中的ID拼写错误并修复了

0 个答案:

没有答案