当我尝试将list的值移动到数组String时,我有一个代码,但是我有这个错误
java.lang.NullPointerException:尝试从空数组中读取
这是代码
public class MainActivity extends Activity {
private GridView gridView;
private GridviewAdapter gridAdapter;
ArrayList<Integer> list = new ArrayList<Integer>();
private static List<Integer> randomNumber;
private int endRange;
String[] strarray1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//Obtenemos la referencia de la vista
gridView = (GridView) findViewById(R.id.gridView);
//**********************************************
// getUrl("http://imgwslili.gtcine.com/index.html", "0");
endRange = getUrl("http://imgwslili.gtcine.com/value.txt");
//********************************************************
randomNumber = new ArrayList<Integer>(endRange);
System.out.println("IMPRIMIR0 " + endRange);
endRange = endRange-100;
int j=100;
for (int i = 0; i <=endRange; i++) {
randomNumber.add(j);
j++;
// System.out.println("IMPRIMIR " + randomNumber.get(i));
}
System.out.println("IMPRIMIR1 " + randomNumber.size());
Collections.shuffle(randomNumber);
// System.out.println("IMPRIMIR2 " + randomNumber.get(5));
strarray1 = getitem();
//Construimos el adaptador pasando como
//segundo parametro el array de imagenes
gridAdapter = new GridviewAdapter(MainActivity.this, getitem());
//Especificamos el adaptador
gridView.setAdapter(gridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("ENVIAR " + position);
Toast.makeText(getBaseContext(), "Salida pos: "+position,Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, ActivityTwo.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
public String[] getitem(){
List<String> strlist = new ArrayList<String>();
// endRange= endRange-100;
int j = 100;
for(int i= 0; i<endRange; i++){
strlist.add("http://imgwslili.gtcine.com/img" + randomNumber.get(i) + ".jpg");
System.out.println("URL " + "http://imgwslili.gtcine.com/img" + randomNumber.get(i) + ".jpg");
}
// System.out.println("URL " + "http://img.gtcine.com/img" + randomNumber.get(j) + ".jpg");
String[] strarray = new String[strlist.size()+1];
strlist.toArray(strarray);
System.out.println("IMPRIME " + strarray.toString());
return strarray; //
}
public String getpost(int post){
String hola[] = strarray1;
System.out.print(hola);
System.out.println("GetPost " + post);
return strarray1[post];
}
我失去了我在android中的新手我想从互联网上显示图像并且工作正常但是当我从图库中选择一个图像时显示此错误:java.lang.NullPointerException:尝试从null读取阵列
答案 0 :(得分:1)
我不知道你到底想要做什么,但是当你向 getpost 方法
发送参数时,我已经改变了一些代码来显示结果 public static void main(String[] args){
System.out.println("Result: " + getpost(1));
}
public static String[] getitem(){
List<String> strlist = new ArrayList<String>();
// endRange= endRange-100;
int j = 100;
for(int i= 0; i<j; i++){
strlist.add("http://ime.com/img" + i + ".jpg");
}
String[] strarray = new String[strlist.size()+1];
strlist.toArray(strarray);
System.out.println("IMPRIME " + strarray);
return strarray; //
}
public static String getpost(int post){
String hola[] = getitem(); //HERE I CALL getitem()
System.out.print(hola);
System.out.println("GetPost " + post);
return hola[post];
}
答案 1 :(得分:0)
在您的代码中进行此更改,它可能会起作用:
strarray = strlist.toArray(strarray);
String hola[] = strarray1.clone();