-3
我是一個學習者和開發Android應用播放實時流顯示在媒體播放器當前曲目信息。除1個問題外,它工作正常。我想顯示當前正在播放的曲目的標題,可在網頁上找到。用於此目的的我已經使用一個TextView並試圖利用異步任務和稱爲包含一個可運行在異步任務與我可以在30處的間隔更新的TextView的文本的方法seconds.In可運行我稱爲它加載的網頁的方法並將其內容作爲字符串提供。安卓:使用網頁
問題:當它試圖更新的TextView,應用程序崩潰的文本。感謝提前。
以下是我的代碼:
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import java.net.URLConnection;
import java.net.URL;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import android.util.Log;
import java.net.MalformedURLException;
import android.widget.TextView;
import android.os.AsyncTask;
public class MusicAndroidActivity extends Activity {
static MediaPlayer mPlayer;
Button buttonPlay;
Button buttonStop;
Button buttonPause;
public TextView txtMessage;
private StringBuilder response;
private String text="***";
ProgressDialog progDailog;
private final static int INTERVAL = 1000 * 30; //2 minutes
Handler mHandler;
int length=0;
String url = "http://www.s8.voscast.com:9630/;stream.mp3";
private final Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStop = (Button) findViewById(R.id.stop);
txtMessage=(TextView)findViewById(R.id.txtMessage);
buttonStop.setEnabled(false);
buttonPause=(Button) findViewById(R.id.pause);
buttonPause.setEnabled(false);
buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
buttonPlay.setEnabled(false);
buttonStop.setEnabled(true);
buttonPause.setEnabled(true);
//txtMessage.setText("Loading...");
progDailog = ProgressDialog.show(MusicAndroidActivity.this, "", "Buffering ... \n It can take upto 1 Minute, depending upon your internet speed", true);
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mPlayer) {
// TODO Auto-generated method stub
mPlayer.start();
progDailog.dismiss();
LoadWebPageASYNC task = new LoadWebPageASYNC();
task.execute(new String[]{"http://khilare.com/swltest/sms.html"});
}
});
mPlayer.prepareAsync();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} //catch (IOException e) {
//Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
//}
}
});
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.stop();
buttonPlay.setEnabled(true);
buttonStop.setEnabled(false);
buttonPause.setEnabled(false);
}
}
});
buttonPause.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.pause();
length=mPlayer.getCurrentPosition();
buttonPause.setText("Resume");
//buttonPlay.setEnabled(true);
buttonStop.setEnabled(true);
buttonPlay.setEnabled(false);
//sapp();
//getHTML();
//txtMessage.setText(text);
txtMessage.setText(text);
}
else {
//mPlayer.seekTo(length);
mPlayer.start();
// mPlayer.seekTo(length);
buttonPause.setText("Pause");
}
}
});
}
protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
private class LoadWebPageASYNC extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
getHTML();
//sapp();
txtMessage.setText(text);
return null;
}
}
private void sapp()
{
//Runnable updater1 = new Runnable() {
handler.postDelayed(new Runnable() {
//@Override
public void run() {
getHTML();
txtMessage.setText(text);
}
}, INTERVAL);
/*public void run() {
getHTML();
txtMessage.setText(text);
}
};*/
//handler.post(updater1);
}
private void getHTML()
{
try {
URLConnection connection = new URL("http://khilare.com/swltest/sms.html").openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
InputStream responseStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
response = new StringBuilder();
String line;
line = br.readLine();
{
response.append(line);
}
text = response.toString();
//txtMessage.setText(text);
//Log.i("Output", text);
} catch (MalformedURLException e) {
txtMessage.setText(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
txtMessage.setText(e.getMessage());
e.printStackTrace();
}
//textStreamed.setText(text);
}
}
這裏是logcat的:
java.lang.SecurityException: Permission denial: writing to settings requires android.permission.WRITE_SETTINGS
at com.android.providers.settings.SettingsProvider.callFromPackage(SettingsProvider.java:645)
at android.content.ContentProvider$Transport.call(ContentProvider.java:279)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:273)
at android.os.Binder.execTransact(Binder.java:388)
at dalvik.system.NativeStart.run(Native Method)
05-29 10:56:45.772 302-302/? E/Parcel﹕ Reading a NULL string not supported here.
05-29 10:56:45.772 302-302/? E/Parcel﹕ Reading a NULL string not supported here.
05-29 10:56:48.462 302-302/? E/Parcel﹕ Reading a NULL string not supported here.
05-29 10:56:48.462 302-302/? E/Parcel﹕ Reading a NULL string not supported here.
05-29 10:56:50.702 302-302/? E/Parcel﹕ Reading a NULL string not supported here.
05-29 10:56:50.702 302-302/? E/Parcel﹕ Reading a NULL string not supported here.
05-29 10:56:50.712 302-302/? E/Parcel﹕ Reading a NULL string not supported here.
05-29 10:56:50.712 302-302/? E/Parcel﹕ Reading a NULL string not supported here.
05-29 10:57:01.772 28164-28569/com.prgguru.example E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5969)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:921)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4276)
at android.view.View.invalidate(View.java:10552)
at android.view.View.invalidate(View.java:10507)
at android.widget.TextView.checkForRelayout(TextView.java:6531)
at android.widget.TextView.setText(TextView.java:3789)
at android.widget.TextView.setText(TextView.java:3643)
at android.widget.TextView.setText(TextView.java:3618)
at com.prgguru.example.MusicAndroidActivity$LoadWebPageASYNC.doInBackground(MusicAndroidActivity.java:158)
at com.prgguru.example.MusicAndroidActivity$LoadWebPageASYNC.doInBackground(MusicAndroidActivity.java:152)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
你的logcat在哪裏? –
對不起,我錯過了。現在我添加了logcat,請檢查。 – SJSSoft
你的'MusicAndroidActivity $ LoadWebPageASYNC'在哪裏? –