将数据从片段传递到viewpager

时间:2015-02-06 05:10:22

标签: android android-intent android-fragments

我有Fragment并希望将数据发送到Viewpager。我尝试使用Bundle,但不能工作。

NewRadioFragment - pass data - > ViewPagerActivity(片段PlayRadio和片段BlogContent)

NewRadioFragment有一些数据,我想发送到PlayRadio,从Playradio发送继续到BlogContent。

如果你知道解决方案。请帮助我 ! 谢谢大家。

我的代码在这里:

公共类NewRadioFragment扩展了Fragment

 btnShowMore.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (item.getLink_play() != null) {
                    Toast.makeText(mContext, item.getLink_play(),
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(mContext,
                            ViewPagerActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.STRING_KEY, Desciption);  
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(mContext, "NULL", Toast.LENGTH_SHORT).show();
                }
            }
        });

ViewPagerActivity扩展了FragmentActivity

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_main_activity);
        setUpView();
    }

公共课PlayBlog扩展片段

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

        View rootView = inflater.inflate(R.layout.playradio_layout, container,
                false);
        mContext = container.getContext();
        final Animation anim = AnimationUtils.loadAnimation(mContext,
                R.anim.rotate);
        Desciption = getArguments().getString(Constant.STRING_KEY);
        Log.i("", Constant.TAG + Desciption);
        ImageView splash = (ImageView) rootView.findViewById(R.id.base);
        TextView tvDesciption = (TextView) rootView
                .findViewById(R.id.tv_Marquee);
         tvDesciption.setText(Desciption);
        splash.startAnimation(anim);
        return rootView;
    }

更新代码

我编辑了我的代码,但是NullPoiterException 公共类ViewPagerActivity扩展了FragmentActivity

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_main_activity);
        Intent intent = getIntent();
        Bundle args = intent.getExtras();
        Fragment playRadio = new PlayBlog();
        playRadio.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.pager, playRadio);
        transaction.commit();
        setUpView();
    } 

公共课PlayBlog扩展片段

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

        View rootView = inflater.inflate(R.layout.playradio_layout, container,
                false);
        mContext = container.getContext();
        final Animation anim = AnimationUtils.loadAnimation(mContext,
                R.anim.rotate);
        Desciption = getArguments().getString(Constant.STRING_KEY);
        Log.i("", Constant.TAG + Desciption);
        ImageView splash = (ImageView) rootView.findViewById(R.id.base);
        TextView tvDesciption = (TextView) rootView
                .findViewById(R.id.tv_Marquee);
         tvDesciption.setText(Desciption);
        splash.startAnimation(anim);
        return rootView;
    }

更新2

Logcat错误

02-06 06:14:39.980: E/AndroidRuntime(1790): FATAL EXCEPTION: main
02-06 06:14:39.980: E/AndroidRuntime(1790): java.lang.NullPointerException
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.tadev.blogradio.main.PlayBlog.onCreateView(PlayBlog.java:36)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.View.measure(View.java:15518)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.View.measure(View.java:15518)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.View.measure(View.java:15518)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.View.measure(View.java:15518)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.View.measure(View.java:15518)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.Choreographer.doFrame(Choreographer.java:532)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.os.Handler.handleCallback(Handler.java:725)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.os.Looper.loop(Looper.java:137)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at java.lang.reflect.Method.invoke(Method.java:511)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-06 06:14:39.980: E/AndroidRuntime(1790):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

要将数据从NewRadioFragment传递到PlayBlog,您首先需要使用ViewPagerActivity将捆绑包发送到putExtras,然后针对setArguments方法调用PlayBlog您在add or replcase活动中调用PlayBlog片段的ViewPagerActivity方法。

btnShowMore按钮上单击发送意图中的Bundle:

Intent intent = new Intent(mContext,
                            ViewPagerActivity.class);
Bundle bundle = new Bundle();
bundle.putString(Constant.STRING_KEY, Desciption);  
intent.putExtras(bundle);
startActivity(intent);

ViewPagerActivity使用getIntent获取Bundle并调用setArguments方法将包发送到片段:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_main_activity);
    Intent intent=getIntent();
    Bundle bundle = intent.getExtras();

现在将bundle捆绑包传递给PlayBlog碎片

答案 1 :(得分:1)

您必须使用interface将数据发送到其容器

此处(最后回复): Passing data between a fragment and its container activity

编辑:我需要更多代码来向您发送好的示例,并了解您的片段启动寻呼机的原因

NewRadioFragment中的

声明了一个像这样的接口

// Container Activity must implement this interface
public interface Onpassdata // name not fixed 
{
    public void start(String T, int ID);
}

声明一个成员:private Onpassdata mCallback;

@Override
public void onAttach(Activity activity) 
{
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (Onpassdata) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement Onpassdata");
    }
}

btnShowMore.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (item.getLink_play() != null) {
                Toast.makeText(mContext, item.getLink_play(),
                        Toast.LENGTH_SHORT).show();
               start(Desciption, id);  
            } else {
                Toast.makeText(mContext, "NULL", Toast.LENGTH_SHORT).show();
            }
        }
    });

in pour ViewPagerActivity

public ViewPagerActivity extends FragmentActivity
     implements NewRadioFragment.Onpassdata
{

public void start(String T, int ID) {
   // do something whith values
   // and you can Start your BlogContent fragment here

}