android viewpager无法正常工作

时间:2015-10-29 08:09:19

标签: android android-viewpager

我在片段中使用了一个viewpager。但是当我尝试运行代码时,我得到NullPointerException.Below是代码

ImageAdapter.java:

package com.example.sabudaniel61.ktdc;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by Sabudaniel61 on 2015-10-29.
 */
public class ImageAdapter extends PagerAdapter {
private int images[]=new int[]{R.drawable.slide2,R.drawable.room1,R.drawable.room2};
   private  Context context;
    ImageAdapter(Context context){
        this.context=context;
    }
    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater=(LayoutInflater) new MascotFragment().getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v=inflater.inflate(R.layout.mascotimages,container,false);
        ImageView imageView=(ImageView) v.findViewById(R.id.imageView1);
        imageView.setImageResource(images[position]);
        ((ViewPager) container).addView(v);
        return v;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((LinearLayout) object);
    }



    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==(LinearLayout) object;
    }
}

mascotlayout.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:scrollbars="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:id="@+id/text"
        android:textColor="#424242"
        android:text="Address"
        android:textSize="30sp"
        android:layout_below="@+id/viewpager"
        android:layout_centerHorizontal="true"
       />
        <View
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_below="@+id/text"
            android:background="#9FA8DA"></View>
        <android.support.v7.widget.CardView
            android:layout_height="150dp"
            android:layout_width="match_parent"
            android:layout_below="@+id/text"
            android:layout_margin="8dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:id="@+id/cardView"
            ><RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"



            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#80000000"
                android:inputType="textMultiLine"
                android:textSize="25sp"
                android:id="@+id/text1"
                android:gravity="center"
                android:layout_marginTop="30dp"
                android:text="Aranya Nivas\nThekkady\nIdukki 685536"

                /></RelativeLayout></android.support.v7.widget.CardView>
        <ImageButton
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentLeft="true"
            android:id="@+id/imageButton"
           android:background="@drawable/calls"

            android:clickable="true"

            android:layout_marginLeft="30dp"
            android:layout_below="@+id/cardView"
           />
        <ImageButton
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:id="@+id/imageButton1"

        android:background="@drawable/mails"


            android:layout_below="@+id/cardView"

          />
        <ImageButton
            android:layout_width="48dp"
            android:layout_height="48dp"

            android:layout_below="@+id/cardView"
            android:layout_marginRight="30dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/location"


            android:id="@+id/imageButton2"
           ></ImageButton>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:id="@+id/text3"
            android:elevation="3dp"
            android:textColor="#f9fff6"
            android:background="@drawable/shape"
           android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="890dp"

            android:text=" Conducted Tours >> "
             />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text2"
            android:text="Facilities"
            android:layout_marginLeft="20dp"
            android:textSize="25sp"
            android:layout_marginTop="200dp"
            android:layout_below="@+id/text3"
            android:textColor="#000000" />
        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_below="@+id/text2"
            android:background="#40000000"
            android:layout_marginTop="13dp"
            android:id="@+id/image24"
            android:layout_marginLeft="20dp"/>
        <TextView
            android:layout_width="170dp"
            android:layout_height="20dp"
            android:textColor="#80000000"
            android:text="Ayurveda therapies"
            android:layout_marginLeft="10dp"
            android:layout_below="@+id/text2"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/image24"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text19"
            android:layout_below="@+id/text2"
            android:text="Attractions"
            android:textSize="25sp"
            android:textColor="#000000"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="30dp"/>
        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:src="@android:drawable/alert_dark_frame"
            android:id="@+id/image16"
            android:layout_toLeftOf="@+id/text20"

            android:layout_marginTop="390dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Kerala Legislature Complex"
            android:id="@+id/text20"
            android:textColor="#80000000"
            android:layout_below="@+id/text19"
            android:layout_marginLeft="40dp"
            />
        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:background="#40000000"
            android:id="@+id/image17"
            android:layout_marginLeft="20dp"
            android:layout_below="@+id/text19"
            android:layout_marginTop="3dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Science and Technology Museum Complex"
            android:id="@+id/text21"
            android:textColor="#80000000"
            android:layout_below="@+id/text20"
            android:layout_marginLeft="40dp"
            />
        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_below="@+id/image17"
            android:id="@+id/image18"
            android:layout_marginTop="7dp"
            android:layout_marginLeft="20dp"
            android:background="#40000000"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text21"
            android:id="@+id/text22"
            android:text="Priyadarshini Planetarium"

            android:layout_marginLeft="40dp"/>
        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
           android:layout_marginLeft="20dp"
            android:background= "#40000000"
            android:id="@+id/image19"
            android:layout_marginTop="7dp"
            android:layout_below="@+id/image18"
            />
        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_marginLeft="20dp"
            android:background= "#40000000"
            android:id="@+id/image28"
            android:layout_marginTop="7dp"
            android:layout_below="@+id/image19"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:id="@+id/text23"
            android:text="The Napier Museum"
            android:layout_below="@+id/text22"/>



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_marginTop="20dp"
    android:layout_below="@+id/text23"
    android:layout_alignParentLeft="true"
    android:id="@+id/text24"
    android:textSize="25sp"
    android:textColor="#000000"
    android:layout_marginLeft="20dp"
    android:text="Activities"/>
        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:src="@android:drawable/alert_dark_frame"
            android:layout_below="@+id/text24"
            android:id="@+id/image20"
            android:layout_marginTop="2dp"
           />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text24"
    android:text="Morning and evening walks inside the property or in the adjaecent museum compound and kanakakkunu Palace Compound"
    android:layout_marginLeft="20dp"
    android:id="@+id/text25"
      />
        <ImageView android:layout_width="15dp"
            android:layout_height="15dp"
            android:id="@+id/image21"
            android:layout_below="@+id/text25"

            android:src="@android:drawable/alert_dark_frame"
            >
        </ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text25"
            android:id="@+id/text26"
            android:text="Visit museum ,zoo, art gallery and other places of historical importance"
            android:layout_marginLeft="20dp"/>
        <ImageView android:layout_width="15dp"
            android:layout_height="15dp"
            android:id="@+id/image22"
            android:layout_below="@+id/image21"
            android:src="@android:drawable/alert_dark_frame"
            android:layout_marginTop="19dp"
            >
        </ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text26"
            android:id="@+id/text27"
            android:text="Visit beaches(Kovalam and Shanghumugham)"
            android:layout_marginLeft="20dp"/>
        <ImageView android:layout_width="15dp"
            android:layout_height="15dp"
            android:id="@+id/image23"
            android:layout_below="@+id/image22"
            android:src="@android:drawable/alert_dark_frame"
            >
        </ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text27"
            android:id="@+id/text28"
            android:text="Visit backwaters(Veli and Akkulam)"
            android:layout_marginLeft="20dp"/>
        <Button
            android:layout_width="15dp"
            android:layout_height="30dp"
            android:background="@drawable/shape"
            android:layout_alignParentRight="true"
            android:text="Know more >>>"
            android:id="@+id/button3"
            android:textColor="#f9fff6"
            android:layout_below="@+id/text28"
            android:layout_toEndOf="@+id/imageButton1" />
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#a8a8bd"
            android:layout_below="@+id/text28"
            android:layout_marginTop="50dp"
            android:id="@+id/view5"></View>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gallery"
            android:id="@+id/gallery"
            android:textSize="35sp"
            android:textColor="#52ff61"
            android:layout_below="@+id/button3"
            android:layout_marginTop="20dp"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_below="@+id/gallery"
            android:id="@+id/view6"
            android:layout_marginTop="20dp"
            android:background="#a8a8bd"
            ></View>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Accesibility"
            android:textSize="35sp"
            android:id="@+id/access"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/view6"
           android:textColor="#52ff61"
            />
        <ImageView android:layout_width="15dp"
            android:layout_height="15dp"
            android:id="@+id/image25"
            android:layout_marginTop="2dp"
            android:layout_below="@+id/access"
            android:src="@android:drawable/alert_dark_frame"
            >
        </ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/access"
            android:id="@+id/text29"
            android:text="Nearest Bus Terminal:Thiruvananthapuram, about 4 km"
            android:layout_marginLeft="20dp"/>
        <ImageView android:layout_width="15dp"
            android:layout_height="15dp"
            android:id="@+id/image26"

            android:layout_below="@+id/image25"
            android:src="@android:drawable/alert_dark_frame"
            >
        </ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text29"
            android:id="@+id/text30"
            android:text="Nearest railway Terminal:Thiruvananthapuram, about 4 km"
            android:layout_marginLeft="20dp"/>
        <ImageView android:layout_width="15dp"
            android:layout_height="15dp"
            android:id="@+id/image27"
            android:layout_below="@+id/image26"
            android:layout_marginTop="2dp"
            android:src="@android:drawable/alert_dark_frame"
            >
        </ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text30"
            android:id="@+id/text31"
            android:text="Nearest airport:Thiruvananthapuram International Airport, about 7 km"
            android:layout_marginLeft="20dp"/>
    </RelativeLayout>
</ScrollView>

MainActivity.java:

我只发布两个代码,因为它包含很多其他代码

 ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        ImageAdapter imageadapter = new ImageAdapter(this);
        viewPager.setAdapter(imageadapter);

mascotimages.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">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:src="@drawable/slide2" android:adjustViewBounds="true"
        android:scaleType="fitXY"

        android:contentDescription="slide2"/>

</LinearLayout>

错误:

>  Caused by: java.lang.NullPointerException: Attempt to invoke virtual
> method 'void
> android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'
> on a null object reference
>             at com.example.sabudaniel61.ktdc.MainActivity.onCreate(MainActivity.java:34)
>             at android.app.Activity.performCreate(Activity.java:5933)
>             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
>             at android.app.ActivityThread.access$800(ActivityThread.java:144)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
>             at android.os.Handler.dispatchMessage(Handler.java:102)
>             at android.os.Looper.loop(Looper.java:135)
>             at android.app.ActivityThread.main(ActivityThread.java:5221)
>             at java.lang.reflect.Method.invoke(Native Method)
>             at java.lang.reflect.Method.invoke(Method.java:372)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 10-29
> 03:35:27.166    2500-2500/com.example.sabudaniel61.ktdc I/Process﹕
> Sending signal. PID: 2500 SIG: 9

0 个答案:

没有答案