我想问一下,每当我到达int []数组中的最后一个对象时,我将如何处理NullException。
这是我的InformationTab
package com.example.aldrinjohn.milktracktest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Aldrin John on 3/20/2017.
*/
public class InformationTab extends Fragment{
ViewPager viewPager;
CustomSwipeAdapter adapter;
Button btnTrivia,btnBenefitB,btnBenefitM,btnPump,btnStoring,btnInfo;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.informationtab, container, false);
viewPager = (ViewPager)rootView.findViewById(R.id.view_pager);
btnTrivia = (Button)rootView.findViewById(R.id.trivia);
btnTrivia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GlobalVariables.buttonClick = "trivia";
adapter = new CustomSwipeAdapter(getActivity());
viewPager.setAdapter(adapter);
}
});
btnBenefitB = (Button)rootView.findViewById(R.id.benefit_babies);
btnBenefitB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GlobalVariables.buttonClick = "benefitb";
adapter = new CustomSwipeAdapter(getActivity());
viewPager.setAdapter(adapter);
}
});
btnBenefitM = (Button)rootView.findViewById(R.id.benefit_mother);
btnBenefitM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GlobalVariables.buttonClick = "benefitm";
adapter = new CustomSwipeAdapter(getActivity());
viewPager.setAdapter(adapter);
}
});
btnPump = (Button)rootView.findViewById(R.id.pump);
btnPump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GlobalVariables.buttonClick = "pump";
adapter = new CustomSwipeAdapter(getActivity());
viewPager.setAdapter(adapter);
}
});
btnStoring = (Button)rootView.findViewById(R.id.storing);
btnStoring.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GlobalVariables.buttonClick = "storing";
adapter = new CustomSwipeAdapter(getActivity());
viewPager.setAdapter(adapter);
}
});
btnInfo = (Button)rootView.findViewById(R.id.tips);
btnInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GlobalVariables.buttonClick = "tips";
adapter = new CustomSwipeAdapter(getActivity());
viewPager.setAdapter(adapter);
}
});
return rootView;
}
}
这是我的
package com.example.aldrinjohn.milktracktest;
import android.content.Context;
import android.provider.ContactsContract;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.google.android.gms.ads.formats.NativeAd;
import com.google.android.gms.tagmanager.Container;
/**
* Created by Aldrin John on 3/26/2017.
*/
public class CustomSwipeAdapter extends PagerAdapter{
private int[] trivia = {R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4, R.drawable.c5,R.drawable.c6};
private int[] benefitBaby = {R.drawable.c7,R.drawable.c8,R.drawable.c9};
private int[] benefitMother = {R.drawable.c91,R.drawable.c92};
private int[] info = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,
R.drawable.a8,R.drawable.a9,R.drawable.a91,R.drawable.a92};
private int[] storing = {R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4};
private int[] pump = {R.drawable.a21,R.drawable.a22,R.drawable.a23,R.drawable.a24,R.drawable.a25,R.drawable.a31,R.drawable.a32,
R.drawable.a33,R.drawable.a34,R.drawable.a35,R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomSwipeAdapter(Context ctx)
{
this.ctx = ctx;
}
@Override
public int getCount() {
return pump.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);
final View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
ImageView imgTrivia = (ImageView)item_view.findViewById(R.id.image_view);
if(GlobalVariables.buttonClick.equals("trivia"))
{
imgTrivia.setImageResource(trivia[position]);
}
else if (GlobalVariables.buttonClick.equals("benefitb"))
{
imgTrivia.setImageResource(benefitBaby[position]);
}
else if (GlobalVariables.buttonClick.equals("benefitm"))
{
imgTrivia.setImageResource(benefitMother[position]);
}
else if (GlobalVariables.buttonClick.equals("storing"))
{
imgTrivia.setImageResource(storing[position]);
}
else if (GlobalVariables.buttonClick.equals("tips"))
{
imgTrivia.setImageResource(info[position]);
}
else if (GlobalVariables.buttonClick.equals("pump"))
{
imgTrivia.setImageResource(pump[position]);
}
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object){
container.removeView((LinearLayout)object);
}
}
答案 0 :(得分:0)
解决了!我刚刚使用了一个全局变量类,其中包含单击的幻灯片数。并将其返回给getCount函数。