这是我的情况: 在我的onCreate()中,我想创建一个AsyncTask来检索WebService的数据并将其存储在ArrayList中(图形的Retrive点)。
然后在我的onCreateView()中,我想使用ArrayList并迭代它来绘制点......
我想要一些方法来确保在启动onCreateView()函数之前等待AsyncTask完成执行。然后我在onPostExecute中使用了回调,但它不起作用。我读到了关于在任务上使用get()的问题,但如果我使用get()方法,UI将被阻塞。所以只能在onPostExecute中使用回调方法。
但是我的代码在完成执行Aysnctask onCreateView并在此行获取NullPointer Exception之前运行如此(PF.size()!= 0)。
package hello.aws.graphs;
import android.support.v7.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import hello.aws.login.WebService;
public class PFGraph extends AppCompatActivity implements AsyncTaskCompleteListener {
public static boolean errored = false;
static ProgressDialog dialog = null;
public static Menu menu;
static ArrayList<PFKVABean> PF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
getData();/*Getting data from webservice**/
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent newIntent;
switch (item.getItemId()) {
case R.id.bb_menu_favorites:
getData();/*Getting data from webservice**/
break;
case R.id.bb_menu_recent:
getData();/*Getting data from webservice**/
break;
case R.id.bb_menu_recents:
getData();/*Getting data from webservice**/
break;
}
return true;
}
public void getData()
{
AsyncCallWS task = new AsyncCallWS(this);
task.execute();
}
@java.lang.Override
public void onTaskComplete(Object result) {
PF=(ArrayList)result;//Storing result of AsyncTask
Log.e("check","check");
}
/**
* A fragment containing a line chart.
*/
public static class PlaceholderFragment extends Fragment {
private LineChartView chart;
private LineChartData data;
private int numberOfLines = 2;
private boolean hasLines = true;
private boolean hasPoints = true;
private ValueShape shape = ValueShape.CIRCLE;
private boolean isFilled = false;
private boolean hasLabels = false;
private boolean isCubic = false;
private boolean hasLabelForSelected = false;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.fragment_pf_graph, container, false);
chart = (LineChartView) rootView.findViewById(R.id.chart);
chart.setOnValueTouchListener(new ValueTouchListener());
generateData();
return rootView;
}
private void generateData() {
chart.setZoomEnabled(true);
chart.setZoomType(ZoomType.HORIZONTAL);
List<Line> lines = new ArrayList<Line>();
List<AxisValue> axisValues = new ArrayList<AxisValue>();
if(!errored) {
if (PF.size() != 0) {/******NULL Pointer Exception***/
/****Here the logic for plotting the points on graph***/
}
else
{
Toast.makeText(getActivity(), "Error occured in invoking Webservice", Toast.LENGTH_LONG).show();
}
}
}
}
private class AsyncCallWS extends AsyncTask<String, Void, ArrayList> {
private AsyncTaskCompleteListener<ArrayList> callback;
public AsyncCallWS(AsyncTaskCompleteListener<ArrayList> cb) {
this.callback = cb;
}
@java.lang.Override
protected ArrayList doInBackground(String... params) {
PF=WebService.GetWSData();
return PF;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(PFGraph.this, R.style.MyTheme);
dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
dialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.cp_bar));
dialog.setCancelable(false);
dialog.show();
// getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();
}
@Override
protected void onPostExecute(ArrayList PF) {
if(dialog!=null||dialog.isShowing()) {
dialog.dismiss();
Log.e("v","v");
callback.onTaskComplete(PF);//Sending result
}
}
}
}
答案 0 :(得分:0)
将来自generateData();
的呼叫从onCreateView()
移至onTaskComplete()
,以便仅在填充数组后才会调用它。