gpt4 book ai didi

java - Glide 在 Android Studio 中不起作用

转载 作者:行者123 更新时间:2023-12-01 09:28:26 27 4
gpt4 key购买 nike

应用程序使用 J son 从服务器读取数据,并显示卡片 View 中的数据,一切工作正常。但我想添加图像并从服务器读取图像到卡片 View 中。使用 Glide 库来执行此操作,但是当添加此库时,我的应用程序将强制停止。这是我的代码:

主要 Activity :

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private ArrayList<AndroidVersion> data;
private DataAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
initViews();
}
private void initViews(){
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
loadJSON();
}
private void loadJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://memaraneha.ir/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

JSONResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
}

@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}
}

JSON 响应:

public class JSONResponse {
private AndroidVersion[] android;

public AndroidVersion[] getAndroid() {
return android;
}
}

数据适配器:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private Context context;
private ArrayList<AndroidVersion> android;


public DataAdapter(ArrayList<AndroidVersion> android) {
this.android = android;
}


@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {

viewHolder.tv_name.setText(android.get(i).getName());
viewHolder.tv_version.setText(android.get(i).getVer());
viewHolder.tv_api_level.setText(android.get(i).getApi());


// load image into imageview using glide
Glide.with(context).load("http://memaraneha.ir/Erfan/images/"+android.get(i).getPic())
.placeholder(R.drawable.truiton)
.error(R.drawable.truiton)
.into(viewHolder.tv_image);
}

@Override
public int getItemCount() {
return android.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name,tv_version,tv_api_level;
public ImageView tv_image;
public ViewHolder(View view) {
super(view);

tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
tv_image= (ImageView) view.findViewById(R.id.img);

}
}

}

Android版本:

public class AndroidVersion {
private String ver;
private String name;
private String api;
private String pic;

public String getVer() {
return ver;
}

public String getName() {
return name;
}

public String getApi() {
return api;
}
public String getPic(){
return pic;
}
}

请求接口(interface):

public interface RequestInterface {

@GET("Erfan/ret.php")
Call<JSONResponse> getJSON();
}

card_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tv_name"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_api_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
</android.support.v7.widget.CardView>

</LinearLayout>

main_page.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/card_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>

这是我的 json :

{ "android": 
[
{ "ver": "1.5", "name": "Cupcake", "api": "API level 3","pic":"pic1.jpg" },
{ "ver": "1.6", "name": "Donut", "api": "API level 4","pic":"pic2.jpg" },
{ "ver": "2.0 - 2.1", "name": "Eclair", "api": "API level 5 - 7","pic":"pic3.jpg" },
{ "ver": "2.2", "name": "Froyo", "api": "API level 8","pic":"pic4.jpg" },
{ "ver": "2.3", "name": "Gingerbread", "api": "API level 9 - 10","pic":"pic5.jpg" },
{ "ver": "3.0 - 3.2", "name": "Honeycomb", "api": "API level 11 - 13","pic":"pic6.jpg" },
{ "ver": "4.0", "name": "Ice Cream Sandwich", "api": "API level 14 - 15","pic":"pic7.jpg" },
{ "ver": "4.1 - 4.3", "name": "JellyBean", "api": "API level 16 - 18","pic":"pic8.jpg" },
{ "ver": "4.4", "name": "KitKat", "api": "API level 19","pic":"pic9.jpg" },
{ "ver": "5.0 - 5.1", "name": "Lollipop", "api": "API level 21 - 22","pic":"pic10.jpg" },
{ "ver": "6.0", "name": "Marshmallow", "api": "API level 23","pic":"pic11.jpg" }
]
}

这是我的异常(exception):

致命异常:main java.lang.IllegalArgumentException:无法在空上下文上启动加载 com.bumptech.glide.manager.RequestManagerRetriever.get(Reque‌​stManagerRetriever.j‌​ava:83) com.bumptech.glide.Glide .with(Glide.java:609) com.exampleret.DataAdapter.onBindViewHolder(DataAdapter.java‌​:44) com.example.ret.DataAdapter.onBindViewHolder(DataAdapter.jav‌​a:19)

最佳答案

您可以将适配器构造函数更改为以下形式:

public DataAdapter(Context context, ArrayList<AndroidVersion> android) {
this.context = context;
this.android = android;
}

然后像这样初始化它:

adapter = new DataAdapter(MainActivity.this, data);

关于java - Glide 在 Android Studio 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655485/

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