使用接口在片段之间传递消息

时间:2015-07-15 20:05:43

标签: java android android-fragments android-activity interface

我目前正在学习使用接口在它们之间传递片段和消息的概念。我创建了一个片段容器活动,它最初包含第一个片段(Home_pageFragment)。它由editText和一个发送按钮组成。单击该按钮时,editText的内容将使用接口传递给第二个片段(Menu_pageFragment),该片段将其显示为文本视图。我的应用程序运行但是当我们点击发送按钮时,应用程序只是被终止,而不是将消息传递给第二个片段。

有谁能告诉我这可能是什么原因?

下面是我的Home_page.java :(片段容器活动)

 --- --- --- ---
| o | o | o | o |
 --- --- --- ---
| o | o | o | o |
 --- --- --- ---
| o | o | o | o |
 --- --- --- ---
| o | o | o | o |
 --- --- --- ---

下面是我的Home_pageFragment.java :(片段1)

    package com.technology.computer.mit.ctechmit;

    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;

    public class Home_page extends ActionBarActivity implements OnButtonPressListener{

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home_page);

            // Create a new Fragment to be placed in the activity layout

            Home_pageFragment firstFragment = new Home_pageFragment();

            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments

            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout

            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();


        }

       @Override
        public void onButtonPressed(String msg) {

           // TODO Auto-generated method stub

               Menu_pageFragment menuFragment = new Menu_pageFragment();
               Bundle args = new Bundle();
               args.putString("message", msg);
               menuFragment.setArguments(args);

               FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

               // Replace whatever is in the fragment_container view with this fragment,
               // and add the transaction to the back stack so the user can navigate back

               transaction.replace(R.id.fragment_container, menuFragment);
               transaction.addToBackStack(null);

               // Commit the transaction

               transaction.commit();
       }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu items for use in the action bar

            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_home_page, menu);
            return super.onCreateOptionsMenu(menu);
        }

        public void openSearch() {

        }

        public void openSettings() {

        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

 // Handle presses on the action bar items

switch (item.getItemId()) {
                case R.id.action_search:
                    openSearch();
                    return true;
                case R.id.action_settings:
                    openSettings();
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }

        }
    }

这是我的Menu_pageFragment.java :(片段2)

package com.technology.computer.mit.ctechmit;


import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.app.Activity;
import android.widget.EditText;
import android.view.View.OnClickListener;

public class Home_pageFragment extends Fragment {


    OnButtonPressListener buttonListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            buttonListener = (OnButtonPressListener) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement onButtonPressListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home_page, null);
        Button button = (Button) view.findViewById(R.id.send_button);
        button.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                EditText editText = (EditText) v.findViewById(R.id.edit_message);
                String message = editText.getText().toString();

                buttonListener.onButtonPressed(message);
            }
        });
        return view;
    }

}

这是我的OnButtonPressListener.java :(界面)

package com.technology.computer.mit.ctechmit;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Menu_pageFragment extends Fragment {

    public Menu_pageFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_menu_page, container, false);
    }

}

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

如果您想将数据从Fragment发送到另一个{/ p>

Menu_pageFragment newFragment = new Menu_pageFragment();
Bundle b=new Bundle();
b.putString(key,value);
newFragment.setArguments(b);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.commit();

并在Menu_pageFragment中,使用

String value=getArguments().getString(key);