请帮我解决这个问题。我试图让一个无线电Android应用程序在后台运行,但我失败了。你能帮我吗...这是我的代码:
Background.java
package com.ihacks.radiolumina;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.content.Intent;
import android.os.IBinder;
import android.app.Service;
import java.io.IOException;
public class Background extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
try {
player.setDataSource("http://www.radiolumina.ro:9000/;stream.nsv");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
@Override
public void onDestroy() {
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
}
Ecran.java
package com.ihacks.radiolumina;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.content.Context;
import android.media.AudioManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.content.Intent;
public class Ecran extends Activity implements OnClickListener{
private Button btnStart, btnStop;
private ProgressBar progressBar;
private MediaPlayer player;
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
Intent playbackServiceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_ecran);
init();
initMediaPlayer();
Intent svc=new Intent(this, com.ihacks.radiolumina.Background.class);
startService(svc);
}
private void initMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://www.radiolumina.ro:9000/;stream.nsv");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.d("Radio_Lumina", "Ecran.initMediaPlayer().new OnBufferingUpdateListener() {...}: "+percent);
}
});
}
private void init() {
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
progressBar.setVisibility(View.GONE);
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
@Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btnStart:
startPlaying();
break;
case R.id.btnStop:
stopPlaying();
break;
default:
break;
}
}
private void startPlaying() {
btnStop.setEnabled(true);
btnStart.setEnabled(false);
progressBar.setVisibility(View.VISIBLE);
//playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressBar.setVisibility(View.GONE);
Log.v("Radio_Lumina", "Ecran.startPlaying().new OnPreparedListener() {...}: Start of radio");
player.start();
}
});
startService(playbackServiceIntent);
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initMediaPlayer();
}
btnStart.setEnabled(true);
btnStop.setEnabled(false);
//playSeekBar.setVisibility(View.INVISIBLE);
stopService(playbackServiceIntent);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ihacks.radiolumina"
android:versionCode="1"
android:versionName="2.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<permission android:name="android.permission.INTERNET"></permission>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<activity
android:name="com.ihacks.radiolumina.Ecran"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="Background"></service>
</application>
</manifest>
这是日志:
10:21:16.394: I/Process(2009): Sending signal. PID: 2009 SIG: 9
06-10 10:21:29.334: D/dalvikvm(2057): GC_FOR_ALLOC freed 61K, 5% free 2882K/3028K, paused 34ms, total 36ms
06-10 10:21:29.334: I/dalvikvm-heap(2057): Grow heap (frag case) to 4.329MB for 1507216-byte allocation
06-10 10:21:29.384: D/dalvikvm(2057): GC_FOR_ALLOC freed 2K, 4% free 4351K/4500K, paused 43ms, total 43ms
06-10 10:21:29.664: D/dalvikvm(2057): GC_FOR_ALLOC freed <1K, 4% free 4611K/4760K, paused 23ms, total 24ms
06-10 10:21:29.664: I/dalvikvm-heap(2057): Grow heap (frag case) to 5.953MB for 1440016-byte allocation
06-10 10:21:29.704: D/dalvikvm(2057): GC_FOR_ALLOC freed <1K, 3% free 6017K/6168K, paused 35ms, total 35ms
06-10 10:21:30.244: D/AndroidRuntime(2057): Shutting down VM
06-10 10:21:30.244: W/dalvikvm(2057): threadid=1: thread exiting with uncaught exception (group=0xb3aceba8)
06-10 10:21:30.254: E/AndroidRuntime(2057): FATAL EXCEPTION: main
06-10 10:21:30.254: E/AndroidRuntime(2057): Process: com.ihacks.radiolumina, PID: 2057
06-10 10:21:30.254: E/AndroidRuntime(2057): java.lang.RuntimeException: Unable to create service com.ihacks.radiolumina.Background: java.lang.NullPointerException
06-10 10:21:30.254: E/AndroidRuntime(2057): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2582)
06-10 10:21:30.254: E/AndroidRuntime(2057): at android.app.ActivityThread.access$1800(ActivityThread.java:135)
06-10 10:21:30.254: E/AndroidRuntime(2057): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
06-10 10:21:30.254: E/AndroidRuntime(2057): at android.os.Handler.dispatchMessage(Handler.java:102)
06-10 10:21:30.254: E/AndroidRuntime(2057): at android.os.Looper.loop(Looper.java:136)
06-10 10:21:30.254: E/AndroidRuntime(2057): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-10 10:21:30.254: E/AndroidRuntime(2057): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 10:21:30.254: E/AndroidRuntime(2057): at java.lang.reflect.Method.invoke(Method.java:515)
06-10 10:21:30.254: E/AndroidRuntime(2057): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-10 10:21:30.254: E/AndroidRuntime(2057): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-10 10:21:30.254: E/AndroidRuntime(2057): at dalvik.system.NativeStart.main(Native Method)
06-10 10:21:30.254: E/AndroidRuntime(2057): Caused by: java.lang.NullPointerException
06-10 10:21:30.254: E/AndroidRuntime(2057): at com.ihacks.radiolumina.Background.onCreate(Background.java:24)
06-10 10:21:30.254: E/AndroidRuntime(2057): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2572)
06-10 10:21:30.254: E/AndroidRuntime(2057): ... 10 more
06-10 10:26:30.654: I/Process(2057): Sending signal. PID: 2057 SIG: 9
你能帮帮我吗...我是绝望的;我也尝试用Fragments创建这个应用程序,但我可以将所有应用程序都放在一个应用程序中。
修改
此代码有效,但不适用于后台;我还删除了Background.java:
Ecran.java 包com.ihacks.radiolumina;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.content.Context;
import android.media.AudioManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.content.Intent;
public class Ecran extends Activity implements OnClickListener{
private Button btnStart, btnStop;
private ProgressBar progressBar;
private MediaPlayer player;
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
Intent playbackServiceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_ecran);
init();
initMediaPlayer();
}
private void initMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://www.radiolumina.ro:9000/;stream.nsv");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.d("Radio_Lumina", "Ecran.initMediaPlayer().new OnBufferingUpdateListener() {...}: "+percent);
}
});
}
private void init() {
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
progressBar.setVisibility(View.GONE);
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
@Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btnStart:
startPlaying();
break;
case R.id.btnStop:
stopPlaying();
break;
default:
break;
}
}
private void startPlaying() {
btnStop.setEnabled(true);
btnStart.setEnabled(false);
progressBar.setVisibility(View.VISIBLE);
//playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressBar.setVisibility(View.GONE);
Log.v("Radio_Lumina", "Ecran.startPlaying().new OnPreparedListener() {...}: Start of radio");
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initMediaPlayer();
}
btnStart.setEnabled(true);
btnStop.setEnabled(false);
//playSeekBar.setVisibility(View.INVISIBLE);
}
}
干杯, 阿德里安
答案 0 :(得分:1)
背景:java.lang.NullPointerException
代码中的某处有一个NullPointerException。它将在您的日志中,通读它。