将数据从服务传递到片段,给出零点

时间:2017-02-02 13:05:25

标签: android android-fragments bundle android-broadcastreceiver

我正在尝试使用BroadcastReceiver从服务中获取数据,并且我能够在日志中显示该数据,但是当我尝试使用Bundle这样发送数据时

 @Override
protected void onStart() {
    myReceiver = new MainActivity.MyReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(MediaService.MY_ACTION);
    registerReceiver(myReceiver, intentFilter);
    super.onStart();
}

private class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        String datapa = arg1.getStringExtra("DATAPASSED");
        String datap2 = arg1.getStringExtra("ALBUM_DATA");
        Log.d("TITLE OF SONG",datapa);
        Log.d("ALBUM",datap2);



            Bundle bundle = new Bundle();
            bundle.putString("Title",datapa);
            bundle.putString("album", datap2);
            Songs someFragment = new Songs();
            someFragment.setArguments(bundle);


    }

}

并获取此类数据

   String strtext = getArguments().getString("album");
        if (strtext!=null){
            textView.setText(strtext);
        }else {
            textView.setText(String.valueOf(songarr.size()));
        }
 String title = getArguments().getString("Title");
    if (title!=null){
        textView.setText(title);
    }else {
        textView.setText("Total Songs");
    }

我遇到错误

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                  at beatbox.neelay.anotherbeat.Fragment.Songs.onCreateView(Songs.java:55)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
                  at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                  at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1969)
                  at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:620)
                  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                  at com.antonyt.infiniteviewpager.InfinitePagerAdapter.finishUpdate(InfinitePagerAdapter.java:67)
                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
                  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
                  at android.view.View.measure(View.java:18811)
                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
                  at android.view.View.measure(View.java:18811)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                  at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
                  at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:786)
                  at android.view.View.measure(View.java:18811)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                  at android.view.View.measure(View.java:18811)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                  at android.view.View.measure(View.java:18811)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at android.view.View.measure(View.java:18811)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                  at android.view.View.measure(View.java:18811)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2645)
                  at android.view.View.measure(View.java:18811)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2127)
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1243)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1479)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6045)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:672)
                  at android.view.Choreographer.doFrame(Choreographer.java:608)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Loop

事情是我在播放歌曲,所以当用户点击按钮时,服务会将当前播放的歌曲数据发送到活动,然后从活动发送到片段,当歌曲没有播放时,它只会显示总歌曲,但当我运行应用程序崩溃说空字符串,但我设置else语句他们我做错了什么?任何提示都会有帮助。片段将无法获取数据,直到用户点击主要内容列表。Using the same bundle way i am able to send the data to other fragment这里的初始值为null我是如何解决此问题的。

2 个答案:

答案 0 :(得分:0)

查看你的getArguments是否先不为空

if(getArguments()!= null)  //然后做你正在做的事情,即从捆绑中拉出字符串。

答案 1 :(得分:0)

不是间接传递数据,而是使用直接方式,我在服务中设置如此

 Intent intent = new Intent();
    intent.setAction(MY_ACTION);

    intent.putExtra("DATAPASSED", activeAudio.getTitle());
    intent.putExtra("ALBUM_DATA",activeAudio.getAlbum());
    sendBroadcast(intent);

在我的片段中

 @Override
public void onStart() {
    super.onStart();


    myReceiver = new MyReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(MediaService.MY_ACTION);
    getActivity().registerReceiver(myReceiver, intentFilter);

}

private class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        String datapa = arg1.getStringExtra("DATAPASSED");
        String datap2 = arg1.getStringExtra("ALBUM_DATA");
        Log.d("TITLE OF SONG", datapa);
        Log.d("ALBUM", datap2);
        textView.setText(datapa);
        textView1.setText(datap2);
    }

}

希望这会对某人有所帮助。