当我在一个类文件中使用我的代码时,它运行得很完美但是从主活动中分离Async方法时:
GetCategories class:
package ir.reza.smss;
import ir.reza.smss.Fragments.TabFragment1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
public class GetCategories extends AsyncTask<Void, Void, Void> {
private Context context;
private ArrayList<Category> categoriesList;
ProgressDialog pDialog;
// Url to get all categories
private String URL_CATEGORIES = "http://varzesh-ilam.xzn.ir/app/get_categories.php";
public GetCategories(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray categories = jsonObj
.getJSONArray("categories");
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(catObj.getInt("id"),
catObj.getString("name"));
categoriesList.add(cat);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
TabFragment1 tb1 = new TabFragment1();
tb1.populateSpinner();
}
}
和logcat:
01-22 11:17:38.475: I/global(9785): call createSocket() return a new socket.
01-22 11:18:22.465: W/dalvikvm(10293): threadid=12: thread exiting with uncaught exception (group=0x417e9a08)
01-22 11:18:22.470: E/AndroidRuntime(10293): FATAL EXCEPTION: AsyncTask #1
01-22 11:18:22.470: E/AndroidRuntime(10293): java.lang.RuntimeException: An error occured while executing doInBackground()
01-22 11:18:22.470: E/AndroidRuntime(10293): at android.os.AsyncTask$3.done(AsyncTask.java)
01-22 11:18:22.470: E/AndroidRuntime(10293): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java)
01-22 11:18:22.470: E/AndroidRuntime(10293): at java.util.concurrent.FutureTask.setException(FutureTask.java)
01-22 11:18:22.470: E/AndroidRuntime(10293): at java.util.concurrent.FutureTask.run(FutureTask.java)
01-22 11:18:22.470: E/AndroidRuntime(10293): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
01-22 11:18:22.470: E/AndroidRuntime(10293): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java)
01-22 11:18:22.470: E/AndroidRuntime(10293): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java)
01-22 11:18:22.470: E/AndroidRuntime(10293): at java.lang.Thread.run(Thread.java)
01-22 11:18:22.470: E/AndroidRuntime(10293): Caused by: java.lang.NullPointerException
01-22 11:18:22.470: E/AndroidRuntime(10293): at ir.reza.smss.GetCategories.doInBackground(GetCategories.java:64)
01-22 11:18:22.470: E/AndroidRuntime(10293): at ir.reza.smss.GetCategories.doInBackground(GetCategories.java:1)
01-22 11:18:22.470: E/AndroidRuntime(10293): at android.os.AsyncTask$2.call(AsyncTask.java)
01-22 11:18:22.470: E/AndroidRuntime(10293): ... 5 more
我想从不同的类文件中运行asyntask 我正在使用eclipse,而且我是java和android dev的新手。
答案 0 :(得分:0)
java.lang.NullPointerException at ir.reza.smss.GetCategories.doInBackground(GetCategories.java:64)
使用调试器检查值,并查看传递空对象的位置。 (第64行)