当我尝试从片段onCreateView
调用AsyncTask时,我得到空指针异常。我必须在发送请求时显示进度对话框。
**在GrIsHu的建议之后我得到了例外**
03-04 11:16:42.762: E/AndroidRuntime(10435): FATAL EXCEPTION: main
03-04 11:16:42.762: E/AndroidRuntime(10435): java.lang.NullPointerException
03-04 11:16:42.762: E/AndroidRuntime(10435): at com.test.newinfiniteapp.RequestClient.onPreExecute(RequestClient.java:25)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.os.AsyncTask.execute(AsyncTask.java:534)
03-04 11:16:42.762: E/AndroidRuntime(10435): at com.test.newinfiniteapp.PendingApprovals.postHttpRequest(PendingApprovals.java:295)
03-04 11:16:42.762: E/AndroidRuntime(10435): at com.test.newinfiniteapp.PendingApprovals.onCreateView(PendingApprovals.java:49)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.os.Handler.handleCallback(Handler.java:800)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.os.Handler.dispatchMessage(Handler.java:100)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.os.Looper.loop(Looper.java:194)
03-04 11:16:42.762: E/AndroidRuntime(10435): at android.app.ActivityThread.main(ActivityThread.java:5371)
03-04 11:16:42.762: E/AndroidRuntime(10435): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 11:16:42.762: E/AndroidRuntime(10435): at java.lang.reflect.Method.invoke(Method.java:525)
03-04 11:16:42.762: E/AndroidRuntime(10435): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-04 11:16:42.762: E/AndroidRuntime(10435): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-04 11:16:42.762: E/AndroidRuntime(10435): at dalvik.system.NativeStart.main(Native Method)
的AsyncTask
public class RequestClient extends AsyncTask<String, Void, String>{
Context context;
CallBack callBack;
public RequestClient (CallBack callBack) {
this.callBack = callBack;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Main.progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String responseString="";
HttpClient client = null;
try {
client = new DefaultHttpClient();
HttpGet get = new HttpGet(param[0]);
client.getParams().setParameter("http.socket.timeout", 6000);
client.getParams().setParameter("http.connection.timeout", 6000);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
responseString = EntityUtils.toString(resEntityGet);
Log.i("GET RESPONSE", responseString.trim());
}
} catch (Exception e) {
Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
}
Log.d("ANDRO_ASYNC_RESPONSE", responseString.trim());
client.getConnectionManager().shutdown();
return responseString.trim();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
callBack.run(result);
Main.progressDialog.dismiss();
}
}
MainClass
public class Main extends Fragment {
public static ProgressDialog progressDialog;
final Context context = getActivity();
public static String url = ConfigUrl.MainUrl;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity,
container, false);
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Please Wait .!!!");
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.THEME_HOLO_DARK);
progressDialog.setCancelable(false);
postHttpRequest();
return rootView;
}
private void postHttpRequest() {
RequestClient reqClient = new RequestClient(new CallBack() {
@Override
public void run(Object result) {
try {
String AppResponse = (String) result;
Log.d("Status recived", AppResponse);
} catch (Exception e) {
Log.e("Exception Occured", "Exception is " + e.getMessage());
}
}
});
reqClient.execute(url);
}
}
请帮助我如何做到这一点。我不想创建异步任务的内部类
答案 0 :(得分:1)
首先在context
方法中定义onCreateView
。
final Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity,
container, false);
context = getActivity();
答案 1 :(得分:1)
progressDialog.setProgressStyle(ProgressDialog.THEME_HOLO_DARK);
答案 2 :(得分:0)
使用以下方式
RegisterAsync extends AsyncTask<String, Void, AsyncTaskResult<JSONObject>> {
ProgressDialog dlg;
@Override
protected void onPreExecute() {
super.onPreExecute();
dlg = new ProgressDialog(context);
dlg.setCanceledOnTouchOutside(false);
dlg.setCancelable(false);
dlg.setMessage("Please wait....");
dlg.show();
}
@Override
protected AsyncTaskResult<JSONObject> doInBackground(String... params) {
//do your stuff
}
protected void onPostExecute(AsyncTaskResult<JSONObject> result) {
}