【问题标题】:my implementation of viewpager confused me a lot on fragment/activities我对 viewpager 的实现在片段/活动上让我很困惑
【发布时间】:2014-08-28 17:02:23
【问题描述】:

我正在运行一个选项卡寻呼机适配器,但我似乎无法理解如何实现不在 Main Activity 中的代码。我制作的一些代码要求我扩展 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();
            }
    }

【问题讨论】:

  • 你有什么问题吗,你只是告诉我们你的心情。有什么问题?
  • 我的代码不起作用。它在 onCreateView 部分有空指针。

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


【解决方案1】:

要在主类中使用此 viewPager,您需要扩展类 FragmentActivity。 viewPager Activity 不是 Fragment,但是 viewPager 的每个 tab 里面的页面都是 Fragment,所以你使用 FragmentActivity 来控制它们。此外,请确保在可用时使用 android.support.v4 版本的导入。

【讨论】:

  • 但是我应该如何在每个片段上设置 contentView 呢?每当我尝试在这些片段上编写一些函数时,它们似乎都没有反映。
  • 第 1 步:Home 扩展 FragmentActivity。第 2 步:获取操作栏。第 3 步:actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS 第 4 步:设置操作栏选项卡的侦听器。在布局文件中,您需要将 android.support.v4.view.ViewPager 作为根布局。您需要制作单独的内容视图来自每个片段。看看这个页面。developer.android.com/training/implementing-navigation/…
  • 我有一个 getItem 方法,它返回一个片段类型。它有一个开关,它根据索引返回一个片段。我该怎么办?
【解决方案2】:

这解决了您在onCreatView 中的NPE

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;
        }

【讨论】:

  • 它说指定的孩子已经有了父母?我不明白
  • 发布行号,但是 NPE 已经去了是吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多