读取文本文件并将其存储在数组中(Android开发)
在读取文本文件并将其存储在数组中(Android开发),android,arrays,file-io,nullpointerexception,crash,Android,Arrays,File Io,Nullpointerexception,Crash,在数据类中 public static String[] array_data = new String[20]; 请注意: public void postData() { //HttpClient httpClient = new DefaultHttpClient(); //13 questions //12 indices //22 questions //21 indices String fullUrl = GoogleForm
数据
类中
public static String[] array_data = new String[20];
请注意:
public void postData()
{
//HttpClient httpClient = new DefaultHttpClient();
//13 questions
//12 indices
//22 questions
//21 indices
String fullUrl = GoogleFormInfo.Google_array[0];
HttpRequest mReq = new HttpRequest();
Log.i("first index of array",Data.array_data[0].toString());
String col1 = Data.array_data[0];
String col2 = Data.array_data[1];
String col3 = Data.array_data[2];
String col4 = Data.array_data[3];
String col5 = Data.array_data[4];
String col6 = Data.array_data[5];
String col7 = Data.array_data[6];
String col8 = Data.array_data[7];
String col9 = Data.array_data[8];
String col10 = Data.array_data[9];
String col11 = Data.array_data[10];
String col12 = Data.array_data[11];
String col13 = Data.array_data[12];
String col14 = Data.array_data[13];
String col15 = Data.array_data[14];
String col16 = Data.array_data[15];
String col17 = Data.array_data[16];
String col18 = Data.array_data[17];
String col19 = Data.array_data[18];
String col20 = Data.array_data[19];
Log.i("google!",GoogleFormInfo.Google_array[1].toString());
Log.i("google!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",URLEncoder.encode(col1).toString());
String data = GoogleFormInfo.Google_array[1] + URLEncoder.encode(col1) + "&" +
GoogleFormInfo.Google_array[2] + URLEncoder.encode(col2) + "&" +
GoogleFormInfo.Google_array[3] + URLEncoder.encode(col3)+ "&" +
GoogleFormInfo.Google_array[4] + URLEncoder.encode(col4)+ "&" +
GoogleFormInfo.Google_array[5] + URLEncoder.encode(col5)+ "&" +
GoogleFormInfo.Google_array[6] + URLEncoder.encode(col6)+ "&" +
GoogleFormInfo.Google_array[7] + URLEncoder.encode(col7)+ "&" +
GoogleFormInfo.Google_array[8] + URLEncoder.encode(col8)+ "&" +
GoogleFormInfo.Google_array[9] + URLEncoder.encode(col9)+ "&" +
GoogleFormInfo.Google_array[10]+ URLEncoder.encode(col10)+ "&" +
GoogleFormInfo.Google_array[11]+ URLEncoder.encode(col11)+ "&" +
GoogleFormInfo.Google_array[12]+ URLEncoder.encode(col12)+ "&" +
GoogleFormInfo.Google_array[13]+ URLEncoder.encode(col13)+ "&" +
GoogleFormInfo.Google_array[14]+ URLEncoder.encode(col14)+ "&" +
GoogleFormInfo.Google_array[15]+ URLEncoder.encode(col15)+ "&" +
GoogleFormInfo.Google_array[16]+ URLEncoder.encode(col16)+ "&" +
GoogleFormInfo.Google_array[17]+ URLEncoder.encode(col17)+ "&" +
GoogleFormInfo.Google_array[18]+ URLEncoder.encode(col18)+ "&" +
GoogleFormInfo.Google_array[19]+ URLEncoder.encode(col19)+ "&" +
GoogleFormInfo.Google_array[20]+ URLEncoder.encode(col20);
String response = mReq.sendPost(fullUrl, data);
//Log.i(myTag, response);
}
未打印出来。线程
t
调用postData()
和原始线程使用此代码为空的静态数组\u数据
:
Log.i("first index of array",Data.array_data[0].toString());
for(int j=0; j<=19; j++)
{
Data.array_data[j]=null;
}
对于(int j=0;j请尝试以下代码:
Log.i("first index of array",Data.array_data[0].toString());
for(int j=0; j<=19; j++)
{
Data.array_data[j]=null;
}
在数据类中,postData方法是:
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
postData(Data.array_data);
}
});
t.start();
这样做可以避免静态数组出现问题堆栈跟踪告诉您开始在第235行的MainActivity.java中查找在为null(未初始化)时尝试使用的内容您没有指明两个代码列表的文件名,因此很难判断它可能是什么。请尝试调试,什么是空的对象?@Ultimo\m我认为数组数据中的元素是空的。if(I==19){Log.I(“零”,data.array\u data[0].toString();…postData();}
这部分工作。但是当我这样做时,Log.I>(“数组的第一个索引”,Data.array_Data[0].toString();
在postData()
中,它是一个空值。我认为这是关于线程的某个地方不正确的…如果(I==19){Log.I(“零”,Data.array_Data[0].toString();…postData();}
这部分可以工作。但是当我执行Log.I(“数组的第一个索引”)“,Data.array_Data[0].toString());
中的postData()
,它是一个空值。我认为这是关于线程的某个方面不正确…@Junuthunlum线程t
唯一的问题是它信任原始线程而不是它的数据。请参阅我更新的答案。你让他将一个静态传递到postData。静态的问题是它只有一个副本共享。这意味着他仍然无法再次读取(或毫无意义地将其设置为null),因为他永远不知道何时完成postData。他需要创建新实例(最好是不可变的)一旦他把它们交给postData,就不要碰它们。如果他创建了一个新实例,他可以随时从文件中读取。是的,对我来说,另一个解决方案是在postData方法下面调用使数组为null的函数。因此,他在完成该操作后,总是使数组为null。我不知道这是如何工作的。没有创建唯一的副本。当他开始为同一数据结构的元素设置空值时,仅仅对同一数据结构有两个引用是没有帮助的。