简体   繁体   English

为什么BlankFragment不起作用?

[英]Why isn't BlankFragment not working?

I'm trying to create an app that displays a fragment whenever you click an option in a navigation drawer. 我正在尝试创建一个在单击导航抽屉中的选项时显示片段的应用程序。 However, when creating a BlankFragment (by using BlankFragment fragment = new BlankFragment(); , I get an error: BlankFragment turns into red and Android Studio doesn't recognize it. 但是,在创建BlankFragment时(通过使用BlankFragment fragment = new BlankFragment(); ,我收到一个错误: BlankFragment变成红色,而Android Studio无法识别它。

MainActivity.java MainActivity.java

...

    } else if (id == R.id.nav_shooks) {

        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        BlankFragment fragment = new BlankFragment();
        transaction.add(R.id.shooksLayout, fragment);

        transaction.commit();

    } else if (id == R.id.nav_prizes) {

...

ShooksFragment 片段

   package com.shook.android.shook;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ShooksFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class ShooksFragment extends Fragment {

    private OnFragmentInteractionListener mListener;

    public ShooksFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_shooks, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

Here is the error. 这是错误。 As you can see, the word BlankFragment is in red 如您所见,BlankFragment字样为红色

You're missing the newInstance() method in the ShooksFragment, 您在ShooksFragment中缺少newInstance()方法,

Add this method and please report back. 添加此方法,然后报告。

// TODO: Rename and change types and number of parameters
public static ShooksFragment newInstance() {

    ShooksFragment fragment = new ShooksFragment();

    return fragment;
}

EDIT : 编辑:

Since you want to load ShooksFragment inside MainActivity, you can simply make an object of the ShooksFragment and load that fragment using the fragment manager. 由于要在MainActivity中加载ShooksFragment,因此只需创建ShooksFragment的对象,然后使用片段管理器加载该片段。

...

    } else if (id == R.id.nav_shooks) {

        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        ShooksFragment fragment = new ShooksFragment();
        transaction.add(R.id.shooksLayout, fragment);

        transaction.commit();

    } else if (id == R.id.nav_prizes) {

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM