我对viewpager的实现让我对片段/活动感到很困惑

时间:2014-08-28 17:02:23

标签: java android android-activity android-fragments android-asynctask

我正在运行标签寻呼机适配器,但我似乎无法理解如何实现不在主要活动中的代码。我制作的一些代码要求我扩展Activity使得它不可能,因为我已经扩展了片段。有谁可以启发我这个?

public class Home extends Fragment{
    ListView list;
    String url = "http://blog.compassion.com/living-in-the-philippines-rural-life/";
    ProgressDialog mProgressDialog;

    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        TextView titlebutton = (TextView) getActivity().findViewById(R.id.TextView01);
        titlebutton.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                new Title().execute();
                }
        });
        return inflater.inflate(R.layout.home_fragment, container);
    }


    private class Title extends AsyncTask<Void, Void, Void> {
        String body;
        String title;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setTitle("Android Basic JSoup Tutorial");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Document document = Jsoup.connect(url).get();
                Elements elements = document.select("p");
                title = document.title();
                for(Element elements123 : elements){
                    body+=elements123.text();
                    System.out.println(elements123.text());

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
            protected void onPostExecute(Void result) {
                TextView txttbody = (TextView) getActivity().findViewById(R.id.textView2);
                txttbody.setMovementMethod(new ScrollingMovementMethod());
                txttbody.setText(title +"\n"+body);
                mProgressDialog.dismiss();
            }
    }

2 个答案:

答案 0 :(得分:0)

要在主类中使用此viewPager,您需要扩展FragmentActivity类。 viewPager活动不是片段,但viewPager的每个选项卡内的页面都是片段,因此您使用FragmentActivity来控制它们。此外,请确保在可用时使用android.support.v4版本的导入。

答案 1 :(得分:0)

这解决了NPE

中的onCreatView
public View onCreateView(LayoutInflater inflater,
                @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
              view v = inflater.inflate(R.layout.home_fragment, container, false);
            // TODO Auto-generated method stub
            TextView titlebutton = (TextView) v.findViewById(R.id.TextView01);
            titlebutton.setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                    new Title().execute();
                    }
            });
            return v;
        }