我开发了简单的imageview应用程序,可以播放背景音乐以及从左到右滑动的图像..但我的问题是每当我的应用程序在设备上运行并且设备进入睡眠状态时......打开它后显示强制关闭对话框,应用程序只是强制关闭..什么可能是错的?..请提供此问题的解决方案..以下是我的代码和logcat错误
Logcat错误
08-30 02:14:31.423: E/AndroidRuntime(1945): FATAL EXCEPTION: main
08-30 02:14:31.423: E/AndroidRuntime(1945): java.lang.RuntimeException: Unable to resume activity {com.manishkpr.viewpagerimagegallery/com.manishkpr.viewpagerimagegallery.MainActivity}: java.lang.IllegalStateException
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2059)
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.os.Looper.loop(Looper.java:123)
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-30 02:14:31.423: E/AndroidRuntime(1945): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 02:14:31.423: E/AndroidRuntime(1945): at java.lang.reflect.Method.invoke(Method.java:521)
08-30 02:14:31.423: E/AndroidRuntime(1945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-30 02:14:31.423: E/AndroidRuntime(1945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-30 02:14:31.423: E/AndroidRuntime(1945): at dalvik.system.NativeStart.main(Native Method)
08-30 02:14:31.423: E/AndroidRuntime(1945): Caused by: java.lang.IllegalStateException
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.media.MediaPlayer.seekTo(Native Method)
08-30 02:14:31.423: E/AndroidRuntime(1945): at com.manishkpr.viewpagerimagegallery.MainActivity.onResume(MainActivity.java:86)
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.app.Activity.performResume(Activity.java:3823)
08-30 02:14:31.423: E/AndroidRuntime(1945): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
08-30 02:14:31.423: E/AndroidRuntime(1945): ... 10 more
Mainactivity.java
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
MediaPlayer oursong;
ViewPager viewPager;
ImageAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
oursong.seekTo(0);
oursong.start();
viewPager = (ViewPager) findViewById(R.id.view_pager);
adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(MyViewPagerListener);
}
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.activity_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
@Override
protected void onPause() {
super.onPause();
if(oursong != null){
oursong.release();
}
}
@Override
protected void onResume(){
super.onResume();
/*
* This is the important part, basically since your releasing the song
* in onPause() you are getting rid of its reference, in this case check
* if your song is null then if it is re-create it, else you can reuse the
* the original, but i suspect that calling release() in onPause() allows the
* song to get cleaned up by Java's Garbage Collector.
*/
if(oursong == null){
oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
oursong.seekTo(0); // You will probably want to save an int to restore here
oursong.start();
}else{
oursong.seekTo(0);
oursong.start();
}
}
/*
* May want to add two methods here: onSaveInstanceState(Bundle outstate) &
* onRestoreInstanceState(Bundle savedInstanceState) to maintain playback position
* in onResume instead of just restarting the song.
*/
private final OnPageChangeListener MyViewPagerListener = new OnPageChangeListener() {
@Override
public void onPageSelected(int pos) {
if (pos == adapter.getCount() - 1){
// adding null checks for safety
if(oursong != null){
oursong.pause();
}
} else if (!oursong.isPlaying()){
// adding null check for safety
if(oursong != null){
oursong.start();
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
};
}
imageadapter.java
import java.io.IOException;
import android.app.WallpaperManager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImageAdapter extends PagerAdapter {
Context context;
private final int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_small);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(GalImages[position]);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(context);
try {
myWallpaperManager.setResource(GalImages[position]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
答案 0 :(得分:0)
如果IllegalStateException
未初始化,您会看到seekTo
会抛出MediaPlayer
。在onPause
您release
对象,在onResume
中,您无需初始化即可再次使用它。顺便说一句,你应该在释放后将oursong
设置为NULL。
尝试以下方法:
private int pos = 0;
@Override
protected void onPause() {
super.onPause();
if(oursong != null){
pos = oursong.getCurrentPosition();
oursong.release();
oursong = null;
}
}
@Override
protected void onResume(){
super.onResume();
/*
* This is the important part, basically since your releasing the song
* in onPause() you are getting rid of its reference, in this case check
* if your song is null then if it is re-create it, else you can reuse the
* the original, but i suspect that calling release() in onPause() allows the
* song to get cleaned up by Java's Garbage Collector.
*/
oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
oursong.seekTo(pos); // You will probably want to save an int to restore here
oursong.start();
}