我在我的Android应用程序中遇到此问题,因为“该字段的值未被使用”。 实际上我正在尝试播放用户滑动页面时的声音,但它不断给我这个错误。这是我的代码。
包com.android.learning_numbers;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class Numbers extends Activity {
private static final OnPageChangeListener mOnPageListener = null;
public static MediaPlayer mp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(mOnPageListener);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {R.drawable.no1,R.drawable.no2,R.drawable.no3,R.drawable.no4,R.drawable.no5,R.drawable.no6,R.drawable.no7,R.drawable.no8,R.drawable.no9,};
private int[] mAudio= new int[]{R.raw.one,R.raw.two,R.raw.three,R.raw.four,R.raw.five,R.raw.six,R.raw.seven,R.raw.eight,R.raw.nine,};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Numbers.this;
ImageView imageView = new ImageView(context);
int padding =context.getResources().
getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
public OnPageChangeListener mOnPageListener = new OnPageChangeListener() {
@Override public void onPageSelected(int position) {
//for Sound
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Numbers.this, mAudio[position]);
mp.start();
}
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override public void onPageScrollStateChanged(int state) {}
};
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}