Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的片段中的GridView不可见_Java_Android_Android Studio_Android Fragments - Fatal编程技术网

Java 我的片段中的GridView不可见

Java 我的片段中的GridView不可见,java,android,android-studio,android-fragments,Java,Android,Android Studio,Android Fragments,我正在制作一个包含2个片段的活动,以显示2种不同的功能。我遇到的问题是,在我的第一个片段中有一个不可见的网格视图。应用程序运行时没有崩溃,我只是不知道为什么GridView不可见 以下是讨论中的片段: public class PaletteFragment extends Fragment { GridView grid; TextView label; public PaletteFragment() { } @Override public View onCreateView(La

我正在制作一个包含2个片段的活动,以显示2种不同的功能。我遇到的问题是,在我的第一个片段中有一个不可见的网格视图。应用程序运行时没有崩溃,我只是不知道为什么GridView不可见

以下是讨论中的片段:

public class PaletteFragment extends Fragment {

GridView grid;
TextView label;

public PaletteFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View palette =  inflater.inflate(R.layout.fragment_palette, container, false);

    label = palette.findViewById(R.id.label);
    label.setText(getResources().getString(R.string.labelText));

    grid = palette.findViewById(R.id.gridView);
    grid.setAdapter(new CustomAdapter(palette.getContext()));

    return palette;
}
}

下面是片段的XML:

<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
tools:context=".MainActivity">

<GridView
    android:id="@+id/gridView"
    android:layout_width="0dp"
    android:layout_height="317dp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0"
    tools:visibility="invisible" />

<TextView
    android:id="@+id/label"
    android:layout_width="0dp"
    android:layout_height="119dp"
    android:gravity="center"
    android:text="@string/labelText"
    android:visibility="visible"
    app:layout_constraintBottom_toTopOf="@+id/gridView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

如果有帮助,下面是我在GridView上使用的适配器:

public class CustomAdapter extends BaseAdapter {

Context context;
ArrayList<Integer> colorVal = new ArrayList<Integer>();

public CustomAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return colorVal.size();
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public View getView(int i, View View, ViewGroup viewGroup) {
    String[] colorName = context.getResources().getStringArray(R.array.colorName);

    colorVal.add(Color.RED);
    colorVal.add(Color.BLUE);
    colorVal.add(Color.BLACK);
    colorVal.add(Color.GREEN);
    colorVal.add(Color.WHITE);
    colorVal.add(Color.LTGRAY);
    colorVal.add(Color.DKGRAY);
    colorVal.add(Color.MAGENTA);
    colorVal.add(Color.YELLOW);
    colorVal.add(Color.CYAN);

    TextView text = new TextView(context);
    text.setText(colorName[i]);
    text.setBackgroundColor(colorVal.get(i));
    text.setTextSize(22);
    text.setPadding(20, 20, 20, 20);

    if(text.getText().toString().equals(context.getResources().getString(R.string.black))){
        text.setTextColor(Color.WHITE);
    }

    return text;
}
公共类CustomAdapter扩展了BaseAdapter{
语境;
ArrayList colorVal=新的ArrayList();
公共CustomAdapter(上下文){
this.context=上下文;
}
@凌驾
public int getCount(){
返回colorVal.size();
}
@凌驾
公共长getItemId(int i){
返回0;
}
@凌驾
公共对象getItem(int i){
返回null;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
String[]colorName=context.getResources().getStringArray(R.array.colorName);
colorVal.add(Color.RED);
colorVal.add(Color.BLUE);
colorVal.add(Color.BLACK);
colorVal.add(Color.GREEN);
colorVal.add(Color.WHITE);
colorVal.add(Color.LTGRAY);
colorVal.add(Color.DKGRAY);
添加(颜色为洋红色);
colorVal.add(Color.YELLOW);
colorVal.add(Color.CYAN);
TextView text=新的TextView(上下文);
text.setText(colorName[i]);
setBackgroundColor(colorVal.get(i));
text.setTextSize(22);
设置填充(20,20,20,20);
if(text.getText().toString().equals(context.getResources().getString(R.string.black))){
text.setTextColor(Color.WHITE);
}
返回文本;
}

}调用适配器时,colorVal列表为空,因此getCount始终返回0。这就是为什么你的getView乐趣没有被调用的原因

只需移动代码,将colorVal列表更新到构造函数,就可以开始了

public CustomAdapter(Context context) {
    this.context = context;

    colorVal.add(Color.RED);
    colorVal.add(Color.BLUE);
    colorVal.add(Color.BLACK);
    colorVal.add(Color.GREEN);
    colorVal.add(Color.WHITE);
    colorVal.add(Color.LTGRAY);
    colorVal.add(Color.DKGRAY);
    colorVal.add(Color.MAGENTA);
    colorVal.add(Color.YELLOW);
    colorVal.add(Color.CYAN);
}
您还可以在片段中创建此列表,并将其发送到适配器的构造函数中