Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我正在尝试在Android中创建一个滑动视图_Android - Fatal编程技术网

我正在尝试在Android中创建一个滑动视图

我正在尝试在Android中创建一个滑动视图,android,Android,程序运行时没有错误。但当我尝试运行应用程序时,它显示不幸停止。我的调试器不工作。 代码如下 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_paren

程序运行时没有错误。但当我尝试运行应用程序时,它显示不幸停止。我的调试器不工作。 代码如下

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent" android:layout_height="match_parent">

</ViewPager>

 </LinearLayout>
fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
   android:background="#FFCC00"
   android:layout_height="match_parent">
 </LinearLayout>

类似地,又创建了三个片段及其xml。

我刚刚复制了该场景,以便更快地确定错误所在,我注意到在您的“activity_main”中,您需要使用支持库中的ViewPager,因此更改您在xml中声明它的方式:

    <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   </android.support.v4.view.ViewPager>

发生这种情况的原因是,在MainActivity中,当您创建ViewPager时,您从支持库中选择了一个ViewPager,并且可以看到与之匹配的导入。但是,稍后您将一个常规的ViewPager从XML传递到您创建的那个,但它们不匹配。很好,你正在使用v4…一

现在应用程序运行良好。您可以自己识别它的方法是在运行应用程序后,在应用程序崩溃后,您可以在

安卓显示器

你会看到一打红线,如果你滚动到它们的开头,有时你可以很容易地确定你的问题是什么。在本例中,我看到它正在给ViewPager充气,然后我注意到您没有使用正确的ViewPager。祝你好运,如果你在YouTube或这里遇到困难,这里有关于ViewPager的信息,请仔细阅读

package com.example.pathak.swipe;

 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;

 /**
 * Created by Pathak on 2/3/2016.
 */
  public class FragmentA extends Fragment {    

    @Override

   public View onCreateView(LayoutInflater layoutInflater,ViewGroup container,Bundle savedInstanceState)
{
    return layoutInflater.inflate(R.layout.fragment_a,container,false);
}
}
    <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   </android.support.v4.view.ViewPager>