有人在说我必须实施OnFragmentInteractionListener,这是一个错误,但是据我所知我正确地实现了它。任何帮助将不胜感激。
主要活动
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){
}
}
导航片段
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);
}
}
内容片段
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)
OnFragmentInteractionListener
两者中的接口都Fragment
包含一个具有相同签名的方法,并且MainActivity
正在实现一种方法来尝试覆盖这两个接口。
更改一个或两个方法的签名,然后在中实现两个方法MainActivity
。例如:
导航片段
public interface OnFragmentInteractionListener {
public void onNavFragmentInteraction(String string);
}
内容片段
public interface OnFragmentInteractionListener {
public void onContentFragmentInteraction(String string);
}
主要活动
@Override
public void onNavFragmentInteraction(String string) {
// Do stuff
}
@Override
public void onContentFragmentInteraction(String string) {
// Do different stuff
}
对于许多应用程序来说,基于输入大小的逻辑拆分是不够的,因为要尊重记录边界。在这种情况下,应用程序还必须实现一个RecordReader,该RecordReader负责尊重记录边界,并向单个任务呈现逻辑输入拆分的面向记录的视图。 WordCount示例应用程序中基于输入大小的逻辑拆分是否不足?如果是,在源代码中的什么地方可以找到RecordReader的实现?
我是Android和Java的初学者。我寻找这个错误的答案,但stackOverflow中的任何东西都不适合我的情况。我在读一本书,书中说我们需要使用一个onListItemClick()方法,该方法来自我们创建的扩展ListFragment的类(CrimeListFragment)(使用该方法和类是必需的)。 然而,我不断发现以下错误: 错误说 “CrimeListFragment类型的onLi
问题内容: 这是我的代码,假设可以在按下按钮时更改一些文本:- 用下划线标记,它给我一个错误“类必须声明为抽象或实现抽象方法”。该代码大部分是从互联网上复制的,并且可以正常工作。可能仅是Android Studio错误。我如何使它工作? 问题答案: 必须实现该函数,否则您的类应该是抽象的,以便可以在某些子类中实现您的函数。但是在您的情况下,您犯了一个拼写错误。应该代替;
我正在使用JUnit 5,并从“软件测试”书中复制了代码,以便创建一个模拟对象进行测试。部分测试器代码是: 我有一个编译器错误,它说“方法isLeap(int)必须覆盖或实现一个超类型方法”。此错误在我覆盖isLeap()方法的行中报告。(@over下面的行) 好吧,令人惊讶的是,这就是我所做的。所以我不知道这是什么抱怨。下面是simpleDate类中的isLeap()方法: 正如您所看到的,测试
我正在为一个学校项目做一个相对简单的应用程序。这是我第一次与android工作室BTW合作。这个应用程序的目的是能够在给定的区域内进行绘制,我从这里复制的绘制部分的代码:https://guides.codepath.com/android/basic-painting-with-views我很确定android studio在我粘贴它的时候把它转换成了kotlin,但我可能错了。 错误出现在我的
问题内容: 我正在尝试在Jenkins / Hudson上配置我的电子邮件,并且不断收到错误消息: 我已经在网上看到了大量有关该错误的信息,但是我没有得到任何帮助。我在Fedora Linux(不是OpenJDK)上使用Sun的JDK。 这是我尝试过的一些方法。我试着从以下这个建议后,但复制从Windows的cacerts到托管詹金斯没有工作,我的Fedora箱。我尝试按照本指南进行操作,因为我试