Android 无法在 ListView 布局中设置图像
Android Can't set image in a ListView layout
看起来好像找到了图像,但我不知道如何插入简单的适配器(或任何让我在布局中设置图像的东西)
https://gyazo.com/61ab064c19ae50090f08e6436776a52e
我想为数组中的每个对象设置一个图像,例如:
monsterimglist.setImageResource(R.mipmap.icon_name);
目前我正在测试在列表视图的所有数组项中显示 R.mipmap.a1
。
错误:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ImageView.setImageResource(int)'
有人可以帮助我吗?非常感谢
ListView monsterslistview;
ImageView monsterimg;
TextView monstername;
TextView monstertype;
private static String monsterid;
ArrayList<HashMap<String, String>> MonstersList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_monsters);
//Fiquem els elements creats la seva id corresponent
monstername = (TextView) findViewById(R.id.namelayout);
monstertype = (TextView) findViewById(R.id.typelayout);
monsterslistview = (ListView) findViewById(R.id.monsterslistview);
monsterimg = (ImageView) findViewById(R.id.imglayout);
HashMap<String, String> hashMap;
//Creem un nou objecte ArrayList per inserir les dades
MonstersList = new ArrayList<HashMap<String, String>>();
//..........Process JSON DATA................
try {
JSONObject reader = new JSONObject(loadJSONFromAsset());
//Guardem a un Array de tipus JSON les dades
JSONArray jr = reader.getJSONArray("monsters");
for (int i = 0; i < jr.length(); i++) {
JSONObject jsonObjectLine = jr.getJSONObject(i);
// Desem els items JSON en una variable
String id = jsonObjectLine.getString("id");
String name = jsonObjectLine.getString("name");
String type = jsonObjectLine.getString("type");
String icon = jsonObjectLine.getString("icon");
//Toast.makeText(ListMonsters.this, id, Toast.LENGTH_LONG).show();
// Afegim la clau-valor a un objecte HashMap
hashMap = new HashMap<String, String>();
hashMap.put("id", id);
hashMap.put("name", name);
hashMap.put("type", type);
hashMap.put("icon", icon);
MonstersList.add(hashMap);
//mostrem per pantalla els elements que volem mostar
ListAdapter adapter = new SimpleAdapter(
ListMonsters.this,
MonstersList,
R.layout.onemonster,
new String[]{"name", "type","icon"},
new int[]{R.id.namelayout, R.id.typelayout, R.id.imglayout}
);
//Picasso.with(getApplicationContext()).load(R.mipmap.a1).into(monsterimglist);
monsterslistview.setAdapter(adapter);
//String idagafat = MonstersList.get(i).get("icon").replace(" ", "");
monsterimg.setImageResource(R.mipmap.a1);
monsterslistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/**
String idagafat = MonstersList.get(position).get("id").replace(" ", "");
String positionagafada = String.valueOf(position);
openmonster(idagafat, positionagafada);
**/
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Funció llegir json
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = ListMonsters.this.getAssets().open("monsters.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
public void openmonster(String id, String pos){
int posarray = Integer.parseInt(pos);
Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("icon").replace(" ", ""), Toast.LENGTH_SHORT).show();
//Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("name").replace(" ", ""), Toast.LENGTH_SHORT).show();
//Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("type").replace(" ", ""), Toast.LENGTH_SHORT).show();
}
}
在创建之前关闭括号 adapter.May 因为它会改变你的结果。
更新:
ListView monsterslistview; ImageView monsterimg; TextView monstername; TextView monstertype;
private static String monsterid; ArrayList<HashMap<String, String>> MonstersList;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_monsters);
//Fiquem els elements creats la seva id corresponent
monstername = (TextView) findViewById(R.id.namelayout);
monstertype = (TextView) findViewById(R.id.typelayout);
monsterslistview = (ListView) findViewById(R.id.monsterslistview);
monsterimg = (ImageView) findViewById(R.id.imglayout);
HashMap<String, String> hashMap;
//Creem un nou objecte ArrayList per inserir les dades
MonstersList = new ArrayList<HashMap<String, String>>();
//..........Process JSON DATA................
try {
JSONObject reader = new JSONObject(loadJSONFromAsset());
//Guardem a un Array de tipus JSON les dades
JSONArray jr = reader.getJSONArray("monsters");
for (int i = 0; i < jr.length(); i++) {
JSONObject jsonObjectLine = jr.getJSONObject(i);
// Desem els items JSON en una variable
String id = jsonObjectLine.getString("id");
String name = jsonObjectLine.getString("name");
String type = jsonObjectLine.getString("type");
String icon = jsonObjectLine.getString("icon");
//Toast.makeText(ListMonsters.this, id, Toast.LENGTH_LONG).show();
// Afegim la clau-valor a un objecte HashMap
hashMap = new HashMap<String, String>();
hashMap.put("id", id);
hashMap.put("name", name);
hashMap.put("type", type);
hashMap.put("icon", icon);
MonstersList.add(hashMap);
} //-----This Bracket was missed in your code
//mostrem per pantalla els elements que volem mostar
ListAdapter adapter = new SimpleAdapter(
ListMonsters.this,
MonstersList,
R.layout.onemonster,
new String[]{"name", "type","icon"},
new int[]{R.id.namelayout, R.id.typelayout, R.id.imglayout}
);
//Picasso.with(getApplicationContext()).load(R.mipmap.a1).into(monsterimglist);
monsterslistview.setAdapter(adapter);
//String idagafat = MonstersList.get(i).get("icon").replace(" ", "");
monsterimg.setImageResource(R.mipmap.a1);
monsterslistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/**
String idagafat = MonstersList.get(position).get("id").replace(" ", "");
String positionagafada = String.valueOf(position);
openmonster(idagafat, positionagafada);
**/
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Funció llegir json public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = ListMonsters.this.getAssets().open("monsters.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json; }
public void openmonster(String id, String pos){
int posarray = Integer.parseInt(pos);
Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("icon").replace(" ", ""), Toast.LENGTH_SHORT).show();
//Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("name").replace(" ", ""), Toast.LENGTH_SHORT).show();
//Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("type").replace(" ", ""), Toast.LENGTH_SHORT).show(); }
看起来好像找到了图像,但我不知道如何插入简单的适配器(或任何让我在布局中设置图像的东西) https://gyazo.com/61ab064c19ae50090f08e6436776a52e
我想为数组中的每个对象设置一个图像,例如:
monsterimglist.setImageResource(R.mipmap.icon_name);
目前我正在测试在列表视图的所有数组项中显示 R.mipmap.a1
。
错误:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ImageView.setImageResource(int)'
有人可以帮助我吗?非常感谢
ListView monsterslistview;
ImageView monsterimg;
TextView monstername;
TextView monstertype;
private static String monsterid;
ArrayList<HashMap<String, String>> MonstersList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_monsters);
//Fiquem els elements creats la seva id corresponent
monstername = (TextView) findViewById(R.id.namelayout);
monstertype = (TextView) findViewById(R.id.typelayout);
monsterslistview = (ListView) findViewById(R.id.monsterslistview);
monsterimg = (ImageView) findViewById(R.id.imglayout);
HashMap<String, String> hashMap;
//Creem un nou objecte ArrayList per inserir les dades
MonstersList = new ArrayList<HashMap<String, String>>();
//..........Process JSON DATA................
try {
JSONObject reader = new JSONObject(loadJSONFromAsset());
//Guardem a un Array de tipus JSON les dades
JSONArray jr = reader.getJSONArray("monsters");
for (int i = 0; i < jr.length(); i++) {
JSONObject jsonObjectLine = jr.getJSONObject(i);
// Desem els items JSON en una variable
String id = jsonObjectLine.getString("id");
String name = jsonObjectLine.getString("name");
String type = jsonObjectLine.getString("type");
String icon = jsonObjectLine.getString("icon");
//Toast.makeText(ListMonsters.this, id, Toast.LENGTH_LONG).show();
// Afegim la clau-valor a un objecte HashMap
hashMap = new HashMap<String, String>();
hashMap.put("id", id);
hashMap.put("name", name);
hashMap.put("type", type);
hashMap.put("icon", icon);
MonstersList.add(hashMap);
//mostrem per pantalla els elements que volem mostar
ListAdapter adapter = new SimpleAdapter(
ListMonsters.this,
MonstersList,
R.layout.onemonster,
new String[]{"name", "type","icon"},
new int[]{R.id.namelayout, R.id.typelayout, R.id.imglayout}
);
//Picasso.with(getApplicationContext()).load(R.mipmap.a1).into(monsterimglist);
monsterslistview.setAdapter(adapter);
//String idagafat = MonstersList.get(i).get("icon").replace(" ", "");
monsterimg.setImageResource(R.mipmap.a1);
monsterslistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/**
String idagafat = MonstersList.get(position).get("id").replace(" ", "");
String positionagafada = String.valueOf(position);
openmonster(idagafat, positionagafada);
**/
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Funció llegir json
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = ListMonsters.this.getAssets().open("monsters.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
public void openmonster(String id, String pos){
int posarray = Integer.parseInt(pos);
Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("icon").replace(" ", ""), Toast.LENGTH_SHORT).show();
//Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("name").replace(" ", ""), Toast.LENGTH_SHORT).show();
//Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("type").replace(" ", ""), Toast.LENGTH_SHORT).show();
}
}
在创建之前关闭括号 adapter.May 因为它会改变你的结果。
更新:
ListView monsterslistview; ImageView monsterimg; TextView monstername; TextView monstertype;
private static String monsterid; ArrayList<HashMap<String, String>> MonstersList;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_monsters);
//Fiquem els elements creats la seva id corresponent
monstername = (TextView) findViewById(R.id.namelayout);
monstertype = (TextView) findViewById(R.id.typelayout);
monsterslistview = (ListView) findViewById(R.id.monsterslistview);
monsterimg = (ImageView) findViewById(R.id.imglayout);
HashMap<String, String> hashMap;
//Creem un nou objecte ArrayList per inserir les dades
MonstersList = new ArrayList<HashMap<String, String>>();
//..........Process JSON DATA................
try {
JSONObject reader = new JSONObject(loadJSONFromAsset());
//Guardem a un Array de tipus JSON les dades
JSONArray jr = reader.getJSONArray("monsters");
for (int i = 0; i < jr.length(); i++) {
JSONObject jsonObjectLine = jr.getJSONObject(i);
// Desem els items JSON en una variable
String id = jsonObjectLine.getString("id");
String name = jsonObjectLine.getString("name");
String type = jsonObjectLine.getString("type");
String icon = jsonObjectLine.getString("icon");
//Toast.makeText(ListMonsters.this, id, Toast.LENGTH_LONG).show();
// Afegim la clau-valor a un objecte HashMap
hashMap = new HashMap<String, String>();
hashMap.put("id", id);
hashMap.put("name", name);
hashMap.put("type", type);
hashMap.put("icon", icon);
MonstersList.add(hashMap);
} //-----This Bracket was missed in your code
//mostrem per pantalla els elements que volem mostar
ListAdapter adapter = new SimpleAdapter(
ListMonsters.this,
MonstersList,
R.layout.onemonster,
new String[]{"name", "type","icon"},
new int[]{R.id.namelayout, R.id.typelayout, R.id.imglayout}
);
//Picasso.with(getApplicationContext()).load(R.mipmap.a1).into(monsterimglist);
monsterslistview.setAdapter(adapter);
//String idagafat = MonstersList.get(i).get("icon").replace(" ", "");
monsterimg.setImageResource(R.mipmap.a1);
monsterslistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/**
String idagafat = MonstersList.get(position).get("id").replace(" ", "");
String positionagafada = String.valueOf(position);
openmonster(idagafat, positionagafada);
**/
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Funció llegir json public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = ListMonsters.this.getAssets().open("monsters.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json; }
public void openmonster(String id, String pos){
int posarray = Integer.parseInt(pos);
Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("icon").replace(" ", ""), Toast.LENGTH_SHORT).show();
//Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("name").replace(" ", ""), Toast.LENGTH_SHORT).show();
//Toast.makeText(ListMonsters.this, MonstersList.get(posarray).get("type").replace(" ", ""), Toast.LENGTH_SHORT).show(); }