【问题标题】:failed to put View set to invisible未能将视图设置为不可见
【发布时间】:2018-08-18 13:20:32
【问题描述】:

如果一个孩子为空,我正在尝试将一个视图(在 list_child 中)设置为不可见(在由 external sqlite database 填充并使用 simplecursortreeadapaterexpandable listview 中)..
我试图这样做像这样:
1.初始化viewbinder class中的视图和2.在case.child2中设置条件..但是我得到了view.setvisibility的空指针(当应用程序崩溃时)..我做错了什么?谢谢

 public class MyViewBinder implements SimpleCursorTreeAdapter.ViewBinder {
    View view1=(View)findViewById(R.id.view1);
            @Override
            public boolean setViewValue(View view, final Cursor cursor, int columnIndex) {
                int viewID = view.getId();
                switch (viewID) {
                    case R.id.group1:
                        TextView groupName = (TextView) view;
                        String groupname;
                        groupname = cursor.getString(cursor.getColumnIndex(Database.DATABASE_GROUP_1));
                        groupName.setText(Html.fromHtml(groupname));

                        break;
                case R.id.child1:
                    TextView friendName = (TextView) view;
                    String friend_name;
                    friend_name = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_1));
                    friendName.setText(Html.fromHtml(friend_name));
                    break;

                case R.id.child2:
                    final ImageView url = (ImageView) view;
                    final String urls;
                    urls = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_2));
                    if (urls != null) {
                        Glide.with(AnatomyNostrils.this).load(urls).apply(new RequestOptions().dontTransform().format(DecodeFormat.PREFER_ARGB_8888)).into(url);
                        view.findViewById(R.id.child2).setVisibility(View.VISIBLE);
view1.setVisibility(View.VISIBLE);

                    } else {
                        view.findViewById(R.id.child2).setVisibility(View.GONE);
view1.setVisibility(View.GONE);
                    }
                    url.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            AlertDialog.Builder mBuilder = new AlertDialog.Builder(AnatomyNostrils.this);
                            View mView = getLayoutInflater().inflate(R.layout.dialog_custom_layout, null);
                            PhotoView photoView = mView.findViewById(R.id.imageView);
                            photoView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                            photoView.setImageDrawable(((ImageView) view).getDrawable());
                            Glide.with(AnatomyNostrils.this).load(urls).apply(new RequestOptions().dontTransform().format(DecodeFormat.PREFER_ARGB_8888)).into(photoView);
                            mBuilder.setView(mView);
                            AlertDialog mDialog = mBuilder.create();
                            mDialog.show();
                        }
                    });
                    break;

list_child.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="12dp">

    <TextView
        android:id="@+id/child1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/textsizechildexpandablelistview"
        android:textIsSelectable="true"
        android:paddingBottom="5dp"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingTop="2dp"
        android:textColor="@android:color/black"

        />

    <LinearLayout
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/child1"
        android:visibility="visible"
        android:orientation="horizontal">


        <ImageView
            android:layout_weight="1"
            android:id="@+id/child3"
            android:layout_width="match_parent"
            android:layout_height="@dimen/heightimageviewchildexpandablelistview"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp" />

        <View
            android:layout_width="1dp"
            android:id="@+id/view2"
            android:layout_height="match_parent"
            android:layout_margin="6dp"
            android:background="#cc0033" />

        <ImageView
            android:layout_weight="1"
            android:id="@+id/child2"
            android:layout_width="match_parent"
            android:layout_height="@dimen/heightimageviewchildexpandablelistview"
            android:layout_marginBottom="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp" />

        <View
            android:layout_width="1dp"
            android:id="@+id/view1"
            android:layout_height="match_parent"
            android:layout_margin="6dp"
            android:background="#cc0033" />

    </LinearLayout>

来自模拟器的图片:https://i.imgur.com/cj9dWSX.png

【问题讨论】:

  • 没有人知道答案

标签: android simplecursortreeadapter


【解决方案1】:

如果您在case R.id.child2: 中的代码被执行,那么view 就是您要显示/隐藏的视图,因此请替换:

view.findViewById(R.id.child2).setVisibility(...);

与:

view.setVisibility(...);

【讨论】:

  • 感谢@mTak 花时间回答我的问题..只是为了解释一下:我试图隐藏一行(在 xml 文件中表示为简单视图),恰好是右边的行(请参阅帖子末尾的模拟器照片)..当数据库 sqlite 案例中没有字符串时,或者换句话说 case=null (Database.DATABASE_CHILD_2=null) 我想要 id=child2 的图像视图并且第二个视图(id=view2 的右侧)设置为不可见..请查看模拟器中的照片以了解。
  • PS:代码在没有这些行的情况下工作得很好:view1.setVisibility(View.VISIBLE); ..我添加了视图(= 两行)来设置子视图的样式 ..但是当有两张图片时,我只希望出现一行
猜你喜欢
  • 2013-02-21
  • 1970-01-01
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 2022-06-13
相关资源
最近更新 更多