aldakur
aldakur

Reputation: 419

"Error receiving broadcast Intent" in Fragment

I receive this error:

04-17 09:35:10.227: E/AndroidRuntime(9377): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.aldakur.instalacionesdep.services.action.FIN flg=0x10 } in com.aldakur.instalacionesdep.info.RssAvisosFragment$ProgressReceiver@42721b68
04-17 09:35:10.227: E/AndroidRuntime(9377):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:776)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at android.os.Handler.handleCallback(Handler.java:733)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at android.os.Looper.loop(Looper.java:136)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at android.app.ActivityThread.main(ActivityThread.java:5146)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at java.lang.reflect.Method.invokeNative(Native Method)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at java.lang.reflect.Method.invoke(Method.java:515)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at dalvik.system.NativeStart.main(Native Method)
04-17 09:35:10.227: E/AndroidRuntime(9377): Caused by: java.lang.NullPointerException
04-17 09:35:10.227: E/AndroidRuntime(9377):     at com.aldakur.instalacionesdep.info.RssAvisosFragment$ProgressReceiver.onReceive(RssAvisosFragment.java:123)
04-17 09:35:10.227: E/AndroidRuntime(9377):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:766)

This is my Fragment code.

onCreateView method:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.rss_avisos, container, false);

    ListView lv = (ListView)view.findViewById(R.id.rss_avisos_lv);
    lv.setOnItemClickListener(this);

    if(isMyServiceRunning()){

        pDialog3 = new ProgressDialog(getActivity());
        pDialog3.setMessage("3.Cargando Noticias...");
        pDialog3.setCancelable(false);
        pDialog3.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog3.show();


        }

        IntentFilter filter = new IntentFilter();
        filter.addAction(CheckNuevosAvisosIntentService.ACTION_PROGRESO);
        filter.addAction(CheckNuevosAvisosIntentService.ACTION_FIN);
        getActivity().registerReceiver(rcv, filter);
        AvisosEnListaAdapter adapter = new AvisosEnListaAdapter(getActivity(), avisosList);
        lv.setAdapter(adapter);
    return view;
}

And my BroadCastReceiver:

    public class ProgressReceiver extends BroadcastReceiver {

    //ProgressDialog pDialog2 = new ProgressDialog(RssAvisosActivity.this);

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_PROGRESO)) {
            int prog = intent.getIntExtra("progreso", 0);

            if(!pDialog3.isShowing()){
            pDialog3.setMessage("2.Cargando Noticias...");
            pDialog3.setCancelable(false);//erabiltzaileak atzera botoia sakatuz ez kantzelatzeko
            pDialog3.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog3.show();

            }

        }
        else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN)) {

            //Select
            DataBaseHelper myDB = new DataBaseHelper(getActivity().getBaseContext());
            myDB.openDataBase();
            avisosList = myDB.getAllAvisos("eu");
            AvisosEnListaAdapter adapter = new AvisosEnListaAdapter(getActivity(), avisosList);
            lv.setAdapter(adapter);

            if(pDialog3.isShowing()){
                pDialog3.dismiss();
        }
        }
    }

}

I think getActivity.unregisterReceiver (rcv) may be the solution But do not know where to write. In an activity, this code is written to the onPause.

Where to write same code for Fragment ?

Thank You.

Upvotes: 1

Views: 1898

Answers (3)

romtsn
romtsn

Reputation: 12002

I think you have a NullPointerException attempt at the line where you trying to getActivity(). So I think you are registering/unregistering receiver in the wrong way. You should register it at onResume and unregister at onPause, like this:

@Override
public void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction(CheckNuevosAvisosIntentService.ACTION_PROGRESO);
    filter.addAction(CheckNuevosAvisosIntentService.ACTION_FIN);
    getActivity().registerReceiver(rcv, filter);
}

@Override
public void onPause() {
    super.onPause();
    getActivity().unregisterReceiver(rcv);
}

Also, don't forget to remove this line getActivity().registerReceiver(rcv, filter); from onCreateView() method. And according to logcat exception appears when you are receiving ACTION_FIN action, so I highly recommend you to add isAdded() condition to your if clause to ensure that fragment is attached to activity:

    else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN) 
        && isAdded()) {
        // your code here
    }

Upvotes: 6

SilentKnight
SilentKnight

Reputation: 14021

In most cases of using BroadcastReceiver in a Fragment, BroadcastReceiver is registered in onStart and unregistered in onStop. Where registering and unregistering a BroadcastReceiver is recommended of onStart and onStop. Because in the life span of a Activity, onStart is called after onCreate and onStop before onDestroy while usually widgets is initialized in onCreate but onDestroy can't be always executed in time. And the life span of a Fragment relies on that of a Activity.

Upvotes: 1

Kushal
Kushal

Reputation: 8508

Add line in onReceive() before you display dialog :

  @Override
        public void onReceive(Context context, Intent intent) {               
        if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_PROGRESO)) {
                int prog = intent.getIntExtra("progreso", 0);
                pDialog3 = new ProgressDialog(getActivity());    // this line will prevent NPE
                if(!pDialog3.isShowing()){
                    pDialog3.setMessage("2.Cargando Noticias...");
                    pDialog3.setCancelable(false);//erabiltzaileak atzera botoia sakatuz ez kantzelatzeko
                    pDialog3.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    pDialog3.show();
                }
           }

It was getting pDialog3 as NULL and throwing Null Pointer Exception

Upvotes: 0

Related Questions