こんにちは、私はこの問題を抱えています。IceCast エラー NetworkOnMainThreadException からのサービスへの接続のため、AsyncTask を作成する必要がありました。
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
しかし、どのような状況でも機能せず、try-catch にもかかわらずエラーを通知することなくアプリケーションがクラッシュします..何について説明してもらえますか?
コードは次のとおりです。
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import net.moraleboost.streamscraper.ScrapeException;
import net.moraleboost.streamscraper.Scraper;
import net.moraleboost.streamscraper.Stream;
import net.moraleboost.streamscraper.scraper.IceCastScraper;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class SwipeyTabsSampleActivity extends FragmentActivity {
private Button streamButton;
private ImageButton playButton;
private TextView textStreamed;
private TextView textSong;
private boolean isPlaying;
private StreamingMediaPlayer audioStreamer;
private static final String [] TITLES = {
"Live Stream",
"Palinsesto",
"Programmi",
"Eventi",
};
private SwipeyTabs mTabs;
private ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipeytab);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mTabs = (SwipeyTabs) findViewById(R.id.swipeytabs);
SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(this,
getSupportFragmentManager());
mViewPager.setAdapter(adapter);
mTabs.setAdapter(adapter);
mViewPager.setOnPageChangeListener(mTabs);
mViewPager.setCurrentItem(0);
Inizializza();
new AsyncTaskProc().execute();
}
private void startStreamingAudio() {
try {
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
if ( audioStreamer != null) {
audioStreamer.interrupt();
}
audioStreamer = new StreamingMediaPlayer(this, textStreamed, playButton, streamButton,progressBar);
audioStreamer.startStreaming("http://r35798.ovh.net:8000/listen",1677, 214);
streamButton.setEnabled(false);
} catch (IOException e) {
}
}
private void Inizializza()
{
textStreamed = (TextView) findViewById(R.id.text_kb_streamed);
streamButton = (Button) findViewById(R.id.button_stream);
streamButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startStreamingAudio();
}});
playButton = (ImageButton) findViewById(R.id.button_play);
playButton.setEnabled(false);
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (audioStreamer.getMediaPlayer().isPlaying()) {
audioStreamer.getMediaPlayer().pause();
playButton.setImageResource(R.drawable.button_play);
} else {
audioStreamer.getMediaPlayer().start();
audioStreamer.startPlayProgressUpdater();
playButton.setImageResource(R.drawable.button_pause);
}
isPlaying = !isPlaying;
}});
}
private class SwipeyTabsPagerAdapter extends FragmentPagerAdapter implements
SwipeyTabsAdapter {
private final Context mContext;
public SwipeyTabsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
this.mContext = context;
}
@Override
public Fragment getItem(int position) {
return SwipeyTabFragment.newInstance(TITLES[position]);
}
@Override
public int getCount() {
return TITLES.length;
}
public TextView getTab(final int position, SwipeyTabs root) {
TextView view = (TextView) LayoutInflater.from(mContext).inflate(
R.layout.swipey_tab_indicator, root, false);
view.setText(TITLES[position]);
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
});
return view;
}
}
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
//////////////////////////////////////
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Uscita").setOnMenuItemClickListener(
new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),
"Uscita Programma", Toast.LENGTH_SHORT).show();
finish();
System.exit(0);
return true;
}
});
;
return true;
}
class AsyncTaskProc extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... unused) {
List<Stream> streams=null;
Scraper scraper = new IceCastScraper();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
streams = scraper.scrape(new URI("@@@@@@"));
} catch (ScrapeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
}
textSong =(TextView) findViewById(R.id.textViewCurrentSong);
try {
for (Stream stream: streams) {
textSong.setText((stream.getCurrentSong()));
}
} catch (Exception e) {
//Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();
}
return (null);
}
}
}
編集: OnPostExecute() で Ui を移動しました。これは新しい logcat です。
010-30 21:22:03.535: E/Trace(4973): error opening trace file: No such file or directory (2)
10-30 21:22:05.145: E/AndroidRuntime(4973): FATAL EXCEPTION: AsyncTask #1
10-30 21:22:05.145: E/AndroidRuntime(4973): java.lang.RuntimeException: An error occured while executing doInBackground()
10-30 21:22:05.145: E/AndroidRuntime(4973): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-30 21:22:05.145: E/AndroidRuntime(4973): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-30 21:22:05.145: E/AndroidRuntime(4973): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-30 21:22:05.145: E/AndroidRuntime(4973): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-30 21:22:05.145: E/AndroidRuntime(4973): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-30 21:22:05.145: E/AndroidRuntime(4973): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-30 21:22:05.145: E/AndroidRuntime(4973): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-30 21:22:05.145: E/AndroidRuntime(4973): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-30 21:22:05.145: E/AndroidRuntime(4973): at java.lang.Thread.run(Thread.java:856)
10-30 21:22:05.145: E/AndroidRuntime(4973): Caused by: java.lang.Error: Unresolved compilation problem:
10-30 21:22:05.145: E/AndroidRuntime(4973): org.apache.commons.logging.LogFactory cannot be resolved to a type
10-30 21:22:05.145: E/AndroidRuntime(4973): at net.htmlparser.jericho.LoggerFactory.determineDefaultLoggerProvider(LoggerFactory.java:51)
10-30 21:22:05.145: E/AndroidRuntime(4973): at net.htmlparser.jericho.LoggerFactory.getDefaultLoggerProvider(LoggerFactory.java:39)
10-30 21:22:05.145: E/AndroidRuntime(4973): at net.htmlparser.jericho.LoggerFactory.getLoggerProvider(LoggerFactory.java:35)
10-30 21:22:05.145: E/AndroidRuntime(4973): at net.htmlparser.jericho.LoggerFactory.getLogger(LoggerFactory.java:27)
10-30 21:22:05.145: E/AndroidRuntime(4973): at net.htmlparser.jericho.Source.newLogger(Source.java:1645)
10-30 21:22:05.145: E/AndroidRuntime(4973): at net.htmlparser.jericho.Source.<init>(Source.java:109)
10-30 21:22:05.145: E/AndroidRuntime(4973): at net.moraleboost.streamscraper.parser.IceCastParser.parse(IceCastParser.java:67)
10-30 21:22:05.145: E/AndroidRuntime(4973): at net.moraleboost.streamscraper.scraper.IceCastScraper.scrape(IceCastScraper.java:65)
10-30 21:22:05.145: E/AndroidRuntime(4973): at com.uniradio.cesena.app.SwipeyTabsSampleActivity$AsyncTaskProc.doInBackground(SwipeyTabsSampleActivity.java:264)
10-30 21:22:05.145: E/AndroidRuntime(4973): at com.uniradio.cesena.app.SwipeyTabsSampleActivity$AsyncTaskProc.doInBackground(SwipeyTabsSampleActivity.java:1)
10-30 21:22:05.145: E/AndroidRuntime(4973): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-30 21:22:05.145: E/AndroidRuntime(4973): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-30 21:22:05.145: E/AndroidRuntime(4973): ... 5 more