gpt4 book ai didi

android - 在 Android 应用程序上使用 Glide 时崩溃

转载 作者:太空狗 更新时间:2023-10-29 13:11:47 24 4
gpt4 key购买 nike

每次我尝试查看图像时,我的应用程序都会关闭。你能解释一下为什么吗?

我认为问题可能出在字符串 Glide.with(getApplicationContext()) 上,也许它不正确。但是对于 Android Studio 我没有错误:

其他元素都没问题,只是 Glide 有问题

public class MainActivity extends AppCompatActivity {

private final String url= "MY ARRAY";
...

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
new gson().execute(url);
}

public class gson extends AsyncTask<String,String, List<MovieModel> >{

....
public class Adapter extends ArrayAdapter{
private List<MovieModel> movieModelList;
private int resource;
private LayoutInflater inflater;
public Adapter(Context context, int resource, List<MovieModel> objects) {
super(context, resource, objects);
movieModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.fruitsfoto = (ImageView)convertView.findViewById(R.id.fruits);
holder.fruitsname = (TextView)convertView.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ImageView imageView = (ImageView) findViewById(R.id.pics);
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
try {
Glide.with(MainActivity.this)//getApplicationContext()
.load(ModelList.get(position).getImage())
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
holder.fruitsname.setText(ModelList.get(position).getName());
return convertView;
}
}
}

更新:显示图像不遵循数组列表。需要解决方案。

    public Adapter(Context context, int resource, List<MovieModel> objects) {
super(context, resource, objects);
ModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
.....
try {
Glide.with(MainActivity.this)//getApplicationContext()
.load(ModelList.get(position).getFruits())
.into(imageView);
} catch (Exception e) {
e.printStackTrace();
}

编辑:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.fruitsfoto = (ImageView)convertView.findViewById(R.id.fruits);
holder.fruitsname = (TextView)convertView.findViewById(R.id.name);
1)holder.image = (TextView)convertView.findViewById(R.id.pics);
2)ImageView imageView = (ImageView)convertView.findViewById(R.id.pics);
convertView.setTag(holder);
//the first cause a crash
//the second return "Variable 'imageView is never used'"

这是控制台错误:

09-28 17:19:42.079 30111-30111/app E/AndroidRuntime: FATAL EXCEPTION: main
Process: app, PID: 30111
java.lang.NullPointerException
at app$1.onResourceReady(MainActivity.java:309)
at app$1.onResourceReady(MainActivity.java:301)
at com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:522)
at com.bumptech.glide.request.GenericRequest.onResourceReady(GenericRequest.java:507)
at com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java:158)
at com.bumptech.glide.load.engine.EngineJob.access$100(EngineJob.java:22)
at com.bumptech.glide.load.engine.EngineJob$MainThreadCallback.handleMessage(EngineJob.java:202)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

请检查您的应用程序 list 中是否添加了INTERNET权限?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="viewpager.nested.example.com.testapplicationforso">

<uses-permission android:name="android.permission.INTERNET" />

<application
...
</application>

</manifest>

是的,将 Glide 图像加载代码段放在 try/catch 语句中总是好的。

try {
Glide.with(getApplicationContext()).load(yourUrl).into(img);
} catch (Exception e) {
e.printStackTrace();
}

更新:

关于错误放置的图像 - 我可以看到在您的 getView 函数中,您没有正确找到 ImageView。您需要像列表中的其他项目一样在 convertView 中找到 ImageView

holder.image = (TextView)convertView.findViewById(R.id.pics);

// Then load the image in holder.image
Glide.with(MainActivity.this)//getApplicationContext()
.load(ModelList.get(position).getFruits())
.into(holder.image);

关于android - 在 Android 应用程序上使用 Glide 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39739409/

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