Issue
I trying to create AsyncTask array for my Android project to assign data to TextView's. I need separate tasks for each city data.
My single AsyncTask working ok so I presume main method is fine. Problem starts when I try to create arrays of tasks.
I getting error Attempt to invoke virtual method on null reference
Array task execute exactly same code as single task so problem must be how I created array
I posting whole Main Activity to show what I try to do. With both single and array tasks. I would be grateful for any help.
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);
}
}
Solution
You are not assigning anything to your task array myTasks
in this line you are only initializing the array but not the array items.
private Task[] myTasks = new Task[10];
You should, at some point, assign a task to a cell in myTasks and only then call execute on it
Answered By - pantos27
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.