gpt4 book ai didi

java - Android: fragment 交互监听器出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:50:25 24 4
gpt4 key购买 nike

我一直在寻找 stackoverflow 来解决我的问题并找到了一些很好的东西,但它对我不起作用或者我很愚蠢地看到它 ( How to implement OnFragmentInteractionListener )

我想在框架内放置一个 fragment ,所以我创建了它等等。我必须实现一个接口(interface)及其方法。我(认为)我做到了,但我的应用程序每次都崩溃...

*编辑:我的应用程序崩溃,当我调用 openHome 或 openRecommended 时,onCreate 方法有效,我收到这些错误,但一切都正确显示。

这是我的代码:

主 Activity .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)
{

}
}

首页 fragment .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: Unable to start activity ComponentInfo{com.domain.app/com.domain.app.MainActivity}: java.lang.ClassCastException: com.domain.app.MainActivity@b1d296b0 must implement OnFragmentInteractionListener at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: com.domain.app.MainActivity@b1d296b0 must implement OnFragmentInteractionListener at com.domain.app.HomeFragment.onAttach(HomeFragment.java:74) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) at android.app.BackStackRecord.run(BackStackRecord.java:684) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) at android.app.Activity.performStart(Activity.java:5240) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)             at android.app.ActivityThread.access$800(ActivityThread.java:135)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5017)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)             at dalvik.system.NativeStart.main(Native Method)

我真的需要帮助,我被困了几个小时......

约翰

最佳答案

我删除了无用的引用以及与 RecommendedFragment 类相关的所有内容。如果您发布它,我会更新我的答案。

我已经测试过它并且有效。但是请尝试解释 openHome() 方法的含义,因为根据您的代码从未使用过它,但是您说调用它时应用程序崩溃了。

我将此方法添加到界面中,因为在我看来这是最合乎逻辑的事情。

//主 Activity

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();
}

}

//首页 fragment

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);
}

}

关于java - Android: fragment 交互监听器出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25753953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com