有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我的viewpager实现在片段/活动方面让我很困惑

我正在运行一个选项卡寻呼机适配器,但我似乎无法理解如何实现不在主活动中的代码。我编写的一些代码要求我扩展活动,使之不可能,因为我已经扩展了片段。有人能给我一些启发吗

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) 个答案

  1. # 1 楼答案

    要在主类中使用此viewPager,需要扩展类FragmentActivity。viewPager活动不是片段,但viewPager的每个选项卡内的页面都是片段,因此可以使用FragmentActivity来控制它们。另外,确保你使用安卓系统。支持v4版本的导入,只要它们可用

  2. # 2 楼答案

    这将解决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;
            }