Error when using fragement and asynctask
I am trying to get a list of data from MySQL that is hosted then return the result in a listview. But I am not able to make it when using fragments.
I am getting the following error:
com.example.test.myapp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.exampletest.myapp, PID: 31491 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 at com.example.test.myapp.homeOperation.doInBackground(homeOperation.java:71) at com.example.test.myapp.homeOperation.doInBackground(homeOperation.java:23) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
With the class shown below: I connect to the server, then I fetch the result, so I can parse the data and put in in ArrayLists.
public class homeOperation extends AsyncTask<String, Void, String> {
List<String> title_list = new ArrayList<String>();
List<String> id_list = new ArrayList<String>();
Context context;
AlertDialog alertDialog;
homeOperation(Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://xxxx/data.php";
if (type.equals("home")) {
try {
String events = params[1];
String task_owner = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("events", "UTF-8") + "=" + URLEncoder.encode(events, "UTF-8") + "&"
+ URLEncoder.encode("task_owner", "UTF-8") + "=" + URLEncoder.encode(task_owner, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
String[] arr = result.split("--");
for (int i = 0; i < arr.length; i++) {
String cur = arr[i];
title_list.add(cur.split(":")[0]);
id_list.add(cur.split(":")[1]);
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(String result) {
Toast toast = Toast.makeText(this.context, result, Toast.LENGTH_LONG);
toast.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Now, this is the class in which I'm trying to get the data from the homeOperation class and then put the data into a ListView.
public class ContentFragment extends Fragment {
ListView lv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_fragment, container, false);
lv = (ListView) view.findViewById(R.id.resultList);
String type = "home";
homeOperation homeOperation = new homeOperation(ContentFragment.this.getActivity());
homeOperation.execute(type, "", "");
// This is the array adapter, it takes the context of the activity as a
// first parameter, the type of list view as a second parameter and your
// array as a third parameter.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ContentFragment.this.getActivity(), android.R.layout.simple_list_item_1, homeOperation.title_list);
lv.setAdapter(arrayAdapter);
//addButton onClick
ImageButton addButton = (ImageButton) view.findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addPaige(view);
}
});
return view;
}
public void addPaige(View v) {
Intent goToAddPaige = new Intent(getActivity(), AddPaige.class);
startActivity(goToAddPaige);
}
}
I think there is a problem when sending the context using fragments.
title_list
will have no data because your AsyncTask.execute()
will be done after lv.setAdapter(arrayAdapter);
.
So if you want to handling title_list
with enough data, use android.os.Handler
in onPostExecute()
to send your result of AsyncTask.
Try as below:
Your AsyncTask
public class homeOperation extends AsyncTask<String,Void,String> {
List<String> title_list = new ArrayList<String>();
List<String> id_list = new ArrayList<String>();
Context context;
Handler handler;
AlertDialog alertDialog;
homeOperation(Context ctx, Handler hnd) {
context = ctx;
handler = hnd;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://xxxx/data.php";
if (type.equals("home")) {
try {
String events = params[1];
String task_owner = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("events", "UTF-8") + "=" + URLEncoder.encode(events, "UTF-8") + "&"
+ URLEncoder.encode("task_owner", "UTF-8") + "=" + URLEncoder.encode(task_owner, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
String[] arr = result.split("--");
for (int i = 0; i < arr.length; i++) {
String cur = arr[i];
title_list.add(cur.split(":")[0]);
id_list.add(cur.split(":")[1]);
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(String result) {
Toast toast = Toast.makeText(this.context, result, Toast.LENGTH_LONG);
toast.show();
handler.sendEmptyMessage(0);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Your Fragment
public class ContentFragment extends Fragment {
ListView lv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_fragment,container,false);
lv = (ListView) view.findViewById(R.id.resultList);
ViewHandler viewHnd = new ViewHandler(ContentFragment.this); // add this handler for parameter of yout AsyncTask.
String type = "home";
homeOperation homeOperation = new homeOperation(ContentFragment.this.getActivity(), viewHnd);
homeOperation.execute(type, "", "");
// This is the array adapter, it takes the context of the activity as a
// first parameter, the type of list view as a second parameter and your
// array as a third parameter.
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ContentFragment.this.getActivity() , android.R.layout.simple_list_item_1, homeOperation.title_list);
//
// lv.setAdapter(arrayAdapter);
//addButton onClick
ImageButton addButton = (ImageButton) view.findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addPaige(view);
}
});
return view;
}
public void addPaige(View v){
Intent goToAddPaige = new Intent(getActivity(), AddPaige.class);
startActivity(goToAddPaige);
}
private static class ViewHandler extends Handler {
private final WeakReference<ContentFragment> mFragment;
ViewHandler(ContentFragment fragment) {
mFragment = new WeakReference<ContentFragment>(fragment);
}
@Override
public void handleMessage(Message msg) {
ContentFragment fragment = mFragment.get();
if (fragment != null) {
fragment.handleMessage(msg);
}
}
}
private void handleMessage(Message msg) {
if (msg.what == 0) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ContentFragment.this.getActivity() , android.R.layout.simple_list_item_1, homeOperation.title_list);
lv.setAdapter(arrayAdapter);
}
}
}
Replace:
for (int i = 0; i < arr.length; i++) {
String cur = arr[i];
title_list.add(cur.split(":")[0]);
id_list.add(cur.split(":")[1]);
}
with:
for (int i = 0; i < arr.length; i++) {
String cur = arr[i];
String[] temp = cur.split(":");
if (temp.length == 2){
title_list.add(cur.split(":")[0]);
id_list.add(cur.split(":")[1]);
}
}
The stack trace is very clear by itself:
java.lang.Thread.run(Thread.java:818) Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 at
which should translate to this line:
id_list.add(cur.split(":")[1]);
You shouldn't expect that the split method will always give you a certain number of substrings. So you have to write a fallback for when it doesn't.
链接地址: http://www.djcxy.com/p/78414.html上一篇: 如何将用户添加到角色