1

所以我正在尝试使用 viewpager 和片段寻呼机适配器来实现滑动视图/片段。convert_home 是我的主要 xml 文件,它具有 android.support.v4.view.PagerTitleStrip 和

temperature.xml 和velocity.xml 是我的另外两个观点。我不知道我哪里出错了。应用程序崩溃(加载时)

package app.converto;
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.widget.TextView;

public class ConverTo extends FragmentActivity {


    SectionsPagerAdapter mSectionsPagerAdapter;


    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
        setContentView(R.layout.converto_home);  
        mViewPager = (ViewPager) findViewById(R.id.pager);  

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.converto_home, menu);
        return true;
    }





    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int i) {
            switch(i){
            case 0:
                Fragment1 fragment = new Fragment1();  

                return fragment;

            case 1:
                        Fragment2 fragment2 = new Fragment2();  
                return fragment2;


            }
            defaultFragment fragment3 = new defaultFragment();  
            return fragment3;
        }

        @Override
        public int getCount() {
            return 2;
        }
//
//        @Override
//        public CharSequence getPageTitle(int position) {
//            switch (position) {
//                case 0: return getString(R.string.velocity);
//                case 1: return getString(R.string.temperature);
//                case 2: return getString(R.string.distance);
//            }
//            return null;
//        }
    }

        public static class Fragment1 extends Fragment{
            public Fragment1(){
            }
             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 

             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
                 //return inflater.inflate(R.layout.temperature, container, false);

                 View view = inflater.inflate(R.layout.temperature, container, false);  
                 TextView textView = (TextView) view.findViewById(R.id.sample);  
                 textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

        public static class Fragment2 extends Fragment{
            public Fragment2(){
            }

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 
             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
                 //return inflater.inflate(R.layout.velocity, container, false);

                 View view = inflater.inflate(R.layout.temperature, container, false);  
                 TextView textView = (TextView) view.findViewById(R.id.sample);  
                 textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

        public static class defaultFragment extends Fragment{
            public defaultFragment(){
            }//end constructor

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 

             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
                // return inflater.inflate(R.layout.temperature, container, false);




                 View view = inflater.inflate(R.layout.temperature, container, false);  
                 TextView textView = (TextView) view.findViewById(R.id.sample);  
                 textView.setText(getArguments().getString("title"));
                 return view;
             }//end oncreate




        }//end default fragment

    }
4

4 回答 4

1

嗯,一切似乎都很好,你面临的问题到底是什么?你可以说得更详细点吗?

编辑:哦,我想我知道你的问题是什么,我第一次没有看到。在您的 ConvertTo 类中,您在初始化它之前设置 mViewPager 的适配器。因此,将您的 OnCreate 方法更改为:(让我知道这是否是问题所在)

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    setContentView(R.layout.converto_home);  

    // Initialize it first otherwise it stays null
    mViewPager = (ViewPager) findViewById(R.id.pager); 

    //Then you set the adapter
    mViewPager.setAdapter(mSectionsPagerAdapter);  


}
于 2012-09-30T03:33:55.557 回答
1

我不确定这是否对任何人有帮助,但我一直在玩上面的示例(作为 Android 编程的菜鸟)并添加了 3 个片段部分并重新启用了 6 个选项的菜单。我还没有测试我自己的片段并将内容放入其中,但我希望如果其他人在我所在的位置,它也可能会受益,因为似乎很少有关于 SDK 示例工作流程的文档。这意味着理解体验可能需要更长的时间。

您将需要为菜单名称设置一些字符串,并准备好布局。

package uk.co.letscommunicate.manx.central;
// http://stackoverflow.com/questions/12658365/viewpager-and-fragment-pager-adapter-implementation

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
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.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
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
    public 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);

    }



    //unknown may be tab swipe
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
   }
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
    // unknown    





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

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

        @Override
        public Fragment getItem(int i) {
            switch(i){
            case 0:
                Fragment1 fragment1 = new Fragment1();
                return fragment1;

            case 1:
                Fragment2 fragment2 = new Fragment2();  
                return fragment2;            

            case 2:
                Fragment3 fragment3 = new Fragment3();  
                return fragment3;    

            case 3:
                Fragment4 fragment4 = new Fragment4();  
                return fragment4;    

            case 4:
                Fragment5 fragment5 = new Fragment5();  
                return fragment5; 

            case 5:
                Fragment6 fragment6 = new Fragment6();  
                return fragment6;
            }            
            DefaultFragment DefaultFragment = new DefaultFragment();  
            return DefaultFragment;
        }



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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0: return getString(R.string.title_section1);
                case 1: return getString(R.string.title_section2);
                case 2: return getString(R.string.title_section3);
                case 3: return getString(R.string.title_section4);
                case 4: return getString(R.string.title_section5);
                case 5: return getString(R.string.title_section6);
            }
            return null;
        }
    }

        public static class Fragment1 extends Fragment{
            public Fragment1(){
            }
             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 

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





                 View view = inflater.inflate(R.layout.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

        public static class Fragment2 extends Fragment{
            public Fragment2(){
            }

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 
             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {



                 View view = inflater.inflate(R.layout.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

        public static class Fragment3 extends Fragment{
            public Fragment3(){
            }

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 
             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {



                 View view = inflater.inflate(R.layout.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

        public static class Fragment4 extends Fragment{
            public Fragment4(){
            }

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 
             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {



                 View view = inflater.inflate(R.layout.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }        

        public static class Fragment5 extends Fragment{
            public Fragment5(){
            }

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 
             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {



                 View view = inflater.inflate(R.layout.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

        public static class Fragment6 extends Fragment{
            public Fragment6(){
            }

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 
             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {



                 View view = inflater.inflate(R.layout.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

        public static class DefaultFragment extends Fragment{
            public DefaultFragment(){
            }

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 
             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {



                 View view = inflater.inflate(R.layout.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }
             //end on create

        }//end default fragment

    }
于 2012-10-20T09:06:16.380 回答
0
mViewPager.setAdapter(mSectionsPagerAdapter);        
mViewPager = (ViewPager) findViewById(R.id.pager);  

交换上面的两行,如下所示:

mViewPager = (ViewPager) findViewById(R.id.pager); 
mViewPager.setAdapter(mSectionsPagerAdapter);      
于 2013-06-14T16:26:05.050 回答
0

textview 导致应用程序崩溃。我没有正确声明它。

于 2012-10-01T20:25:54.150 回答