我正在使用片段构建广播流应用程序,但在此过程中遇到了问题:
b_play = (Button) findViewById(R.id.b_play);
我尝试了“在'RadioFragment'中为'findViewById'创建方法
b_play = (Button) findViewById(R.id.b_play);
b_play.setEnabled(false);
b_play.setText("LOADING");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
该应用由于错误而无法运行,我想解决该问题。
答案 0 :(得分:0)
将您的视图添加到此行
b_play = (Button) view.findViewById(R.id.b_play);
答案 1 :(得分:0)
您必须在findViewById
的{{1}}实例上调用view
。
RadioFragment
更新:基于作者的代码。
public class RadioFragment extends Fragment {
Button b_play;
MediaPlayer mediaPlayer;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_radio, container, false);
b_play = (Button) view.findViewById(R.id.b_play);
b_play.setEnabled(false);
b_play.setText("LOADING");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
return view;
}
}
答案 2 :(得分:0)
Tommy和MartijndeM我在RadioFragment的视图实例上调用了findViewById,现在有更多错误。在“ b_play.setOnClickListener(new View.OnClickListener()”行上,预期将出现错误“类”或“接口”,并且会有更多错误。这是代码:
package com.urbannet.radioindico;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.io.IOException;
public class RadioFragment extends Fragment {
Button b_play;
MediaPlayer mediaPlayer;
boolean prepared = false;
boolean started = false;
String stream = "http://streaming.radiodifusao.org:8110/stream?type=.mp3";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_radio, container, false);
b_play = (Button) view.findViewById(R.id.b_play);
b_play.setEnabled(false);
b_play.setText("LOADING");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
return view;
}
}
b_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (started) {
started = false;
mediaPlayer.pause();
b_play.setText("PLAY");
} else {
started = true;
mediaPlayer.start();
b_play.setText("PAUSE");
}
}
});
}
class PlayerTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
b_play.setEnabled(true);
b_play.setText("PLAY");
}
}
@Override
public void onPause() {
super.onPause();
if (started) {
mediaPlayer.pause();
}
}
@Override
public void onResume() {
super.onResume();
if (started) {
mediaPlayer.start();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (prepared) {
mediaPlayer.release();
}
}
}