首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自BroadcastReceiver的安卓BroadcastReceiver对话框

来自BroadcastReceiver的安卓BroadcastReceiver对话框
EN

Stack Overflow用户
提问于 2015-12-11 16:03:40
回答 1查看 526关注 0票数 0

我需要在从Bitmap的新窗口中弹出BroadcastReceiver,在该窗口中,位图在从url下载后从另一个服务发送。我正在BroadcastReceiver上收到正确的位图,但是弹出对话框会创建一些异常。

代码语言:javascript
复制
 public class MainActivity extends Activity {

    ImageView popupImage;
    LocalBroadcastManager mLocalBroadcastManager;
    BroadcastReceiver broadcastReceiver;
    Dialog settingsDialog;
    String action = "com.bitmap.receive";

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   popupImage = (ImageView)findViewById(R.layout.image_layout);

   settingsDialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);

   mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
   broadcastReceiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
           if(intent.getAction().equals(action)) {
               //do work here
               Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
               popupImage.setImageBitmap(bitmap);

               settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
               settingsDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
               settingsDialog.setContentView(R.layout.image_layout);
               settingsDialog.show();
           } 
       }
   };



 //starting service
   findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
   Intent intent = new Intent(MainActivity.this, BackGroundService.class);
   startService(intent);
   }
   });

 //service onDestroy callback method will be called
   findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
   Intent intent = new Intent(MainActivity.this, BackGroundService.class);
   stopService(intent);
   }
   });
}

@Override
public void onResume() {
      super.onResume();
      //some other code for alarm service
      IntentFilter filter = new IntentFilter();
      filter.addAction(action);
      mLocalBroadcastManager.registerReceiver(broadcastReceiver, filter);
}

// Method to start the service
public void startService(View view) {
   startService(new Intent(getBaseContext(), BackGroundService.class));
}

// Method to stop the service
public void stopService(View view) {
   stopService(new Intent(getBaseContext(), BackGroundService.class));
}
}

错误日志:

代码语言:javascript
复制
12-11 10:57:56.089: D/gralloc_goldfish(1930): Emulator without GPU emulation detected.
12-11 10:57:58.879: I/HelloService(1930): Service onCreate
12-11 10:57:59.009: I/HelloService(1930): Service onStartCommand
12-11 10:58:00.059: I/HelloService(1930): Service running
12-11 10:58:00.099: I/HelloService(1930): Service onDestroy
12-11 10:58:02.089: D/dalvikvm(1930): GC_FOR_ALLOC freed 207K, 9% free 2911K/3188K, paused 47ms, total 47ms
12-11 10:58:02.089: I/dalvikvm-heap(1930): Grow heap (frag case) to 4.290MB for 1451356-byte allocation
12-11 10:58:02.139: D/dalvikvm(1930): GC_FOR_ALLOC freed 1K, 7% free 4327K/4608K, paused 34ms, total 35ms
12-11 10:58:05.559: I/HelloService(1930): downlad compleated
12-11 10:58:05.559: D/AndroidRuntime(1930): Shutting down VM
12-11 10:58:05.559: W/dalvikvm(1930): threadid=1: thread exiting with uncaught exception (group=0xb4a41ba8)
12-11 10:58:05.599: E/AndroidRuntime(1930): FATAL EXCEPTION: main
12-11 10:58:05.599: E/AndroidRuntime(1930): Process: com.ibeacon.vapplicaspecials, PID: 1930
12-11 10:58:05.599: E/AndroidRuntime(1930): java.lang.NullPointerException
12-11 10:58:05.599: E/AndroidRuntime(1930):     at com.ibeacon.vapplicaspecials.MainActivity$1.onReceive(MainActivity.java:45)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at android.os.Looper.loop(Looper.java:136)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at android.app.ActivityThread.main(ActivityThread.java:5001)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at java.lang.reflect.Method.invoke(Method.java:515)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-11 10:58:05.599: E/AndroidRuntime(1930):     at dalvik.system.NativeStart.main(Native Method)
12-11 11:02:28.179: I/Process(1930): Sending signal. PID: 1930 SIG: 9
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-11 16:29:11

删除紧接在popupImage调用之后的setContentView()初始化。然后按照下面的方式更改onReceive()中的代码,用ImageView的实际ID替换R.id.image_view_id

代码语言:javascript
复制
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
settingsDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
settingsDialog.setContentView(R.layout.image_layout);

popupImage = (ImageView) settingsDialog.findViewById(R.id.image_view_id);
popupImage.setImageBitmap(bitmap);

settingsDialog.show();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34227720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档