gpt4 book ai didi

java - 使用 AsyncTask 在 ListView 中显示图像

转载 作者:行者123 更新时间:2023-12-01 21:30:43 26 4
gpt4 key购买 nike

我尝试使用 AsyncTask 在 ListView 中显示图像,但出现错误。说...

Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

这是发生错误的

public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString();
}

    LoadImage loadImage = new LoadImage(imageView);
Bitmap bitmap = loadImage.doInBackground();

这是LoadImage.java

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

/**
* Created by Yudii on 27/05/2016.
*/
class LoadImage extends AsyncTask<Object, Void, Bitmap>{

private ImageView imv;
private String path;

public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString();
}

@Override
protected Bitmap doInBackground(Object... params) {

Bitmap bitmap = null;
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + path);

if (file.exists()) {
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}

return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {


if (!imv.getTag().toString().equals(path)) {
/* The path is not same. This means that this
image view is handled by some other async task.
We don't do anything and return. */
// return;
}




if(result != null && imv != null){
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
}else{
imv.setVisibility(View.GONE);
}
}

}

这是 CustomList.java

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final Integer[] imageId;

public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.imageId = imageId;

}
@Override
public View getView(int position, View view, ViewGroup parent) {

LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);


LoadImage loadImage = new LoadImage(imageView);
Bitmap bitmap = loadImage.doInBackground();


imageView.setImageResource(imageId[position]);

return rowView;

}
}

这是 MainActivity.java

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

ListView list;
String[] web = {
"Google Plus",
"Twitter",
"Tall",
};

Integer[] imageId = {
R.drawable.gggggggggg,
R.drawable.hhhhhhhhhh,
R.drawable.ttttttttttt
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



CustomList adapter = new
CustomList(MainActivity.this, web, imageId);


list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();

}
});

}
}

最佳答案

您需要更改loadImage.execute();而不是Bitmap bitmap = loadImage.doInBackground();

关于java - 使用 AsyncTask 在 ListView 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37479497/

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