我在问我的问题之前经常搜索并尝试了很多方法,但没有一个能与我合作。 我正在制作一个广播播放器!所以我有一个包含广播电台列表的列表视图,每个项目都包含一些信息,当我点击该项目时,该链接将被播放。
因此,当点击项目我创建一个包含玩家的新活动时(玩家将启动一个将播放该流的新服务) 当我点击返回列表视图时,播放器的活动将从堆栈中删除。 当我点击通知时,应用程序崩溃,因为找不到活动。 我试图覆盖BackButton,但当我点击返回应用程序返回主屏幕时:
@Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}
我尝试在开始前为意图添加一些标志!但它也不起作用!! 所以,我完全被阻止,找不到任何方法来处理这个问题 谢谢你的帮助 ! 如果你想要一些源代码,请告诉我我会更新我的帖子! 谢谢 更新:日志:
07-27 00:02:20.650: E/dalvikvm(9406): The following issues were seen:
07-27 00:02:22.390: E/AndroidRuntime(9406): FATAL EXCEPTION: main
07-27 00:02:22.390: E/AndroidRuntime(9406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MSoft.socialradio/com.MSoft.socialradio.StationListenActivity}: java.lang.NullPointerException
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.access$700(ActivityThread.java:150)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.os.Looper.loop(Looper.java:176)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.main(ActivityThread.java:5279)
07-27 00:02:22.390: E/AndroidRuntime(9406): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 00:02:22.390: E/AndroidRuntime(9406): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 00:02:22.390: E/AndroidRuntime(9406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-27 00:02:22.390: E/AndroidRuntime(9406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-27 00:02:22.390: E/AndroidRuntime(9406): at dalvik.system.NativeStart.main(Native Method)
07-27 00:02:22.390: E/AndroidRuntime(9406): Caused by: java.lang.NullPointerException
07-27 00:02:22.390: E/AndroidRuntime(9406): at com.MSoft.socialradio.StationListenActivity.onCreate(StationListenActivity.java:91)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.Activity.performCreate(Activity.java:5267)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
07-27 00:02:22.390: E/AndroidRuntime(9406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
07-27 00:02:22.390: E/AndroidRuntime(9406): ... 11 more
07-27 00:02:31.900: E/AndroidRuntime(10122): FATAL EXCEPTION: main
07-27 00:02:31.900: E/AndroidRuntime(10122): java.lang.RuntimeException: Unable to start service com.MSoft.socialradio.MyMediaPlayerService@210972d0 with null: java.lang.NullPointerException
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2757)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread.access$2000(ActivityThread.java:150)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1383)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.os.Looper.loop(Looper.java:176)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread.main(ActivityThread.java:5279)
07-27 00:02:31.900: E/AndroidRuntime(10122): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 00:02:31.900: E/AndroidRuntime(10122): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 00:02:31.900: E/AndroidRuntime(10122): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-27 00:02:31.900: E/AndroidRuntime(10122): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-27 00:02:31.900: E/AndroidRuntime(10122): at dalvik.system.NativeStart.main(Native Method)
07-27 00:02:31.900: E/AndroidRuntime(10122): Caused by: java.lang.NullPointerException
07-27 00:02:31.900: E/AndroidRuntime(10122): at com.MSoft.socialradio.MyMediaPlayerService.onStartCommand(MyMediaPlayerService.java:51)
07-27 00:02:31.900: E/AndroidRuntime(10122): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2740)
07-27 00:02:31.900: E/AndroidRuntime(10122): ... 10 more
服务播放器:
package com.MSoft.socialradio;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;
public class MyMediaPlayerService extends Service implements OnPreparedListener, OnErrorListener, OnBufferingUpdateListener {
Timer timer ;
TextView songName;
String GetSongInfo;
MetadataTask metaTask=null;
private MediaPlayer mediaPlayer = null;
private boolean isPlaying = false;
String url;
private static int classID = 579; // just a number
public static String START_PLAY = "START_PLAY";
final static String MY_ACTION = "IcyData";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getBooleanExtra(START_PLAY, false)) {
url=intent.getStringExtra("url");
play();
}
return Service.START_STICKY;
}
private void play() {
if (!isPlaying) {
isPlaying = true;
Intent intent = new Intent(this, StationListenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Social Radio")
.setContentText("Now Playing:under Construction")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.build();
/*mediaPlayer = MediaPlayer.create(this, R.raw.rehab); // change this for your file
mediaPlayer.setLooping(true); // this will make it loop forever
mediaPlayer.start();*/
//url = "http://ice.abradio.cz:80/hit90128.mp3"; // your URL here
Log.i("play","playing:"+url);
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setVolume(100, 100);
mediaPlayer.setDataSource(url);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
} catch (Exception e) {
}
startForeground(classID, notification);
}
}
@Override
public void onDestroy() {
Log.i("on Destory","Stopping");
stop();
//timer.cancel();
}
private void stop() {
if (isPlaying) {
Log.i("on Destory","Stopping");
isPlaying = false;
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
stopForeground(true);
}
}
@Override
public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onPrepared(MediaPlayer arg0) {
mediaPlayer.start();
StartTimer();
}
public InputStream OpenHttpPOSTConnection(String url) {
InputStream inputStream = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// ---the key/value pairs to post to the server---
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
} catch (ConnectTimeoutException e) {
Log.d("OpenHttpPOSTConnection TIMEOUT", e.getLocalizedMessage());
} catch (Exception e) {
Log.d("OpenHttpPOSTConnection", e.getLocalizedMessage());
}
return inputStream;
}
public void StartTimer()
{
try {
final URL Url = new URL(url);
Log.i("StartTimer",url);
new Thread(new Runnable() {
@Override
public void run() {
while(mediaPlayer!=null)
{
try {
Log.i("StartTimer","Sending:");
metaTask=new MetadataTask();
metaTask.execute(Url);
Intent intent_braodcast = new Intent();
intent_braodcast.setAction(MY_ACTION);
intent_braodcast.putExtra("DATAPASSED", GetSongInfo);
sendBroadcast(intent_braodcast);
Log.i("StartTimer","Sending:");
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
} catch (Exception e) {
Log.i("StartTimer Exception","");
}
}
protected class MetadataTask extends AsyncTask<URL, Void, IcyStreamMeta> {
protected IcyStreamMeta streamMeta;
@Override
protected IcyStreamMeta doInBackground(URL... urls) {
try {
if (OpenHttpPOSTConnection(urls[0].toString()) == null) {
//songName.setText("Error in connection");
throw new Exception("Error network");
}
streamMeta = new IcyStreamMeta(urls[0]);
if (streamMeta == null) {
//songName.setText("Error in connection");
throw new Exception("Error in connection");
}
streamMeta.refreshMeta();
} catch (IOException e) {
// TODO: Handle
Log.e(MetadataTask.class.toString(), e.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("ExceptionDoinBackground", e.getLocalizedMessage());
}
return streamMeta;
}
@Override
protected void onPostExecute(IcyStreamMeta result) {
//super.onPostExecute(result);
try {
if (streamMeta.GetFullData() == "")
GetSongInfo="No Icy Data";
else {
GetSongInfo=streamMeta.GetFullData();
//songName.setText(streamMeta.GetFullData());
Log.i("Onposteexecute", streamMeta.GetFullData());
}
// txtArtist.setText(streamMeta.getArtist());
// txtTitle.setText(streamMeta.getTitle());
} catch (IOException e) {
// TODO: Handle
Log.e(MetadataTask.class.toString(), e.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
你是第一次添加网址时启动服务,所以在MyMediaPlayerService.java:51行中工作正常。当您从nofitication启动服务时,您没有在参数中设置URL。服务意图:
Intent intent = new Intent(this,StationListenActivity.class);
应该有网址