简体   繁体   中英

thread asynctask and listview

Hi guys i have a problem with my code... i have moved myTask into Splashscreen.java.. this is arrivi.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View arrivi = inflater.inflate(R.layout.arrivi, container, false); 


       return arrivi;
    }

    public void CheckRow(){

        ListView list;
        rowItems = new ArrayList<RowItem>();
        for (int i = 1; i <= thread; i++) {

            RowItem item = new RowItem(Compagnia[i], CodiceVolo[i], Citta[i],OraPrevista[i],  OraStimata[i], StatoVolo[i]);
            rowItems.add(item);
            System.out.println(CodiceVolo[i]);
        } 
        list=(ListView)arrivi.findViewById(R.id.listView1);
        CustomBaseAdapter adapter = new CustomBaseAdapter(getActivity(), rowItems);
        list.setAdapter(adapter);

line 103 is

  list=(ListView)getActivity().findViewById(R.id.listView1);

SplashScreen.java

public class SplashScreen extends Activity {

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.splashscreen);

Instead of using getActivity(), you should be getting the layout from the Fragment.getView().

Update

So, you're launching your AsyncTask in the onCreate of your Activity. The AsyncTask's onPostExecute can get called before your Fragment's onCreateView gets called, which is what I suspect is happening here; hence the null pointer. In general, these are some of the nuances of using AsyncTasks.

You have several options:

  1. Use Loaders in favor of AsyncTasks
  2. Launch the AsyncTask from your Fragment, which guarantees the Fragment to be attached and active.
  3. In CheckRow, make sure you're Fragment is active before performing the work.
  4. Use retained fragments to perform background processing

Checkout this link for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM