2016-08-25 64 views
0

這是我的情況: 在我的onCreate()中,我想創建一個AsyncTask來檢索WebService的數據並將其存儲在ArrayList(圖的Retrive points)中。將AsyncTask結果傳遞給片段中的onCreateView

在我onCreateView(),我想使用ArrayList和遍歷它來繪製點

則...

我想一些方法來確保我等待的AsyncTask完成啓動前的執行onCreateView()函數。然後我在onPostExecute中使用回調,但它不工作。我讀過有關使用get()的任務,但如果我使用get()方法,UI將被阻塞。因此只能在onPostExecute中使用回調方法。

但是,我的代碼運行之前,完成Aysnctask onCreateView執行並獲得NullPointer異常在此行​​if(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

首先調用AsyncTask和onPostExecute(結果)調用您的片段結果PlaceHolderFragment.newInstance(YourClassToSend) – Nepster

回答

0

onCreateView()移動呼叫generateData();onTaskComplete(),以便它能夠數組填滿後才能調用。