无法写入inflate layout

时间:2015-07-05 14:49:14

标签: java android android-fragments broadcastreceiver layout-inflater

我在DrawerActivity中使用广播接收器,我可以从服务中接收通知,然后我会在片段中的布局中设置通知文本(导航抽屉的第一个片段) 。所以我的DrawerActivity是我启动广播接收器的地方(我尝试直接从片段开始它,但它不起作用!)所以这是广播接收器:

 public BroadcastReceiver onNotice= new BroadcastReceiver() {
        LinearLayout notificationLayout;
        Drawable icon;

        @Override
        public void onReceive(Context context, Intent intent) {
            final String pack = intent.getStringExtra("package");
            String title = intent.getStringExtra("title");
            String text = intent.getStringExtra("text");

            View myView = getLayoutInflater().inflate(R.layout.activity_main, null);
            notificationLayout = (LinearLayout)myView.findViewById(R.id.notificationLayout);
            TextView notificationDescription = (TextView) myView.findViewById(R.id.notificationDesc);
            TextView notificationTitle = (TextView) myView.findViewById(R.id.notificationTitle);
            CircularImageView notificationImage = (CircularImageView) myView.findViewById(R.id.img_thumbnail);

            Log.i("Notification ", title);
            Toast.makeText(DrawerActivity.this, title, Toast.LENGTH_SHORT).show();

            if(!pack.equals("") || !title.equals("") || !text.equals("")) {
                notificationLayout.setVisibility(View.VISIBLE);
                notificationTitle.setText("Ciao");
                notificationDescription.setText(text);

                try {
                    icon = DrawerActivity.this.getPackageManager().getApplicationIcon(pack);
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
                notificationImage.setImageDrawable(icon);

                notificationLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        try {
                            PackageManager pmi = DrawerActivity.this.getPackageManager();
                            Intent intent;

                            intent = pmi.getLaunchIntentForPackage(pack);
                            if (intent != null) {
                                DrawerActivity.this.startActivity(intent);
                            }
                            //LocalBroadcastManager.getInstance(MainActivity.instance).sendBroadcast(new Intent("collapseAfterClick"));
                        } catch (Exception ignored) {
                        }
                    }
                });

                notificationLayout.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {

                        notificationLayout.setVisibility(View.INVISIBLE);


                        return true;
                    }
                });
            } else {
                notificationLayout.setVisibility(View.INVISIBLE);
            }

        }
    };
用这种方式很奇怪!!!当通知到达时,Broadcastrecevier内的toast被触发但是inflate布局中的settext没有!所以我可以看到吐司的通知,但不是我的布局!怎么可能?

1 个答案:

答案 0 :(得分:0)

问题是代码没有输入if语句。

        *if(!pack.equals("") || !title.equals("") || !text.equals(""))*