【问题标题】:Can't write in inflate layout无法在膨胀布局中写入
【发布时间】:2015-07-05 14:49:14
【问题描述】:

我在我的 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 不会!所以我可以看到来自 Toast 的通知,但在我的布局中看不到!怎么可能?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 是的,但即使我删除该行似乎也不起作用..我该如何解决?
    • 你有什么解决办法吗?
    猜你喜欢
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多