Android:由于ViewPager适配器,活动崩溃了

时间:2016-05-27 07:08:09

标签: java android android-layout android-studio

查看寻呼机适配器是不是正常工作,我发布我的代码,我在其下面写了不同的文件,如.java和.xml 善意解决问题。感谢帮助者。

    package com.example.abbas.exmpression;
    import android.app.Activity;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    import android.content.Context;
    import android.support.v4.view.LayoutInflaterCompat;
    import android.support.v4.view.PagerAdapter;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    public class view_pager2 extends PagerAdapter {

    private int[] image_resources= 
    {R.drawable.centre,R.drawable.left,
    R.drawable.right,R.drawable.face_guide_center};
    private Context ctx;
    private LayoutInflater layoutInflater;
    public view_pager2(Context ctx){
       this.ctx= ctx;
    }
    @Override
    public int getCount() {
        return image_resources.length;
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view ==(LinearLayout)object);
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position){
    layoutInflater= (LayoutInflater) 
    ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View item_view = 
    layoutInflater.inflate(R.layout.activity_third_activity 
    ,container,false);
    ImageView imageView = (ImageView) 
    item_view.findViewById(R.id.cam_images);
    TextView textView= (TextView)
    item_view.findViewById(R.id.act3_txt2);
    imageView.setImageResource(image_resources[position]);
    if(position==0)
    {
        textView.setText("abc");
    }
    else if(position==1)
    {
        textView.setText("bla bla ");
    }
    else if(position==2)
    {
        textView.setText("ha ha ha ");
    }
    else
    {
        textView.setText("akjsdkasjdkl");
    }
    container.addView(item_view);
    return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object 
object) {

    container.removeView((LinearLayout)object);
    }
}

third_activity.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"
    >
    <android.support.v4.view.ViewPager
        android:id="@+id/screen3"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        >
    </android.support.v4.view.ViewPager>
    <TextView
        android:id="@+id/act3_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show us your face, &amp; some moves!"
        android:textSize="35sp"
        android:textAlignment="center"
        android:paddingBottom="20dp"
        />
    <ImageView
        android:id="@+id/cam_images"
        android:layout_height="250sp"
        android:layout_width="wrap_content"
        android:src="@drawable/centre"
        android:layout_below="@+id/act3_txt1"
        android:paddingBottom="10dp"
        />
    <TextView
        android:id="@+id/act3_txt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Align your face &amp; hold to start Recording"
        android:layout_below="@+id/cam_images"
        android:textSize="35sp"
        android:textAlignment="center"
        />
</RelativeLayout>

third_activity.java 所以这里适配器=新的view_pager2(这)产生了一个问题,我没有得到它是什么。需要帮助。

public class third_activity extends Activity
{
    ViewPager viewPager;
    view_pager2 adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(getWindow().FEATURE_NO_TITLE);
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_third_activity);
        viewPager= (ViewPager)findViewById(R.id.screen3);
        adapter= new view_pager2(this);
        viewPager.setAdapter(adapter);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个, 在您的活动中

adapter= new view_pager2(third_activity.this);

在您的适配器类

Activity ctx;
  public view_pager2(Activity ctx){
       this.ctx= ctx;
    }