AsyncTask 数组创建
AsyncTask Array creation
我正在尝试为我的 Android 项目创建 AsyncTask 数组
将数据分配给 TextView。我需要为每个城市数据单独执行任务。
我的单个 AsyncTask 工作正常,所以我认为 main 方法没问题。
当我尝试创建任务数组时出现问题。
我收到错误 Attempt to invoke virtual method on null reference
数组任务执行与单个任务完全相同的代码,所以问题一定是我如何创建数组
我发布整个 Main Activity 来展示我尝试做的事情。具有单个任务和数组任务。
如有任何帮助,我将不胜感激。
private LinearLayout editLayout;
private LinearLayout layout1;
private LinearLayout layout2;
private LinearLayout layout3;
private LinearLayout layout4;
int n;
TextView[] city = new TextView[10];
TextView[] time = new TextView[10];
TextView[] temperature = new TextView[10];
private ImageView delete1;
private ImageView delete2;
private ImageView delete3;
private ImageView delete4;
private ImageView condition1;
private ImageView condition2;
private ImageView condition3;
private ImageView condition4;
Weather weather = new Weather();
Task task = new Task();
Task task1 = new Task();
private Task[] myTasks = new Task[10];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editLayout = (LinearLayout) findViewById(R.id.layEdit);
layout1 = (LinearLayout) findViewById(R.id.lay1);
layout2 = (LinearLayout) findViewById(R.id.lay2);
layout3 = (LinearLayout) findViewById(R.id.lay3);
layout4 = (LinearLayout) findViewById(R.id.lay4);
city[0] = (TextView) findViewById(R.id.city1);
city[1] = (TextView) findViewById(R.id.city2);
city[2] = (TextView) findViewById(R.id.city3);
city[3] = (TextView) findViewById(R.id.city4);
time[0] = (TextView) findViewById(R.id.time1);
time[1] = (TextView) findViewById(R.id.time2);
time[2] = (TextView) findViewById(R.id.time3);
time[3] = (TextView) findViewById(R.id.time4);
temperature[0] = (TextView) findViewById(R.id.temp1);
temperature[1] = (TextView) findViewById(R.id.temp2);
temperature[2] = (TextView) findViewById(R.id.temp3);
temperature[3] = (TextView) findViewById(R.id.temp4);
delete1 = (ImageView) findViewById(R.id.delete1);
delete2 = (ImageView) findViewById(R.id.delete2);
delete3 = (ImageView) findViewById(R.id.delete3);
delete4 = (ImageView) findViewById(R.id.delete4);
condition1 = (ImageView) findViewById(R.id.sun1);
condition2 = (ImageView) findViewById(R.id.sun2);
condition3 = (ImageView) findViewById(R.id.sun3);
condition4 = (ImageView) findViewById(R.id.sun4);
//weatherData("Paris, France");
//weatherData("Paris, France", 0);
loadTask(1,"Dublin, Ireland" );
private void loadTask(int sInt, String city){
this.n=n;
myTasks[sInt].execute(new String[]{city + "&APPID="+ WeatherApiNumber + "&units=metric"});
}
public void weatherData(String city, int n){
this.n=n;
task1.execute(new String[]{city + "&APPID="+ WeatherApiNumber + "&units=metric"});
}
private class Task extends AsyncTask<String, Void, Weather>{
@Override
protected Weather doInBackground(String... params) {
String data = ((new WeatherClient()).getWeatherData(params[0]));
weather = Parser.getWeather(data);
return weather;
}
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
cityData(n);
}
}
public void cityData(int n){
city[n].setText(weather.place.getCity());
time[n].setText(Integer.toString(weather.place.getTimeZone()));
temperature[n].setText(Double.toString(weather.temp.getTemp()).substring(0,2));
//task.cancel(true);
}
public void cityData2(int n){
;
city[n].setText(weather.place.getCity());
time[n].setText(Integer.toString(weather.place.getTimeZone()));
temperature[1].setText(Double.toString(weather.temp.getTemp()).substring(0,2));
//task.cancel(true);
}
}
您没有为任务数组 myTasks 分配任何内容
在这一行中,您只初始化数组而不是数组项。
private Task[] myTasks = new Task[10];
您应该在某个时候将任务分配给 myTasks 中的单元格,然后才对其调用执行
我正在尝试为我的 Android 项目创建 AsyncTask 数组 将数据分配给 TextView。我需要为每个城市数据单独执行任务。
我的单个 AsyncTask 工作正常,所以我认为 main 方法没问题。 当我尝试创建任务数组时出现问题。
我收到错误 Attempt to invoke virtual method on null reference
数组任务执行与单个任务完全相同的代码,所以问题一定是我如何创建数组
我发布整个 Main Activity 来展示我尝试做的事情。具有单个任务和数组任务。 如有任何帮助,我将不胜感激。
private LinearLayout editLayout;
private LinearLayout layout1;
private LinearLayout layout2;
private LinearLayout layout3;
private LinearLayout layout4;
int n;
TextView[] city = new TextView[10];
TextView[] time = new TextView[10];
TextView[] temperature = new TextView[10];
private ImageView delete1;
private ImageView delete2;
private ImageView delete3;
private ImageView delete4;
private ImageView condition1;
private ImageView condition2;
private ImageView condition3;
private ImageView condition4;
Weather weather = new Weather();
Task task = new Task();
Task task1 = new Task();
private Task[] myTasks = new Task[10];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editLayout = (LinearLayout) findViewById(R.id.layEdit);
layout1 = (LinearLayout) findViewById(R.id.lay1);
layout2 = (LinearLayout) findViewById(R.id.lay2);
layout3 = (LinearLayout) findViewById(R.id.lay3);
layout4 = (LinearLayout) findViewById(R.id.lay4);
city[0] = (TextView) findViewById(R.id.city1);
city[1] = (TextView) findViewById(R.id.city2);
city[2] = (TextView) findViewById(R.id.city3);
city[3] = (TextView) findViewById(R.id.city4);
time[0] = (TextView) findViewById(R.id.time1);
time[1] = (TextView) findViewById(R.id.time2);
time[2] = (TextView) findViewById(R.id.time3);
time[3] = (TextView) findViewById(R.id.time4);
temperature[0] = (TextView) findViewById(R.id.temp1);
temperature[1] = (TextView) findViewById(R.id.temp2);
temperature[2] = (TextView) findViewById(R.id.temp3);
temperature[3] = (TextView) findViewById(R.id.temp4);
delete1 = (ImageView) findViewById(R.id.delete1);
delete2 = (ImageView) findViewById(R.id.delete2);
delete3 = (ImageView) findViewById(R.id.delete3);
delete4 = (ImageView) findViewById(R.id.delete4);
condition1 = (ImageView) findViewById(R.id.sun1);
condition2 = (ImageView) findViewById(R.id.sun2);
condition3 = (ImageView) findViewById(R.id.sun3);
condition4 = (ImageView) findViewById(R.id.sun4);
//weatherData("Paris, France");
//weatherData("Paris, France", 0);
loadTask(1,"Dublin, Ireland" );
private void loadTask(int sInt, String city){
this.n=n;
myTasks[sInt].execute(new String[]{city + "&APPID="+ WeatherApiNumber + "&units=metric"});
}
public void weatherData(String city, int n){
this.n=n;
task1.execute(new String[]{city + "&APPID="+ WeatherApiNumber + "&units=metric"});
}
private class Task extends AsyncTask<String, Void, Weather>{
@Override
protected Weather doInBackground(String... params) {
String data = ((new WeatherClient()).getWeatherData(params[0]));
weather = Parser.getWeather(data);
return weather;
}
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
cityData(n);
}
}
public void cityData(int n){
city[n].setText(weather.place.getCity());
time[n].setText(Integer.toString(weather.place.getTimeZone()));
temperature[n].setText(Double.toString(weather.temp.getTemp()).substring(0,2));
//task.cancel(true);
}
public void cityData2(int n){
;
city[n].setText(weather.place.getCity());
time[n].setText(Integer.toString(weather.place.getTimeZone()));
temperature[1].setText(Double.toString(weather.temp.getTemp()).substring(0,2));
//task.cancel(true);
}
}
您没有为任务数组 myTasks 分配任何内容
在这一行中,您只初始化数组而不是数组项。
private Task[] myTasks = new Task[10];
您应该在某个时候将任务分配给 myTasks 中的单元格,然后才对其调用执行