简体   繁体   English

线程asynctask和listview

[英]thread asynctask and listview

Hi guys i have a problem with my code... i have moved myTask into Splashscreen.java.. this is arrivi.java 嗨,大家好,我的代码有问题...我已将myTask移至Splashscreen.java ..这是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 103行是

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

SplashScreen.java 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(). 而不是使用getActivity(),应该从Fragment.getView()获取布局。

Update 更新

So, you're launching your AsyncTask in the onCreate of your Activity. 因此,您将在Activity的onCreate中启动AsyncTask。 The AsyncTask's onPostExecute can get called before your Fragment's onCreateView gets called, which is what I suspect is happening here; 可以在调用您的Fragment的onCreateView之前调用AsyncTask的onPostExecute,这是我怀疑在这里发生的情况; hence the null pointer. 因此为空指针。 In general, these are some of the nuances of using AsyncTasks. 通常,这些是使用AsyncTasks的一些细微差别。

You have several options: 您有几种选择:

  1. Use Loaders in favor of AsyncTasks 使用加载程序以支持AsyncTasks
  2. Launch the AsyncTask from your Fragment, which guarantees the Fragment to be attached and active. 从您的片段启动AsyncTask,这将确保片段被附加并处于活动状态。
  3. In CheckRow, make sure you're Fragment is active before performing the work. 在CheckRow中,在执行工作之前,请确保您处于“活动片段”状态。
  4. Use retained fragments to perform background processing 使用保留的片段执行后台处理

Checkout this link for more info. 查看链接以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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