为什么BlankFragment不工作?

时间:2016-08-29 15:33:05

标签: android android-fragments

我正在尝试创建一个应用程序,只要您单击导航抽屉中的选项,就会显示一个片段。但是,在创建BlankFragment时(使用BlankFragment fragment = new BlankFragment();,我收到错误:BlankFragment变为红色,而Android Studio无法识别它。

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

1 个答案:

答案 0 :(得分:1)

你错过了ShooksFragment中的df$mgr_mgr_id <- mapply(get_mgr_mgr_id, df$mgr_id, df$period) df id mgr_id period left joined mgr_mgr_id 1 1 NA 1 1 0 NA 2 2 1 1 0 0 NA 3 3 1 1 0 0 NA 4 4 2 1 0 0 1 5 5 2 1 0 0 1 6 6 2 1 0 0 1 7 7 2 1 0 0 1 8 8 3 1 0 0 1 9 9 3 1 0 0 1 10 10 5 1 0 0 2 11 11 5 1 0 0 2 12 2 NA 2 0 0 NA 13 3 2 2 0 0 NA 14 4 5 2 0 0 2 15 5 2 2 0 0 NA 16 6 5 2 0 0 2 17 7 5 2 0 0 2 18 8 3 2 0 0 2 19 9 3 2 0 0 2 20 10 5 2 0 0 2 21 11 10 2 0 0 5 22 12 10 2 0 1 5 方法,

添加此方法,请报告回来。

newInstance()

编辑:

由于你想在MainActivity中加载ShooksFragment,你可以简单地创建一个ShooksFragment的对象并使用片段管理器加载该片段。

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

    ShooksFragment fragment = new ShooksFragment();

    return fragment;
}