我一直在寻找stackoverflow来解决我的问题并找到了一些相当不错的东西,但它对我不起作用,或者我很愚蠢地看到它(How to implement OnFragmentInteractionListener)
我想在Frame中放置一个片段,所以我创建了它,依此类推。我必须实现一个接口及其方法。我(想)我做到了,但我的应用程序每次都崩溃了......
*编辑:我的应用程序崩溃,当我调用openHome或openRecommended时,onCreate方法有效,我收到这些错误,但所有内容都正确显示。
这是我的代码:
MainActivity.java
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener, RecommendedFragment.OnFragmentInteractionListener
{
FragmentTransaction fragmentTransaction;
HomeFragment homeFragment;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homeFragment = new HomeFragment();
getFragmentManager().beginTransaction().add(R.id.mainFrame, homeFragment).commit();
}
protected void openHome(View view)
{
/*Intent homeIntent = new Intent(this, HomeFragment.class);
startActivity(homeIntent);
homeFragment = new HomeFragment();
getFragmentManager().beginTransaction().replace(R.id.mainFrame, homeFragment).commit();*/
System.out.println("Success");
}
public void openRecommended(View view)
{
Intent recommendedIntent = new Intent(this, RecommendedFragment.class);
startActivity(recommendedIntent);
RecommendedFragment recommendedFragment = new RecommendedFragment();
getFragmentManager().beginTransaction().replace(R.id.mainFrame, recommendedFragment).commit();
}
@Override
public void onFragmentInteractionHome(Uri uri)
{
}
@Override
public void onFragmentInteractionRecommended(Uri uri)
{
}
}
HomeFragment.java
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;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link HomeFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment
{
private OnFragmentInteractionListener mListener;
public static HomeFragment newInstance()
{
HomeFragment fragment = new HomeFragment();
return fragment;
}
public HomeFragment()
{
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri)
{
if (mListener != null)
{
mListener.onFragmentInteractionHome(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 onFragmentInteractionHome(Uri uri);
}
}
RecommendedFragment看起来一样,但有些方法名称不同。
我总是收到这个错误:
java.lang.RuntimeException:无法启动活动 ComponentInfo {} com.domain.app/com.domain.app.MainActivity: java.lang.ClassCastException:com.domain.app.MainActivity@b1d296b0 必须实现OnFragmentInteractionListener 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.ClassCastException:com.domain.app.MainActivity@b1d296b0必须实现 OnFragmentInteractionListener 在com.domain.app.HomeFragment.onAttach(HomeFragment.java:74) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 在android.app.BackStackRecord.run(BackStackRecord.java:684) 在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 在android.app.Activity.performStart(Activity.java:5240) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在dalvik.system.NativeStart.main(本地方法)
我真的需要帮助,我被困了几个小时......
约翰
答案 0 :(得分:7)
我删除了无用的引用以及与RecommendedFragment类相关的所有内容。如果你发布它我会更新我的答案。
我测试了它并且它有效。但是试着解释一下openHome()方法的含义是什么,因为它永远不会根据你的代码使用,但是你说当你调用它时应用程序会崩溃。
我在界面中添加了这个方法,因为在我看来这是最合乎逻辑的事情。
// MainActivity
public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.add(R.id.mainFrame, new HomeFragment())
.commit();
}
@Override
public void openHome(View view) {
System.out.println("Success");
}
@Override
public void onFragmentInteractionHome(Uri uri) {
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
}
}
// HomeFragment
public class HomeFragment extends Fragment {
private OnFragmentInteractionListener mListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteractionHome(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
mListener.onFragmentInteractionHome(Uri.parse("doWhatYouWant"));
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void onFragmentInteractionHome(Uri uri);
public void openHome(View view);
}
}
答案 1 :(得分:0)
只需从片段中删除OnAttach()方法