0

我目前有以下代码作为示例并稍作修改:

package com.boy.test;

import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

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

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Fragment fragment = new Facebook();
            Bundle args = new Bundle();
            args.putInt(Facebook.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return "Welcome".toUpperCase(l);
                case 1:
                    return "Facebook".toUpperCase(l);
                case 2:
                    return "Twitter".toUpperCase(l);
                case 3:
                    return "dashboard".toUpperCase(l);
            }
            return null;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class Facebook extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";
        private WebView faceBook;


        public Facebook() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_facebook, container, false);


            return rootView;
        }
    }

}

我想要做的是将 facebook 添加到第二个选项卡中。这是我在另一个类中的代码:

public class Facebook extends Activity {
    private WebView faceBook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facebook);
        faceBook = new WebView(this);
        faceBook.getSettings().setJavaScriptEnabled(true);
        final Activity act = this;
        faceBook.setWebViewClient(new WebViewClient(){
            public void onRecievedError(WebView view,int errorCode,String description,String failingUrl){
                Toast.makeText(act,description,Toast.LENGTH_SHORT).show();
            }
        });

        faceBook.loadUrl("http://m.facebook.com/pages/hi");
        setContentView(faceBook);
    }

我尝试简单地将其复制到我的静态 facebook 片段中并得到错误。我还尝试制作扩展片段活动而不是片段并得到更多错误。谁能建议我如何最好地解决这个问题?

谢谢你。

4

1 回答 1

0

首先,您不需要 Facebook 活动,您必须在 Facebook 类中重写Fragment'onCreateView()方法以返回WebView.

尝试这个,

您的片段应如下所示,

public final class MyTab extends Fragment {

    public static MyTab newInstance(int type) {
        MyTab tab = new MyTab();
        tab.mType = type;
        return fragment;
    }

    private int mType = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        /*
         * Create a web view here, and on the basis of mType decide what to load.
         * and return that web view. 
         */
    } 
}

你的适配器将是

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return MyTab.newInstance(position);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return "Welcome".toUpperCase(l);
            case 1:
                return "Facebook".toUpperCase(l);
            case 2:
                return "Twitter".toUpperCase(l);
            case 3:
                return "dashboard".toUpperCase(l);
        }
        return null;
    }
}
于 2013-08-12T22:49:01.087 回答