我收到此错误:
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)
这是我的片段代码。
onCreateView
方法:
@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;
}
我的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();
}
}
}
}
我认为getActivity.unregisterReceiver(rcv)可能是解决方案
但不知道在哪里写。
在活动中,此代码将写入onPause
。
在哪里为Fragment
编写相同的代码?
谢谢。
答案 0 :(得分:2)
我认为您在尝试getActivity()
的行中尝试了NullPointerException。所以我认为你是以错误的方式注册/取消注册接收器。您应该在onResume
注册并在onPause
取消注册,如下所示:
@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);
}
此外,请不要忘记从getActivity().registerReceiver(rcv, filter);
方法中删除此行onCreateView()
。并且当您收到ACTION_FIN
操作时,根据logcat异常显示,因此我强烈建议您在isAdded()
子句中添加if
条件,以确保该片段附加到活动:
else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN)
&& isAdded()) {
// your code here
}
答案 1 :(得分:0)
在显示对话框之前在onReceive()
中添加行:
@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();
}
}
它将pDialog3
设为NULL并抛出空指针异常
答案 2 :(得分:0)
在BroadcastReceiver
中使用Fragment
的大多数情况下,BroadcastReceiver
在onStart
中注册,在onStop
中未注册。建议BroadcastReceiver
和onStart
注册和取消注册onStop
的位置。因为在Activity
的生命周期内,onStart
之后调用onCreate
而onStop
之前调用onDestroy
,而通常在onCreate
中初始化小部件但onDestroy
不能总是及时执行。 Fragment
的生命周期依赖于Activity
的生命周期。