我有这个logcat,我不知道我的错误是什么。我不知道我的错误在哪里。
http://pastebin.com/jzCZKWKG< - LOGCAT
我使用适配器的viewpage指示器。这是我的代码
Activity3
package com.example.jcw;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import com.viewpagerindicator.UnderlinePageIndicator;
public class activity3 extends Activity
{
ViewPager viewPager;
PagerAdapter adapter;
int[] flag;
String[] rank;
UnderlinePageIndicator mIndicator;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app3);
rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14" };
flag = new int[] { R.drawable.gr, R.drawable.calvitcdetails,
R.drawable.barleydetails, R.drawable.hilifedetails,
R.drawable.kapedetails, R.drawable.glutafitdetails, R.drawable.omnidaydetails,
R.drawable.omnipinkishdetails, R.drawable.omnisoapdetails, R.drawable.omnitonerdetails
, R.drawable.organicbarleyjuicedetails, R.drawable.pgtdetails, R.drawable.shuyadetails
, R.drawable.spirulinadetails };
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(activity3.this, flag , rank);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
// ViewPager Indicator
mIndicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
mIndicator.setFades(false);
mIndicator.setViewPager(viewPager);
}
}
ViewPageAdapter
package com.example.jcw;
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.RelativeLayout;
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
int[] flag;
String[] rank;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, int[] flag, String[] rank) {
this.flag = flag;
this.rank = rank;
}
@Override
public int getCount() {
return rank.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgflag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.app3x, container,
false);
// Locate the ImageView in viewpager_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.imageView);
// Capture position and set to the ImageView
imgflag.setImageResource(flag[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
app3 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.viewpagerindicator.UnderlinePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="5dp" />
</LinearLayout>
app3x XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp" >
<ImageView
android:id="@+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp" />
</RelativeLayout>
答案 0 :(得分:0)
context
中的 ViewPagerAdapter
为空,导致NullPointerException
位于:
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
按如下方式更改构造函数:
public ViewPagerAdapter(Context context, int[] flag, String[] rank) {
this.context = context;
this.flag = flag;
this.rank = rank;
}
您还有一个不正确的ImageView ID。变化:
imgflag = (ImageView) itemView.findViewById(R.id.imageView);
为:
imgflag = (ImageView) itemView.findViewById(R.id.flag);