【发布时间】:2015-11-17 19:45:43
【问题描述】:
我正在开发一个 Android 应用程序,用户在其中上传照片。现在,我正在从服务器调用一个方法,它将返回一个用户列表。我想将此列表与照片一起显示。 存储在服务器中的图像是字节数组。所以我将它们转换为字符串,然后转换为位图进行显示。不幸的是,它不起作用,因为我也在使用适配器做事。在适配器类中检索到的图像为空。我做错了什么?
其他用户.java:
public class OtherUsers extends Activity {
private PersonServiceImpl personService = new PersonServiceImpl();
private static volatile List<RestPerson> restPersonList = new ArrayList<>();
public static final String firstName = "firstname";
public static final String userImage = "userimage";
static final Long userId = (long)0;
ListView listView;
UserAdapter userAdapter;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.displayuserlist);
restPersonList = this.personService.getOtherUsers();
// Base64 base64Encoder = new Base64();
ArrayList<HashMap<String, String>> usersArrayHashList = new ArrayList<>();
for(RestPerson restPerson : restPersonList){
HashMap<String, String> restDisplay = new HashMap<>();
restDisplay.put("userId",String.valueOf(restPerson.getUserId()));
restDisplay.put("firstName",restPerson.getFirstName());
restDisplay.put("userImage","data:image/png;base64," + Base64.encode(restPerson.getProfilePhoto()));
usersArrayHashList.add(restDisplay);
}
listView = (ListView) findViewById(R.id.usersdisplaylist);
userAdapter = new UserAdapter(this,usersArrayHashList);
listView.setAdapter(userAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Long userid = restPersonList.get(position).getUserId();
Log.d("Userid is ", String.valueOf(userid));
}
});
UserAdapter 类:
public class UserAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public UserAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.userprofiles, null);
TextView username = (TextView) view.findViewById(R.id.userName);
ImageView userImage = (ImageView) view.findViewById(R.id.userImage);
HashMap<String, String> usersList = new HashMap<>();
usersList = data.get(position);
username.setText(usersList.get(OtherUsers.firstName));
String image = usersList.get(OtherUsers.userImage);
Log.d("Image is ",image);
byte[] newImageBytes = image.getBytes();
Bitmap bitmap = BitmapFactory.decodeByteArray(newImageBytes,0,newImageBytes.length);
userImage.setImageBitmap(bitmap);
userImage.setImageBitmap(null);
return view;
}
}
如果需要任何其他信息,请告诉我。谢谢。
更新 抱歉,忘记放LogCat了
08-24 13:43:16.618 29948-29948/internetlegion.com.gradlecheck E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: internetlegion.com.gradlecheck, PID: 29948
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:139)
at internetlegion.com.gradlecheck.Adapters.UserAdapter.getView(UserAdapter.java:62)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17547)
【问题讨论】:
-
image的值是多少?!您将其声明为 String 然后使用getByte方法?! -
@shayanpourvatan 是的,我知道它是一个字符串,就像在将它插入到 restDisplay 之前一样,我将它转换为 restDisplay.put("userImage","data:image/png;base64," + Base64 .encode(restPerson.getProfilePhoto())); .
-
你有一个错误
Log.d("Image is ",image);图像为空并且错误尝试说“println 需要一条消息” -
@shayanpourvatan:是的。但是在我正在检索 RestPerson 列表的 OtherUsers 类中,那里有图像。我正在将它连接到适配器,但我仍然不明白为什么图像不存在,即使用位置查找也是如此。你知道我能做些什么来解决这个问题吗?
-
@shayanpourvatan 就是这个,restDisplay.put("userImage","data:image/png;base64," + Base64.encode(restPerson.getProfilePhoto())); .您可以在 for 循环中的 OtherUsers 类中看到它。我是否应该在类的顶部声明它不是字符串。你能否请检查一下 OtherUsers 类,这样你就会明白我想要做什么。谢谢。
标签: java android android-listview baseadapter