OnFragmentInteractionListener

OnFragmentInteractionListener

尝试通过片段连接从sqlite数据库填充的列表视图。在下面粘贴的代码中,您将看到我的开关中有一个case块,因为我只是想在有更多片段之前使它工作。

异常我得到“ ...必须实现OnFragmentInteractionListener”

public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawer;
private Toolbar toolbar;

private NavigationView mNVDrawer;
private ActionBarDrawerToggle drawerToggle;

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

    //Set a Toolbar to replace the ActionBar.
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerToggle = setupDrawerToggle();

    mDrawer.setDrawerListener(drawerToggle);

    //Find our drawer view
    mNVDrawer = (NavigationView) findViewById(R.id.nvView);
    //Setup drawer view
    setupDrawerContent(mNVDrawer);

}

private ActionBarDrawerToggle setupDrawerToggle() {
    return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open,  R.string.drawer_close);
}

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    selectDrawerItem(menuItem);
                    return true;
                }
            });
}

public void selectDrawerItem(MenuItem menuItem) {
    //Creat a new fragment and specify the planet to show based on
    //position
    Fragment fragment = null;

    Class fragmentClass;
    switch (menuItem.getItemId()) {
        case R.id.nav_first_fragment:
            fragmentClass = AllQuestionsFragment.class;
            break;
        default:
            fragmentClass = AllQuestionsFragment.class;
    }

    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }

    //Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

    //Highlight the selected item, update the title, and close the drawer
    menuItem.setChecked(true);
    setTitle(menuItem.getTitle());
    mDrawer.closeDrawers();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //The action bar home/up action should open or close the drawer
    switch (item.getItemId()) {
        case android.R.id.home:
            mDrawer.openDrawer(GravityCompat.START);
            return true;
    }

    if (drawerToggle.onOptionsItemSelected(item)){
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    //Sync the toggle state after onRestoreInstanceState has occurred
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged (Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //Pass any configuration change to the drawer toggles
}


}

public class AllQuestionsFragment extends android.support.v4.app.Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
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;

private DataBaseHelper mDBHelper;
private Cursor mCursor;
private QuestionAdapter adapter;

private static Context mContext;

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

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment AllQuestionsFragment.
 */
// TODO: Rename and change types and number of parameters
public static AllQuestionsFragment newInstance(String param1, String param2) {
    AllQuestionsFragment fragment = new AllQuestionsFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    mContext = getActivity().getApplicationContext();

    ListView mListView = (ListView) getView().findViewById(R.id.list_view);

    mDBHelper = new DataBaseHelper(mContext);

    mDBHelper.createDataBase();

    try {
        mDBHelper.openDataBase();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    mCursor = mDBHelper.getCursor();
    getActivity().startManagingCursor(mCursor);

    adapter = new QuestionAdapter(mCursor);
    mListView.setAdapter(adapter);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_all_questions, 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);
}

class QuestionAdapter extends CursorAdapter {

    QuestionAdapter(Cursor c) {
        super(mContext, c);
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View row = inflater.inflate(R.layout.list_view_row, parent, false);
        QuestionHolder holder = new QuestionHolder(row);
        row.setTag(holder);

        return row;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        QuestionHolder holder = (QuestionHolder) view.getTag();
        holder.populateFrom(cursor, mDBHelper);
    }
}

static class QuestionHolder {
    private TextView txt = null;

    QuestionHolder(View row) {
        txt = (TextView) row.findViewById(R.id.tv_question);
    }

    void populateFrom(Cursor c, DataBaseHelper r) {
        txt.setText(r.getName(c));
    }
}

}


错误信息

01-11 15:08:24.539 6491-6491/com.example.victor.nattest4 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.victor.nattest4, PID: 6491
                                                                       java.lang.RuntimeException: com.example.victor.nattest4.MainActivity@cedfc0a must implement OnFragmentInteractionListener
                                                                           at com.example.victor.nattest4.AllQuestionsFragment.onAttach(AllQuestionsFragment.java:119)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
                                                                           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.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5421)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

最佳答案

更改

public class MainActivity extends AppCompatActivity {




public class MainActivity extends AppCompatActivity
implements AllQuestionsFragment.OnFragmentInteractionListener {


如日志中所述,


  com.example.victor.nattest4.MainActivity@cedfc0a必须实现
  OnFragmentInteractionListener


并覆盖onFragmentInteraction(Uri uri)方法。

编辑:

您需要在片段的onCreateView方法中而不是在onCreate内部初始化布局:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

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

    ListView mListView = (ListView) rootView.findViewById(R.id.list_view);

    mContext = getActivity().getApplicationContext();
    mDBHelper = new DataBaseHelper(mContext);
    mDBHelper.createDataBase();

    try {
        mDBHelper.openDataBase();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    mCursor = mDBHelper.getCursor();
    getActivity().startManagingCursor(mCursor);

    adapter = new QuestionAdapter(mCursor);
    mListView.setAdapter(adapter);

    return rootView;
}

10-05 18:25