"Error receiving broadcast Intent" 在片段中
"Error receiving broadcast Intent" in Fragment
我收到此错误:
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) 可能是解决方案
但是不知道写在哪里。
在activity中,这段代码被写入了onPause
。
在哪里为 Fragment
编写相同的代码?
谢谢。
在显示对话框之前在 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 并抛出空指针异常
在大多数情况下,在 Fragment
中使用 BroadcastReceiver
,BroadcastReceiver
在 onStart
中注册,在 onStop
中取消注册。 onStart
和 onStop
推荐注册和注销 BroadcastReceiver
。因为在 Activity
的生命周期中, onStart
在 onCreate
之后调用, onStop
在 onDestroy
之前调用,而通常 widgets 在 onCreate
中初始化但是onDestroy
不能总是及时执行。而Fragment
的寿命依赖于Activity
的寿命。
我认为您在尝试 getActivity()
的那一行尝试了 NullPointerException。所以我认为你是 registering/unregistering 错误的接收者。您应该在 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);
}
此外,不要忘记从 onCreateView()
方法中删除这一行 getActivity().registerReceiver(rcv, filter);
。根据 logcat 当您收到 ACTION_FIN
操作时会出现异常,因此我强烈建议您将 isAdded()
条件添加到您的 if
子句以确保片段附加到 activity:
else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN)
&& isAdded()) {
// your code here
}
我收到此错误:
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) 可能是解决方案
但是不知道写在哪里。
在activity中,这段代码被写入了onPause
。
在哪里为 Fragment
编写相同的代码?
谢谢。
在显示对话框之前在 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 并抛出空指针异常
在大多数情况下,在 Fragment
中使用 BroadcastReceiver
,BroadcastReceiver
在 onStart
中注册,在 onStop
中取消注册。 onStart
和 onStop
推荐注册和注销 BroadcastReceiver
。因为在 Activity
的生命周期中, onStart
在 onCreate
之后调用, onStop
在 onDestroy
之前调用,而通常 widgets 在 onCreate
中初始化但是onDestroy
不能总是及时执行。而Fragment
的寿命依赖于Activity
的寿命。
我认为您在尝试 getActivity()
的那一行尝试了 NullPointerException。所以我认为你是 registering/unregistering 错误的接收者。您应该在 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);
}
此外,不要忘记从 onCreateView()
方法中删除这一行 getActivity().registerReceiver(rcv, filter);
。根据 logcat 当您收到 ACTION_FIN
操作时会出现异常,因此我强烈建议您将 isAdded()
条件添加到您的 if
子句以确保片段附加到 activity:
else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN)
&& isAdded()) {
// your code here
}