在发布之前我已经尝试过几个方面,其中一个是关注此stackoverflow: How to change fragments using Android navigation drawer
然而,当我尝试在那里发布的一些解决方案时,即使片段是由android studio创建的,我也会收到错误。它说的是关于fragment.v4的东西,而在整个由android studio创建的片段中使用了它。
这是我的代码:
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = new Fragment();
switch(position) {
case 1:
fragment = new WeatherFragment();
break;
case 2:
fragment = new WeatherFragment();
break;
}
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}
点击第二项时我尝试更改的天气片段未显示。
任何提示?
这是我当前的错误:
06-10 05:28:23.247 24328-24328 / nl.shacklez.mijnrecepten E / AndroidRuntime:FATAL EXCEPTION:main 处理:nl.shacklez.mijnrecepten,PID:24328 java.lang.ClassCastException:nl.shacklez.mijnrecepten.MainActivity@2c5b3c99必须实现OnFragmentInteractionListener 在nl.shacklez.mijnrecepten.WeatherFragment.onAttach(WeatherFragment.java:83) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:853) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 在android.app.BackStackRecord.run(BackStackRecord.java:833) 在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:447) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
这是我的weatherFrag课程:
package nl.shacklez.mijnrecepten;
import android.app.Activity;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class WeatherFragment extends 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;
/**
* 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 WeatherFragment.
*/
// TODO: Rename and change types and number of parameters
public static WeatherFragment newInstance(String param1, String param2) {
WeatherFragment fragment = new WeatherFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public WeatherFragment() {
// Required empty public constructor
}
@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
return inflater.inflate(R.layout.fragment_weather, 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(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.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
public void onFragmentInteraction(Uri uri);
}
}
答案 0 :(得分:0)
首先确保您的片段确实扩展了Fragment,然后使用此代码:
@SuppressLint("NewApi")
void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new firstFragment();
break;
case 2:
fragment = new secondFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
如果你不喜欢使用支持片段而不是android.app.Fragment;
尝试导入V4 fragment
如果仍然显示错误,请确保已从SDK管理器安装Android Support Library
,您还可以检查依赖项并重新同步项目。
希望这会有所帮助
答案 1 :(得分:0)
查看以下代码,
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
在你的片段中。你必须实现这个
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
您活动中片段的界面。这意味着您的活动应该实现WeatherFragment.OnFragmentInteractionListener。这个思想是Android Studio创建的片段的一个特性。它创建此接口以在片段和活动之间进行通信。通常我会在onAttach函数中的super.onAttach(activity)之后删除代码。