在这里,当我单击每个按钮时,我有 50 个按钮,那时我需要在画廊中显示一些图像。它工作正常。但同时我也需要播放与图像相关的声音,那时我得到错误。我运行模拟器的同一个应用程序工作正常。但是在三星galaxy gt-p1010中,当运行应用程序图像滚动时间时,我遇到了错误。在这个gt-1010中,在为每个图像添加音乐之前它的工作良好。但是在添加音乐之后,我面临这个prblm ..请任何人建议我...
logcat error:
05-16 09:00:53.152: E/AndroidRuntime(12917): FATAL EXCEPTION: main
05-16 09:00:43.292: E/AndroidRuntime(12799): java.lang.NullPointerException
05-16 09:00:43.292: E/AndroidRuntime(12799): at com.sample2.HomE.onItemSelected(HomE.java:508)
HomE.class:
public class HomE extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory{
private TextSwitcher mSwitcher;
private TextSwitcher mSwitcher3;
private TextSwitcher mSwitcher7;
ImageButton home, sound;
Gallery g;
static int nextbtn = 0;
static int prebtn = 0;
ImageAdapter iadapter;
int galpos;
int countk1 = 0;
int countk2;
int countk3 = 0;
int countk4 = 0;
int count111 = 0;
int count111i = 0;
int count111l = 0;
int count111s = 0;
public static int countin,counti,countl,counts;
public static int countin1=0;
public static int countini=0;
public static int countinl=0;
public static int countins=0;
public int ht;
public int wt;
MediaPlayer intro1, intro2, intro3, intro4, intro5, intro6, intro7, intro8,
intro9, intro10, intro11, intro12, intro13;
public static int motiongal = 3000;
RelativeLayout l1, l2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.home1);
intro1 = MediaPlayer.create(this, R.raw.abcsongnorwegianonlymusic);
intro2 = MediaPlayer.create(this, R.raw.bokstavslottet_2);
intro3 = MediaPlayer.create(this, R.raw.bokstavslottet_3);
intro4 = MediaPlayer.create(this, R.raw.bokstavslottet_4);
intro5 = MediaPlayer.create(this, R.raw.bokstavslottet_5);
intro6 = MediaPlayer.create(this, R.raw.bokstavslottet_6);
intro7 = MediaPlayer.create(this, R.raw.bokstavslottet_7);
intro8 = MediaPlayer.create(this, R.raw.bokstavslottet_8);
intro9 = MediaPlayer.create(this, R.raw.bokstavslottet_9);
intro10 = MediaPlayer.create(this, R.raw.bokstavslottet_10);
intro11 = MediaPlayer.create(this, R.raw.bokstavslottet_11);
home = (ImageButton) findViewById(R.id.homeimage);
home.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Intent i = new Intent(HomE.this, HomeDEmo.class);
startActivity(i);
return false;
}
});
sound = (ImageButton) findViewById(R.id.soundimage);
sound.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
ht = displaymetrics.heightPixels;
wt = displaymetrics.widthPixels;
l1 = (RelativeLayout) findViewById(R.id.linear);
l2 = (RelativeLayout) findViewById(R.id.linear1);
l1.setVisibility(View.INVISIBLE);
l2.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
l1.setVisibility(View.VISIBLE);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Do something after 100ms
l1.setVisibility(View.INVISIBLE);
}
}, 1500);
return false;
}
});
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
mSwitcher.startLayoutAnimation();
mSwitcher3 = (TextSwitcher) findViewById(R.id.switcher3);
mSwitcher3.setFactory(this);
mSwitcher3.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher3.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
mSwitcher3.startLayoutAnimation();
mSwitcher7 = (TextSwitcher) findViewById(R.id.switcher7);
mSwitcher7.setFactory(this);
mSwitcher7.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher7.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
mSwitcher7.startLayoutAnimation();
countin = getIntent().getIntExtra("a1", 0);
counti = getIntent().getIntExtra("i1", 0);
countl = getIntent().getIntExtra("l1", 0);
counts = getIntent().getIntExtra("s1", 0);
try {
g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setHorizontalScrollBarEnabled(true);
Bundle bundle = getIntent().getExtras();
galpos = bundle.getInt("GALPOS");
countk1 = getIntent().getIntExtra("k", 0);
// countk2 = getIntent().getIntExtra("k2", 0);
// countk3 = getIntent().getIntExtra("k3", 0);
// countk4 = getIntent().getIntExtra("k4", 0);
// count = getIntent().getIntExtra("k", 0);
g.setSelection(galpos);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
g.setOnItemSelectedListener(this);
ImageButton nextButton = (ImageButton) findViewById(R.id.nextimage);
nextButton.setOnClickListener(nextButtonOnClick);
ImageButton preButton = (ImageButton) findViewById(R.id.preimage);
preButton.setOnClickListener(preButtonOnClick);
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Toast.makeText(GalleryActivity.this, "" +
// mTextNames[position] , Toast.LENGTH_SHORT).show();
if (countk1 == 0) {
// mSwitcher8.setVisibility(View.INVISIBLE);
// mSwitcher7.setVisibility(View.INVISIBLE);
// mSwitcher6.setVisibility(View.INVISIBLE);
// mSwitcher5.setVisibility(View.INVISIBLE);
// mSwitcher4.setVisibility(View.INVISIBLE);
// mSwitcher3.setVisibility(View.INVISIBLE);
// mSwitcher1.setVisibility(View.INVISIBLE);
// mSwitcher2.setVisibility(View.INVISIBLE);
if (position == 12) {
count111 = 111;
countin1++;
Intent i = new Intent(HomE.this, HomeDEmo.class);
i.putExtra("k3", count111);
i.putExtra("aa1", countin1);
startActivityForResult(i, 0);
}
}
if (countk1 == 9) {
if (position == 12) {
count111i=9;
countini++;
Intent i = new Intent(HomE.this, DrawingScreEn1.class);
i.putExtra("i", countini);
i.putExtra("k3",count111i);
startActivityForResult(i, 0);
}
}
if (countk1 == 12) {
if (position == 13) {
count111l=13;
countinl++;
Intent i = new Intent(HomE.this, DrawingScreEn1.class);
i.putExtra("k3",count111l);
i.putExtra("l", countinl);
startActivityForResult(i, 0);
}
}
if (countk1 == 19) {
// mSwitcher.setVisibility(View.INVISIBLE);
// mSwitcher1.setVisibility(View.INVISIBLE);
// mSwitcher2.setVisibility(View.INVISIBLE);
if (position == 10) {
count111s=19;
countins++;
Intent i = new Intent(HomE.this, DrawingScreEn1.class);
i.putExtra("k3", count111s);
i.putExtra("s", countins);
startActivityForResult(i, 0);
}
}
l1.setVisibility(View.VISIBLE);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Do something after 100ms
l1.setVisibility(View.INVISIBLE);
}
}, 1500);
}
});
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
nextbtn = position;
if (countk1 == 0) {
mSwitcher.setText(mTextNames[position]);
}
if(countk1==9)
{
mSwitcher.setText(mTextNames[position]);
}
if(countk1==12)
{
mSwitcher7.setText(mTextNamesfourtin[position]);
}
if(countk1==19)
{
mSwitcher3.setText(mTextNamesleven[position]);
if (position == 0) {
intro1 = MediaPlayer.create(this, R.raw.abcsongnorwegianonlymusic);
intro1.start();
intro2.stop();
}
if (position == 1) {
intro1.stop();
intro2 = MediaPlayer.create(this, R.raw.bokstavslottet_2);
intro2.start();
intro3.stop();
}
if (position == 2) {
intro2.stop();
intro3 = MediaPlayer.create(this, R.raw.bokstavslottet_3);
intro3.start();
intro4.stop();
}
if (position == 3) {
intro3.stop();
intro4 = MediaPlayer.create(this, R.raw.bokstavslottet_4);
intro4.start();
intro5.stop();
}
if (position == 4) {
intro4.stop();
intro5 = MediaPlayer.create(this, R.raw.bokstavslottet_5);
intro5.start();
intro6.stop();
}
if (position == 5) {
intro5.stop();
intro6 = MediaPlayer.create(this, R.raw.bokstavslottet_6);
intro6.start();
intro7.stop();
}
if (position == 6) {
intro6.stop();
intro7 = MediaPlayer.create(this, R.raw.bokstavslottet_7);
intro7.start();
intro8.stop();
}
if (position == 7) {
intro7.stop();
intro8 = MediaPlayer.create(this, R.raw.bokstavslottet_8);
intro8.start();
intro9.stop();
}
if (position == 8) {
intro8.stop();
intro9 = MediaPlayer.create(this, R.raw.bokstavslottet_9);
intro9.start();
intro10.stop();
}
if (position == 9) {
intro9.stop();
intro10 = MediaPlayer.create(this, R.raw.bokstavslottet_10);
intro10.start();
intro11.stop();
}
if (position == 10) {
intro10.stop();
intro11 = MediaPlayer.create(this, R.raw.bokstavslottet_11);
intro11.start();
}
}
}
public OnClickListener nextButtonOnClick = new OnClickListener() {
public void onClick(View v) {
if ( countk1 == 9 && countk1 == 13
) {
if (nextbtn < (mTextNames.length - 1)) {
try {
mSwitcher.setText(mTextNames[nextbtn + 1]);
mSwitcher.startLayoutAnimation();
mSwitcher.getDrawingTime();
g.setSelection(nextbtn + 1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
} else {
try {
mSwitcher.setText(mTextNames[0]);
mSwitcher.startLayoutAnimation();
mSwitcher.getDrawingTime();
g.setSelection(0);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
}
}
if (countk1 == 12
) {
if (nextbtn < (mTextNamesfourtin.length - 1)) {
try {
mSwitcher7.setText(mTextNamesfourtin[nextbtn + 1]);
mSwitcher7.startLayoutAnimation();
mSwitcher7.getDrawingTime();
g.setSelection(nextbtn + 1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
} else {
try {
mSwitcher7.setText(mTextNamesfourtin[0]);
mSwitcher7.startLayoutAnimation();
mSwitcher7.getDrawingTime();
g.setSelection(0);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
}
}
if (countk1 == 19 ) {
if (nextbtn < (mTextNamesleven.length - 1)) {
try {
mSwitcher3.setText(mTextNamesleven[nextbtn + 1]);
mSwitcher3.startLayoutAnimation();
mSwitcher3.getDrawingTime();
g.setSelection(nextbtn + 1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
} else {
try {
mSwitcher3.setText(mTextNamesleven[0]);
mSwitcher3.startLayoutAnimation();
mSwitcher3.getDrawingTime();
g.setSelection(0);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
}
}
}
};
public OnClickListener preButtonOnClick = new OnClickListener() {
public void onClick(View v) {
if ( countk1 == 9 && countk1 == 13
) {
if (nextbtn > 0) {
try {
// mSwitcher.setText(mTextNames[nextbtn-1]);
mSwitcher.startLayoutAnimation();
mSwitcher.getDrawingTime();
g.setSelection(nextbtn - 1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
} else {
try {
mSwitcher.setText(mTextNames[mTextNames.length - 1]);
mSwitcher.startLayoutAnimation();
mSwitcher.getDrawingTime();
g.setSelection(mTextNames.length - 1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
}
}
if ( countk1 == 12
) {
if (nextbtn > 0) {
try {
// mSwitcher.setText(mTextNames[nextbtn-1]);
mSwitcher7.startLayoutAnimation();
mSwitcher7.getDrawingTime();
g.setSelection(nextbtn - 1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
} else {
try {
mSwitcher7
.setText(mTextNamesfourtin[mTextNamesfourtin.length - 1]);
mSwitcher7.startLayoutAnimation();
mSwitcher7.getDrawingTime();
g.setSelection(mTextNamesfourtin.length - 1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
}
}
if ( countk1 == 19
) {
if (nextbtn > 0) {
try {
// mSwitcher.setText(mTextNames[nextbtn-1]);
mSwitcher3.startLayoutAnimation();
mSwitcher3.getDrawingTime();
g.setSelection(nextbtn - 1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
} else {
try {
mSwitcher3
.setText(mTextNamesleven[mTextNamesleven.length - 1]);
mSwitcher3.startLayoutAnimation();
mSwitcher3.getDrawingTime();
g.setSelection(mTextNamesleven.length - 1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
} catch (Exception e) {
}
}
}
}
};
public void onNothingSelected(AdapterView<?> parent) {
}
public View makeView() {
TextView i = new TextView(this);
i.setTextSize(18);
i.setGravity(1);
return i;
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getView() {
// TODO Auto-generated method stub
return 0;
}
public int getCount() {
// if(countk1==1)
// {
// return mPhotos.size();
// }
// else
//
// if(countk2==2)
// {
// return mPhotos1.size();
// }
countk2 = getIntent().getIntExtra("k1", 0);
return countk2;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if (countk1 == 0) {
mSwitcher.setVisibility(View.INVISIBLE);
mSwitcher3.setVisibility(View.INVISIBLE);
mSwitcher7.setVisibility(View.INVISIBLE);
i.setImageResource(mPhotos.get(position));
}
if (countk1 == 9) {
mSwitcher.setVisibility(View.INVISIBLE);
mSwitcher3.setVisibility(View.INVISIBLE);
mSwitcher7.setVisibility(View.INVISIBLE);
i.setImageResource(mPhotosi.get(position));
}
if (countk1 == 12) {
mSwitcher7.setVisibility(View.INVISIBLE);
mSwitcher.setVisibility(View.INVISIBLE);
mSwitcher3.setVisibility(View.INVISIBLE);
i.setImageResource(mPhotosl.get(position));
}
if (countk1 == 19) {
mSwitcher3.setVisibility(View.INVISIBLE);
mSwitcher.setVisibility(View.INVISIBLE);
mSwitcher7.setVisibility(View.INVISIBLE);
i.setImageResource(mPhotosi.get(position));
}
// i.setMaxHeight(ht);
// i.setMaxWidth(wt);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.FILL_PARENT));
// if(wt >= 480){
// if(wt == 480 && ht > 320) {
// i.setLayoutParams(new Gallery.LayoutParams(
// LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// } else {
// i.setLayoutParams(new Gallery.LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// }
//
// }else{
//
// i.setLayoutParams(new Gallery.LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// }
// i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}
private Integer[] mImageIds = { R.drawable.bokstavslottet01,
R.drawable.bokstavslottet02, R.drawable.bokstavslottet03,
R.drawable.bokstavslottet04, R.drawable.bokstavslottet05,
R.drawable.bokstavslottet06, R.drawable.bokstavslottet07,
R.drawable.bokstavslottet08, R.drawable.bokstavslottet09,
R.drawable.bokstavslottet10, R.drawable.bokstavslottet11,
R.drawable.bokstavslottet12, R.drawable.bokstavslottet13,
};
private Integer[] mImageIdsi = { R.drawable.i1, R.drawable.i2,
R.drawable.i3, R.drawable.i4, R.drawable.i5, R.drawable.i6,
R.drawable.i7, R.drawable.i8, R.drawable.i9, R.drawable.i10,
R.drawable.i11, R.drawable.i12, R.drawable.i13 };
private Integer[] mImageIdsl = { R.drawable.l1, R.drawable.l2,
R.drawable.l3, R.drawable.l4, R.drawable.l5, R.drawable.l6,
R.drawable.l7, R.drawable.l8, R.drawable.l9, R.drawable.l10,
R.drawable.l11, R.drawable.l12, R.drawable.l13, R.drawable.l14
};
private static String[] mTextNamesleven = { "BOOKSTOVEN1", "BOOKSTOVEN2",
"BOOKSTOVEN3", "BOOKSTOVEN4", "BOOKSTOVEN5", "BOOKSTOVEN6",
"BOOKSTOVEN7", "BOOKSTOVEN8", "BOOKSTOVEN9", "BOOKSTOVEN10",
"BOOKSTOVEN11" };
private static String[] mTextNames = { "BOOKSTOVEN1", "BOOKSTOVEN2",
"BOOKSTOVEN3", "BOOKSTOVEN4", "BOOKSTOVEN5", "BOOKSTOVEN6",
"BOOKSTOVEN7", "BOOKSTOVEN8", "BOOKSTOVEN9", "BOOKSTOVEN10",
"BOOKSTOVEN11", "BOOKSTOVEN12", "BOOKSTOVEN13" };
private static String[] mTextNamesfourtin = { "BOOKSTOVEN1", "BOOKSTOVEN2",
"BOOKSTOVEN3", "BOOKSTOVEN4", "BOOKSTOVEN5", "BOOKSTOVEN6",
"BOOKSTOVEN7", "BOOKSTOVEN8", "BOOKSTOVEN9", "BOOKSTOVEN10",
"BOOKSTOVEN11", "BOOKSTOVEN12", "BOOKSTOVEN13", "BOOKSTOVEN14" };
private ArrayList<Integer> mPhotos = new ArrayList<Integer>(
Arrays.asList(mImageIds));
private ArrayList<Integer> mPhotosi = new ArrayList<Integer>(
Arrays.asList(mImageIdsi));
private ArrayList<Integer> mPhotosl = new ArrayList<Integer>(
Arrays.asList(mImageIdsl));
protected void onPause() {
if (intro1 != null){
intro1.stop();
if (isFinishing()){
intro1.stop();
intro1.release();
}
}
if (intro2 != null){
intro2.stop();
if (isFinishing()){
intro2.stop();
intro2.release();
}
}
if (intro3 != null){
intro3.stop();
if (isFinishing()){
intro3.stop();
intro3.release();
}
}
if (intro4 != null){
intro4.stop();
if (isFinishing()){
intro4.stop();
intro4.release();
}
}
if (intro5 != null){
intro5.stop();
if (isFinishing()){
intro5.stop();
intro5.release();
}
}
if (intro6 != null){
intro6.stop();
if (isFinishing()){
intro6.stop();
intro6.release();
}
}
if (intro7 != null){
intro7.stop();
if (isFinishing()){
intro7.stop();
intro7.release();
}
}
if (intro8 != null){
intro8.stop();
if (isFinishing()){
intro8.stop();
intro8.release();
}
}
if (intro9 != null){
intro9.stop();
if (isFinishing()){
intro9.stop();
intro9.release();
}
}
if (intro10 != null){
intro10.stop();
if (isFinishing()){
intro10.stop();
intro10.release();
}
}
if (intro11 != null){
intro11.stop();
if (isFinishing()){
intro11.stop();
intro11.release();
}
}
super.onPause();
} }