如果我单击PoiView类中的Back Button,我会得到一个NullPointer异常。已加载父活动,但随后会引发错误。在我点击后退按钮之前我在媒体播放器中播放了一个文件,但在使用后退按钮之前暂停了。
激怒任何人一个提示,我怎么能解决这个NullPointer?问题如下:
startTimeField.setText(String.format("%d min, %d sec",
似乎没有值可用于填充此textView?
PoiView类:
public class PoiView extends ActionBarActivity {
ArrayList<RowItem> pois;
Integer citySave;
String cityTitle;
Integer routeSave;
String routeTitel;
String poiTitel;
String poiAudio;
String poiImage;
String textPoi;
private MediaPlayer mediaPlayer;
private double startTime = 0;
private double finalTime = 0;
private Handler myHandler = new Handler();
private SeekBar seekbar;
private ImageButton playButton,pauseButton;
public static int oneTimeOnly = 0;
public TextView startTimeField,endTimeField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_poi_view);
Bundle b = getIntent().getExtras();
pois = b.getParcelableArrayList("pois");
citySave = b.getInt("stadt");
cityTitle = b.getString("titel_stadt");
routeTitel = b.getString("titel_route");
routeSave = b.getInt("route");
poiTitel = b.getString("titel_poi");
poiAudio = b.getString("audioPoi");
poiImage = b.getString("imagePoi");
textPoi = b.getString("textPoi");
getActionBar().setDisplayHomeAsUpEnabled(true);
final ActionBar actionBar = getSupportActionBar();
setTitle(String.format(getResources().getString(R.string.title_activity_poi_view)) + " " + poiTitel);
TextView titelPoi = (TextView) findViewById(R.id.namePoi);
titelPoi.setText(poiTitel);
ImageView picPoi = (ImageView) findViewById(R.id.imagePoi);
picPoi.setImageResource(getResources().getIdentifier(poiImage, "drawable", getPackageName()));
TextView infotextPoi = (TextView) findViewById(R.id.textPoi);
infotextPoi.setText(textPoi);
seekbar = (SeekBar)findViewById(R.id.seekBar);
playButton = (ImageButton)findViewById(R.id.imagePlay);
pauseButton = (ImageButton)findViewById(R.id.imagePause);
startTimeField =(TextView)findViewById(R.id.textStart);
seekbar.setClickable(false);
pauseButton.setEnabled(false);
endTimeField =(TextView)findViewById(R.id.textStop);
try {
int resID=getResources().getIdentifier(poiAudio, "raw", getPackageName());
mediaPlayer = MediaPlayer.create(this, resID);
} catch (NullPointerException e) {
Context context = getApplicationContext();
CharSequence text = getResources().getString(R.string.audioerror);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.stop();
mediaPlayer.release();
}
});
}
public void play(View view) {
Toast.makeText(getApplicationContext(), R.string.audio_play,
Toast.LENGTH_SHORT).show();
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if (oneTimeOnly == 0) {
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}
endTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) finalTime)))
);
startTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(UpdateSongTime,100);
pauseButton.setEnabled(true);
playButton.setEnabled(false);
}
private Runnable UpdateSongTime = new Runnable() {
public void run() {
startTime = mediaPlayer.getCurrentPosition();
startTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(this, 100);
}
};
public void pause(View view){
Toast.makeText(getApplicationContext(), R.string.audio_pause,
Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
pauseButton.setEnabled(false);
playButton.setEnabled(true);
}
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.setClass(PoiView.this, RouteView.class);
Bundle b = new Bundle();
b.putInt("stadt", citySave);
b.putString("titel_stadt", cityTitle);
b.putInt("route", routeSave);
b.putString("titel_route", routeTitel);
intent.putExtras(b);
startActivity(intent);
startTime = mediaPlayer.getCurrentPosition();
finish();
}
@Override
public void onPause() {
super.onPause();
//Bundle b = new Bundle();
unbindDrawables(findViewById(R.id.imagePoi));
System.gc();
mediaPlayer.pause();
startTime = mediaPlayer.getCurrentPosition();
finish();
}
@Override
public void onResume() {
super.onResume();
//Bundle b = getIntent().getExtras();
startTime = mediaPlayer.getCurrentPosition();
}
public class MusicIntentReceiver extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
if (intent.getAction().equals(
android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
mediaPlayer.pause();
pauseButton.setEnabled(false);
playButton.setEnabled(true);
startTime = mediaPlayer.getCurrentPosition();
}
}
}
@Override
public void onStop() {
super.onStop();
mediaPlayer.release();
mediaPlayer = null;
finish();
}
private void unbindDrawables(View view)
{
if (view.getBackground() != null)
{
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup && !(view instanceof AdapterView))
{
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
{
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
@Override
public void onDestroy() {
super.onDestroy();
unbindDrawables(findViewById(R.id.imagePoi));
System.gc();
if (mediaPlayer != null) mediaPlayer.release();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.poi_view, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_stadt:
setContentView(R.layout.activity_stadt);
Intent stadt = new Intent(PoiView.this, StadtActivity.class);
startActivity(stadt);
return true;
case R.id.action_route:
setContentView(R.layout.activity_route_choose);
Intent route = new Intent(PoiView.this, RouteChooseActivity.class);
route.putExtra("stadt", citySave);
route.putExtra("titel_stadt", cityTitle);
startActivity(route);
return true;
case R.id.action_routeview:
setContentView(R.layout.activity_route_view);
Intent routeview = new Intent(PoiView.this, RouteView.class);
NavUtils.navigateUpFromSameTask(this);
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new task
// when navigating up, with a synthesized back stack.
routeview.putExtra("stadt", citySave);
routeview.putExtra("titel_stadt", cityTitle);
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
routeview.putExtra("stadt", citySave);
routeview.putExtra("titel_stadt", cityTitle);
}
routeview.putExtra("route", routeSave);
routeview.putExtra("titel_route", routeTitel);
startActivity(routeview);
return true;
case R.id.action_help:
setContentView(R.layout.activity_help);
Intent help = new Intent(PoiView.this, Help.class);
startActivity(help);
return true;
case R.id.action_exit:
moveTaskToBack(true);
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
logcat的:
08-07 12:31:34.054 4602-4602/de.cityknight.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2ae2ba8)
08-07 12:31:34.064 4602-4602/de.cityknight.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.cityknight.app, PID: 4602
java.lang.NullPointerException
at de.cityknight.app.PoiView$2.run(PoiView.java:142)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
可能是因为您的UpdateSongTime
runnable仍然计划运行。在完成您的活动之前致电myHandler.remove callbacks( UpdateSongTime);
。附:你的代码还有很多其他问题,我现在不会进入这个问题。