gpt4 book ai didi

java - 应该将什么放入该 fragment View 模型中?

转载 作者:行者123 更新时间:2023-12-02 09:09:00 25 4
gpt4 key购买 nike

我想问一下关于fragment和viewmodel的问题。我正在制作一个 gridView ,可以通过改造(抽屉导航)显示图像。使用mainActivity时,没有问题,但是使用fragment时,出现如下错误。

public class InfografisFragment extends Fragment {

private InfografisViewModel infografisViewModel;
private List<Spacecraft> spacecraftList;

private GridViewAdapter adapter;
private GridView gridView;

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infografisViewModel = ViewModelProviders.of(this).get(InfografisViewModel.class);
View root = inflater.inflate(R.layout.fragment_infografis, container, false);

return root;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

MyAPIService myAPIService = RetrofitClientInstance.getRetrofitInstance().create(MyAPIService.class);
gridView = getView().findViewById(R.id.grid_view);

Call<List<Spacecraft>> call = myAPIService.getSpacecrafts();
call.enqueue(new Callback<List<Spacecraft>>() {

@Override
public void onResponse(Call<List<Spacecraft>> call, Response<List<Spacecraft>> response) {
spacecraftList = response.body();
adapter = new GridViewAdapter(getContext(), spacecraftList);
gridView.setAdapter(adapter);
}
@Override
public void onFailure(Call<List<Spacecraft>> call, Throwable throwable) {
Toast.makeText(getContext(), throwable.getMessage(), Toast.LENGTH_LONG).show();
}
});
}

class Spacecraft {
/*
INSTANCE FIELDS
*/
@SerializedName("gambar_info")
private String gambar_info;

public Spacecraft(int id_info, String gambar_info) {
this.gambar_info = gambar_info;
}

/*
*GETTERS AND SETTERS
*/

public String getImageURL() {
return gambar_info;
}
}

interface MyAPIService {

@GET("connection_image_bps.php")
Call<List<Spacecraft>> getSpacecrafts();
}

static class RetrofitClientInstance {

private static Retrofit retrofit;
private static final String BASE_URL = "https://bpskotayogyakarta.000webhostapp.com/";

public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}

class GridViewAdapter extends BaseAdapter {

private List<Spacecraft> spacecrafts;
private Context context;

public GridViewAdapter(Context context,List<Spacecraft> spacecrafts){
this.context = context;
this.spacecrafts = spacecrafts;
}

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

@Override
public Object getItem(int pos) {
return spacecrafts.get(pos);
}

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

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if(view==null)
{
view=LayoutInflater.from(context).inflate(R.layout.list_gambar_info,viewGroup,false);
}

ImageView spacecraftImageView = view.findViewById(R.id.image_view);

final Spacecraft thisSpacecraft= spacecrafts.get(position);

if(thisSpacecraft.getImageURL() != null && thisSpacecraft.getImageURL().length()>0)
{
Picasso.get().load(thisSpacecraft.getImageURL()).placeholder(R.drawable.placeholder).into(spacecraftImageView);
}else {
Toast.makeText(context, "Empty Image URL", Toast.LENGTH_LONG).show();
Picasso.get().load(R.drawable.placeholder).into(spacecraftImageView);
}

return view;
}
}

当我尝试运行并打开该 fragment 时,会导致崩溃和错误

2020-01-02 15:26:12.401 2455-2455/com.example.bpskotayogyakarta A/zygote: thread.cc:2090] No pending exception expected: java.lang.ArrayIndexOutOfBoundsException: length=6; index=6
2020-01-02 15:26:12.491 2455-2455/com.example.bpskotayogyakarta A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 2455 (skotayogyakarta), pid 2455 (skotayogyakarta)

其余的调试 https://justpaste.it/2rv1v

以及 API PHP 文件 https://justpaste.it/4pdp8

我试了一遍又一遍,在谷歌上搜索,但我还是不明白。我该怎么办?

最佳答案

我通过更改相对于cardview的布局并在build.gradle中添加compatibilityOption找到了解决方案。

compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

关于java - 应该将什么放入该 fragment View 模型中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59560706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com