onPostExecute在片段活动中不起作用

时间:2015-08-16 19:49:11

标签: java android android-fragments android-asynctask

我想在片段活动中使用Jsoup从网站加载一些数据,并在Textview中显示输出。我正在使用 AsyncTask 从使用Jsoup的网站加载数据,但在成功从网站获取数据后, AsyncTask 中的 onPostExecute 方法不是显示数据。我试图调试它,似乎 onPostExecute 中存在问题 而且我不知道为什么。

这是我的代码..

MainActivity2.java

package com.example.ebad.bustudentprofile;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import com.example.ebad.bustudentprofile.tabs.SlidingTabLayout;

public class MainActivity2 extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        // Layout manager that allows the user to flip through the pages
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

        // getSupportFragmentManager allows use to interact with the fragments
        // MyFragmentPagerAdapter will return a fragment based on an index that is passed
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
                MainActivity2.this));

        // Initialize the Sliding Tab Layout
        SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);

        // Connect the viewPager with the sliding tab layout
        slidingTabLayout.setViewPager(viewPager);
    }


}

MyFragmentPagerAdapter.java

package com.example.ebad.bustudentprofile;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private String tabTitles[] = new String[]{"Tab 1", "Tab 2", "Tab 3"};
    private Context context;

    public MyFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return 3;
    }

    // Return the correct Fragment based on index
    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return new TabFragment1();
        } else if (position == 1) {
            return new TabFragment2();
        } else if (position == 2) {
            return new TabFragment3();
        }

        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Return the tab title to SlidingTabLayout
        return tabTitles[position];
    }


}

TabFragment1.java

package com.example.ebad.bustudentprofile;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.HashMap;


public class TabFragment1 extends Fragment {

    ProgressDialog progressDialoge;
    TextView fa, na;
    String urlw = "http://111.68.99.8/StudentProfile/PersonalInfo.aspx";
    HashMap<String, String> hashMap;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        hashMap = Data.map;

        //hashMap = (HashMap<String, String>) getArguments().get("hashMap");
        View rootview = inflater.inflate(R.layout.tab_fragment_1, container, false);
        Activity activity = getActivity();

        fa = (TextView) rootview.findViewById(R.id.Fathere);
        na = (TextView) rootview.findViewById(R.id.Y_Name);

        new peea(activity,rootview).execute();

        return inflater.inflate(R.layout.tab_fragment_1, container, false);
    }


    private class peea extends AsyncTask<Void, Void, Void> {

        String father ;
        String son ;
        private Context activity;
        private View rootView;

        public peea(Context context, View main){
            this.activity=context;
            this.rootView=main;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialoge = new ProgressDialog(getActivity());
            progressDialoge.setMessage("Getting into your information");
            progressDialoge.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            try {
                Document doce = Jsoup.connect(urlw)
                        .cookies(hashMap)
                        .get();

                father = doce.select("span[id=ctl00_Body_FATHERNAME]").html();
                son = doce.select("span[id=ctl00_Body_NAME]").html();


            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }


        @Override
        protected void onPostExecute(Void aVoid) {
           super.onPreExecute();
               TextView wa = (TextView)rootView.findViewById(R.id.Y_Name);
                wa.setText(son);

                /*fa.setText(father);


                na.setText(son);*/
                /*fa = (TextView)findViewById(R.id.das);

                fa.setText(father);*/

                progressDialoge.dismiss();

            }


        }

    }

tab_fragment_1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView

        android:id="@+id/Fathere"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Father Name"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/Y_Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Your name"
        android:textSize="30dp" />
</LinearLayout>

如果你们告诉问题在哪里或如何解决,那对我来说非常有帮助

1 个答案:

答案 0 :(得分:1)

我认为您的super班级不具有onPreExecute功能,所以

super.onPreExecute();

可能会给您带来一些问题。你可以尝试

super.onPostExecute();

这可能会解决它。

相关问题