本文介绍了安卓:错误与片段的互动监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找计算器来解决我的问题,并发现了一些pretty的好,但它不为我工作,或者我是愚蠢的,看看它(How实施OnFragmentInteractionListener )

I have been looking at stackoverflow to solve my problem and found something pretty good, but it does not work for me or I am to stupid to see it (How to implement OnFragmentInteractionListener)

我要放置一个片段的框架里面,所以我创造了它,并依此类推。我要implenet接口及其方法。我(认为)我做到了,但我的应用程序崩溃每次...

I want to place a fragment inside a Frame, so I created it and so on. I have to implenet an interface and its method. I (think) I did it, but my App crashes everytime...

*编辑:我的应用程序崩溃,当我打电话openHome或openRecommended,在onCreate方法的工作原理,我得到这些错误,但一切都beeing显示正确

* My App crashes, when I call openHome or openRecommended, the onCreate method works, I get these errors, but everything is beeing displayed correctly.

这是我的code:

MainActivity.java

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

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看起来是一样的,但有些方法有不同的名称。

RecommendedFragment looks the same, except some methods have a different name.

我总是得到这个错误:

了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)              在java.lang.reflect.Method.invokeNative(本机方法)              在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(本机方法)       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)  在java.lang.reflect.Method.invokeNative(本机方法)  在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(本机方法)

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

I really need help here, I am stuck for hours...

约翰·

推荐答案

我已经删除无用的参考和一切与RecommendedFragment类相关。如果您发布它,我会更新我的答案。

I’ve removed useless references and everything related with RecommendedFragment class. If you post it I will update my answer.

我已经测试了它和它的作品。但是,试图解释什么是openHome的含义()方法,因为它从来没有使用过基于你的code,但和你说的那个应用程序崩溃,当你调用它。

I’ve tested it and it works. But try to explain what is the meaning of openHome() method, because it is never used based on your code, and nevertheless you say that the app crashes when you call it.

我添加了这个方法的接口,因为在我看来是最合乎逻辑的事情。

I added this method to the interface because it seems to me the most logical thing to do.

// MainActivity

 //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

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

}

这篇关于安卓:错误与片段的互动监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:30