<i id='bCVvv'><tr id='bCVvv'><dt id='bCVvv'><q id='bCVvv'><span id='bCVvv'><b id='bCVvv'><form id='bCVvv'><ins id='bCVvv'></ins><ul id='bCVvv'></ul><sub id='bCVvv'></sub></form><legend id='bCVvv'></legend><bdo id='bCVvv'><pre id='bCVvv'><center id='bCVvv'></center></pre></bdo></b><th id='bCVvv'></th></span></q></dt></tr></i><div id='bCVvv'><tfoot id='bCVvv'></tfoot><dl id='bCVvv'><fieldset id='bCVvv'></fieldset></dl></div>

  1. <tfoot id='bCVvv'></tfoot>
      <legend id='bCVvv'><style id='bCVvv'><dir id='bCVvv'><q id='bCVvv'></q></dir></style></legend>
      • <bdo id='bCVvv'></bdo><ul id='bCVvv'></ul>
    1. <small id='bCVvv'></small><noframes id='bCVvv'>

    2. android ViewPager xml膨胀错误

      时间:2023-10-04
      <tfoot id='0ZsOa'></tfoot>

        <legend id='0ZsOa'><style id='0ZsOa'><dir id='0ZsOa'><q id='0ZsOa'></q></dir></style></legend>

          • <bdo id='0ZsOa'></bdo><ul id='0ZsOa'></ul>

            <small id='0ZsOa'></small><noframes id='0ZsOa'>

              <i id='0ZsOa'><tr id='0ZsOa'><dt id='0ZsOa'><q id='0ZsOa'><span id='0ZsOa'><b id='0ZsOa'><form id='0ZsOa'><ins id='0ZsOa'></ins><ul id='0ZsOa'></ul><sub id='0ZsOa'></sub></form><legend id='0ZsOa'></legend><bdo id='0ZsOa'><pre id='0ZsOa'><center id='0ZsOa'></center></pre></bdo></b><th id='0ZsOa'></th></span></q></dt></tr></i><div id='0ZsOa'><tfoot id='0ZsOa'></tfoot><dl id='0ZsOa'><fieldset id='0ZsOa'></fieldset></dl></div>
                <tbody id='0ZsOa'></tbody>
                本文介绍了android ViewPager xml膨胀错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在学习如何实现水平滑动,但在尝试启动布局中包含 ViewPager 的应用时遇到以下错误.

                I'm learning how to implement horizontal swiping and I'm getting the following error while trying to launch my app having ViewPager in its layout.

                03-25 17:12:13.166: E/AndroidRuntime(19449): FATAL EXCEPTION: main
                03-25 17:12:13.166: E/AndroidRuntime(19449): java.lang.RuntimeException: Unable to start activity ComponentInfo{androidapps.viewpagerdemo/androidapps.viewpagerdemo.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.v4.app.ViewPager
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.os.Handler.dispatchMessage(Handler.java:99)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.os.Looper.loop(Looper.java:137)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.main(ActivityThread.java:4745)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at java.lang.reflect.Method.invokeNative(Native Method)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at java.lang.reflect.Method.invoke(Method.java:511)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at dalvik.system.NativeStart.main(Native Method)
                03-25 17:12:13.166: E/AndroidRuntime(19449): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.v4.app.ViewPager
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.Activity.setContentView(Activity.java:1867)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at androidapps.viewpagerdemo.MainActivity.onCreate(MainActivity.java:14)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.Activity.performCreate(Activity.java:5008)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
                03-25 17:12:13.166: E/AndroidRuntime(19449):    ... 11 more
                

                我尝试了此链接中的解决方案,但它对我不起作用.错误膨胀类android.support.v4.view.ViewPager

                I tried out the solution in this link but it didn't work for me. Error inflating class android.support.v4.view.ViewPager

                activity_main.xml

                activity_main.xml

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context=".MainActivity" >
                
                <android.support.v4.app.ViewPager 
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    />
                
                </RelativeLayout>
                

                片段的布局文件只包含一个TextView.片段类如下:

                The layout file of the fragment consists of simply one TextView. The fragment class is as follows:

                public class MyFragment extends Fragment{
                int mCurrentPage;
                
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                
                    /** Getting the arguments to the Bundle object */
                    Bundle data = getArguments();
                
                    /** Getting integer data of the key current_page from the bundle */
                    mCurrentPage = data.getInt("current_page", 0);
                
                }
                
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View v = inflater.inflate(R.layout.frag_layout, container,false);
                    TextView tv = (TextView ) v.findViewById(R.id.tv);
                    tv.setText("You are viewing the page #" + mCurrentPage + "
                
                " + "Swipe Horizontally left / right");
                    return v;
                }
                }
                

                FragmentPagerAdapter 类:

                The FragmentPagerAdapter class:

                public class MyAdapter extends FragmentPagerAdapter{
                
                final int PAGE_COUNT = 3;
                public MyAdapter(FragmentManager fm) {
                    super(fm);
                    // TODO Auto-generated constructor stub
                }
                
                @Override
                public Fragment getItem(int arg0) {
                    // TODO Auto-generated method stub
                    MyFragment myFragment = new MyFragment();
                    Bundle data = new Bundle();
                    data.putInt("current_page", arg0+1);
                    myFragment.setArguments(data);
                    return myFragment;
                }
                
                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return PAGE_COUNT;
                }
                }
                

                主要活动:

                public class MainActivity extends FragmentActivity {
                
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    ViewPager pager = (ViewPager) findViewById(R.id.pager);
                
                    /** Instantiating FragmentPagerAdapter */
                    MyAdapter pagerAdapter = new MyAdapter(getSupportFragmentManager());
                
                    /** Setting the pagerAdapter to the pager object */
                    pager.setAdapter(pagerAdapter);
                }
                
                @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;
                }
                
                }
                

                推荐答案

                您好,我刚刚对您的布局文件进行了一些更改 &它现在工作正常..

                Hi i have just made some change in your layout file & its working fine now..

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="10dp" >
                
                    <android.support.v4.view.ViewPager
                        android:id="@+id/pager"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" />
                
                </RelativeLayout>
                

                你可以检查一下:

                以下代码仅供参考---->

                below code is just for your refrance---->

                你的主要活动:

                package com.example.viewpage;
                
                import android.app.Activity;
                import android.os.Bundle;
                import android.support.v4.app.FragmentActivity;
                import android.support.v4.view.ViewPager;
                import android.view.Menu;
                
                public class MainActivity extends FragmentActivity {
                
                    @Override
                    public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                
                        ViewPager pager = (ViewPager) findViewById(R.id.pager);
                
                        /** Instantiating FragmentPagerAdapter */
                        MyAdapter pagerAdapter = new MyAdapter(getSupportFragmentManager());
                
                        /** Setting the pagerAdapter to the pager object */
                        pager.setAdapter(pagerAdapter);
                    }
                
                    @Override
                    public boolean onCreateOptionsMenu(Menu menu) {
                        getMenuInflater().inflate(R.menu.activity_main, menu);
                        return true;
                    }
                }
                

                我的适配器:

                package com.example.viewpage;
                
                import android.os.Bundle;
                import android.support.v4.app.Fragment;
                import android.support.v4.app.FragmentManager;
                
                public class MyAdapter extends android.support.v4.app.FragmentPagerAdapter {
                
                    final int PAGE_COUNT = 3;
                
                    public MyAdapter(FragmentManager fm) {
                        super(fm);
                        // TODO Auto-generated constructor stub
                    }
                
                    @Override
                    public Fragment getItem(int arg0) {
                        // TODO Auto-generated method stub
                        MyFragment myFragment = new MyFragment();
                        Bundle data = new Bundle();
                        data.putInt("current_page", arg0 + 1);
                        myFragment.setArguments(data);
                        return myFragment;
                    }
                
                    @Override
                    public int getCount() {
                        // TODO Auto-generated method stub
                        return PAGE_COUNT;
                    }
                
                }
                

                我的片段:

                package com.example.viewpage;
                
                import android.os.Bundle;
                import android.support.v4.app.Fragment;
                import android.view.LayoutInflater;
                import android.view.View;
                import android.view.ViewGroup;
                import android.widget.TextView;
                
                public class MyFragment extends Fragment {
                
                    private int mCurrentPage;
                
                    @Override
                    public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                
                        /** Getting the arguments to the Bundle object */
                        Bundle data = getArguments();
                
                        /** Getting integer data of the key current_page from the bundle */
                        mCurrentPage = data.getInt("current_page", 0);
                
                    }
                
                    @Override
                    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
                        View v = inflater.inflate(R.layout.frag_layout, container, false);
                        TextView tv = (TextView) v.findViewById(R.id.textView1);
                        tv.setText("You are viewing the page #" + mCurrentPage + "
                
                "
                                + "Swipe Horizontally left / right");
                        return v;
                    }
                }
                

                frag_layout.xml

                frag_layout.xml

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
                
                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Large Text"
                        android:textAppearance="?android:attr/textAppearanceLarge" />
                
                </LinearLayout>
                

                注意: 在 lib 文件夹中添加支持 android-support-v4.jar.在 Build Path 中添加这个 jar.现在转到项目属性-->构建路径-->订单&出口-->全选 -->好的.清洁&运行.

                NOTE: add support android-support-v4.jar in lib folder. add this jar in Build Path. now go to project property-->Build Path--> Order & Export--> select all --> ok. Clean & RUN.

                这篇关于android ViewPager xml膨胀错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在另一个 ViewPager2 中使用 ViewPager2 下一篇:如何在片段中使用 ViewPager

                相关文章

              1. <i id='c9tMA'><tr id='c9tMA'><dt id='c9tMA'><q id='c9tMA'><span id='c9tMA'><b id='c9tMA'><form id='c9tMA'><ins id='c9tMA'></ins><ul id='c9tMA'></ul><sub id='c9tMA'></sub></form><legend id='c9tMA'></legend><bdo id='c9tMA'><pre id='c9tMA'><center id='c9tMA'></center></pre></bdo></b><th id='c9tMA'></th></span></q></dt></tr></i><div id='c9tMA'><tfoot id='c9tMA'></tfoot><dl id='c9tMA'><fieldset id='c9tMA'></fieldset></dl></div>

                <small id='c9tMA'></small><noframes id='c9tMA'>

                    <bdo id='c9tMA'></bdo><ul id='c9tMA'></ul>

                  <tfoot id='c9tMA'></tfoot>

                  1. <legend id='c9tMA'><style id='c9tMA'><dir id='c9tMA'><q id='c9tMA'></q></dir></style></legend>