- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
它抛出一个错误,说我必须实现 OnFragmentInteractionListener,但据我所知,我正在正确实现它......任何帮助将不胜感激。
主要 Activity
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity implements NavFragment.OnFragmentInteractionListener, ContentFragment.OnFragmentInteractionListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.nav, new NavFragment());
fragmentTransaction.commit();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content, new ContentFragment());
fragmentTransaction.commit();
}
@Override
public void onNavFragmentInteraction(String string) {
Bundle args = new Bundle();
args.putString("string", string);
ContentFragment content = new ContentFragment();
content.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
@Override
public void onContentFragmentInteraction(String string){
}
}
导航 fragment
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class NavFragment extends Fragment implements View.OnClickListener {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private Button button;
private OnFragmentInteractionListener mListener;
public static NavFragment newInstance(String param1, String param2) {
NavFragment fragment = new NavFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public NavFragment() {
}
@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) {
View v = inflater.inflate(R.layout.fragment_nav, container, false);
button = (Button) v.findViewById(R.id.submit);
button.setOnClickListener(this);
return v;
}
public void onButtonPressed(String string) {
}
@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;
}
@Override
public void onClick(View v) {
String string = ((EditText) v.findViewById(R.id.gridSize)).toString();
mListener.onNavFragmentInteraction(string);
}
public interface OnFragmentInteractionListener {
public void onNavFragmentInteraction(String string);
}
}
内容 fragment
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;
import android.widget.GridView;
public class ContentFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public static ContentFragment newInstance(String param1, String param2) {
ContentFragment fragment = new ContentFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public ContentFragment() {
// 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) {
View v = inflater.inflate(R.layout.fragment_content_fragmet, container, false);
String string = "1";
if (getArguments() != null) {
Bundle args = getArguments();
string = (String) args.get("string");
}
GridView gridView = (GridView) v.findViewById(R.id.gridview);
gridView.setNumColumns(Integer.parseInt(string));
gridView.setAdapter(new GridAdapter(this, Integer.parseInt(string)));
return v;
}
public void onButtonPressed(Uri 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;
}
public interface OnFragmentInteractionListener {
public void onContentFragmentInteraction(String string);
}
}
错误
03-02 18:46:47.254 1958-1958/com.peterchappy.lab5 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.peterchappy.lab5, PID: 1958
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.peterchappy.lab5/com.peterchappy.lab5.MainActivity}: java.lang.ClassCastException: com.peterchappy.lab5.MainActivity@2298f3ca must implement OnFragmentInteractionListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassCastException: com.peterchappy.lab5.MainActivity@2298f3ca must implement OnFragmentInteractionListener
at com.peterchappy.lab5.ContentFragmet.onAttach(ContentFragmet.java:83)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:853)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.Activity.performStart(Activity.java:5948)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
最佳答案
您的Fragment
中的OnFragmentInteractionListener
接口(interface)s 包含一个具有相同签名的方法,并且 MainActivity
正在实现一种方法来尝试覆盖这两个接口(interface)。
更改一个或两个方法的签名,然后在 MainActivity
中实现这两个方法。例如:
导航 fragment
public interface OnFragmentInteractionListener {
public void onNavFragmentInteraction(String string);
}
内容 fragment
public interface OnFragmentInteractionListener {
public void onContentFragmentInteraction(String string);
}
MainActivity
@Override
public void onNavFragmentInteraction(String string) {
// Do stuff
}
@Override
public void onContentFragmentInteraction(String string) {
// Do different stuff
}
关于java - 错误 : must implement OnFragmentInteractionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28822229/
尝试通过 fragment 连接从 sqlite 数据库填充的 ListView 。在下面粘贴的代码中,您会看到我的 switch 中有一个 case block ,因为我只是想在我有更多 fragm
这是我需要澄清的帖子(我无法对此帖子发表评论,因为我是新用户,我试着在下面提出一个刚刚被删除的问题): How to implement OnFragmentInteractionListener 在
我已经查看了与此相关的其他问题,但我仍然不确定如何使用我正在做的事情来实现 OnFragmentInteractionListener。我正在登录 facebook,我想在我的主要 Activity
我在 android studio 0.8.2 中有一个带有抽屉导航的向导生成应用程序 我创建了一个 fragment 并使用 newInstance() 添加了它,但出现此错误: com.domai
我已将 fragment 添加到 android 抽屉导航 Activity 中并出现此错误,我正在学习教程并且完全相同的东西在教程中工作,我是否遗漏了什么? java.lang.RuntimeExc
我收到运行时错误:必须实现 OnFragmentInteractionListener 这是我的 fragment 类文件,它呈现两个选项卡,tab1 和 tab2: package com.erce
它抛出一个错误,说我必须实现 OnFragmentInteractionListener,但据我所知,我正在正确实现它......任何帮助将不胜感激。 主要 Activity import a
我收到一条消息,上面写着“必须实现 OnFragmentInteractionListener,而且我已经拥有它... 我已经查看了这里提出的每个问题,但没有人帮助我。 谁能帮帮我? 错误: FATA
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我正在尝试使用 fragments 实现一个抽屉布局。 content_main.xml(应显示 fragment 的位置): MainActivity.java: import android
我正在尝试制作一个使用 Navigation Drawer Activity 的应用程序但是我需要使用 Fragments 当我添加 fragment 时,并试图在 fragment 之间切换它崩溃了
我是 Android 新手,我正在遵循 Facebook SDK 指南 (https://developers.facebook.com/docs/android/login-with-faceboo
我是一名优秀的程序员,十分优秀!