通过无限片段轻扫 - 在ViewPager进程中混淆

时间:2016-08-23 21:18:37

标签: android android-fragments

我正在尝试创建一个应用程序,从api中提取这些Ron Swanson引号并将其显示在片段中。当片段被刷过时,应用程序将转到另一个片段并显示新的引用。这个过程应该无限期重复。

相反,该应用加载到空白屏幕。我在片段上放置了一个按钮和TextView,每个按钮都有初始文本以确定它是否有负载,但是什么也没有出现。

P.S。:您可能还会注意到一些滑稽的变量名称和注释。我显然没有考虑到写作时我可能需要帮助(它只是一个学习的个人应用程序)。

P.P.S。:老实说,我不确定该网站是否仍然存在,但这不应该对此产生任何影响。

感谢任何可以提供帮助的人。下面是应用2)activity_main.xml文件的单个java文件。

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public class MainActivity extends FragmentActivity {

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

            //
            ViewPager pagingRon = (ViewPager) findViewById(R.id.pager);
            PagerAdapter RonsPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager());
            pagingRon.setAdapter(RonsPagerAdapter);
            pagingRon.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TOxDO Auto-generated method stub

                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub

                }
            });

            //Initial AsyncTask for setting up first quote of the Great Swan
            RonSwanSetup ronSwansonOG = new RonSwanSetup();
            ronSwansonOG.execute();
        }

        private class RonSwanSetup extends AsyncTask<Void, String, Void> {

            @Override
            protected Void doInBackground(Void... Socialism) {
                String Ronquoteson = ""; //Ron's wonderful first quote
                String Rondotcom = "http://ron-swanson-quotes.herokuapp.com/quotes"; //the website for fetching Ron's wonderful quotes
                String[] Ronsquotes = new String[1]; //Ron only speaks seldomly, which is the pinnacle of wisdom


                try {
                    URL Ronsurl = new URL(Rondotcom);
                    HttpURLConnection Ronsconnection = (HttpURLConnection) Ronsurl.openConnection();

                    BufferedReader thanksRon = new BufferedReader(new InputStreamReader(Ronsconnection.getInputStream()));
                    String current;

                    while((current = thanksRon.readLine()) != null)
                    {
                        Ronquoteson += current;
                    }

                    Ronsquotes[0] = Ronquoteson;

                } catch (Exception sorryRon) {
                    Ronsquotes[0] = "The app appears unable to do even the SIMPLEST THINGS CORRECTLY - Maybe try restarting it?";
                    sorryRon.printStackTrace();
                }

                publishProgress(Ronsquotes);

                return null;
            }

            protected void onProgressUpdate(String... Ronsquote) {
                TextView RonsText = (TextView) findViewById(R.id.text);

                String RonQouteson = Ronsquote[0];
                RonsText.setText("Not Ron's Quote");//For testing

            }
        }

        public class CustomPagerAdapter extends FragmentPagerAdapter {

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

            @Override
            // This method returns the fragment associated with
            // the specified position.
            // It is called when the Adapter needs a fragment
            // and it does not exist.
            public Fragment getItem(int position) {

                // Create fragment object
                RonFragSon fragment = new RonFragSon();

                // Attach some data to it that we'll
                // use to populate our fragment layouts
                Bundle args = new Bundle();
                args.putInt("page_position", position + 1);

                // Set the arguments on the fragment
                // that will be fetched
                fragment.setArguments(args);

                return fragment;
            }

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

        }

        public class RonFragSon extends Fragment {

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                // Inflate the layout resource that'll be returned
                View rootView = inflater.inflate(R.layout.swansons_fragment, container, false);

                // Get the arguments that was supplied when
                // the fragment was instantiated in the
                // CustomPagerAdapter
                Bundle args = getArguments();
                ((TextView) rootView.findViewById(R.id.text)).setText("Page");

                return rootView;
            }
        }
    }
    <HorizontalScrollView
        android:id="@+id/horizontal_Swangrid"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="40" 
        xmlns:android="http://schemas.android.com/apk/res/android">
        <!--   I don't think this is necessary, but don't want to get rid of it in case I'm wrong
        <LinearLayout
            android:id="@+id/Swanson_cant_container"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
            <TextView 
                android:id="@+id/RonSwan1stQuote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
           <TextView 
                android:id="@+id/RonSwan2ndQuote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView 
                android:id="@+id/RonSwan3rdQuote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>
    -->
        <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </HorizontalScrollView>

0 个答案:

没有答案