Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

android - passing data from service to fragment giving null point

I am trying to get the data from service using BroadcastReceiver and i am able to display that data in the Log but when i try to send the data using the Bundle like this

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


    }

}

and getting the data like this

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

Error i am getting

 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

the thing is i am playing song in service so when the user click the button the service will send the current playing song data to the activity and then from activity to the fragment and when the song is not playing it will just show the total song but when i run the app it crash saying that the null string but i am setting the else statement they what i am doing wrong ?? any hint will be helpfull .the fragment will not get the data untill the user click on the list thats the main thing .Using the same bundle way i am able to send the data to other fragment here the initial value is null how i can fix this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

see if your getArguments is not null first

if(getArguments() !=null ) // then do the stuff you're doing i.e. pull strings from bundle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...