如何修复无法解析方法'findViewById'

问题描述 投票:0回答:3

我正在使用片段构建一个无线电流媒体应用程序,我遇到了问题:

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);

该应用程序因错误而无法运行,我想解决这个问题。

java android android-fragments streaming
3个回答
0
投票

将您的视图添加到此行

b_play = (Button) view.findViewById(R.id.b_play);

0
投票

Tommy和MartijndeM我在RadioFragment的视图实例上调用了findViewById,现在有更多错误。在“b_play.setOnClickListener(new View.OnClickListener()”行上,预计会出现错误“class”或“interface”,并且会出现更多错误。以下是代码:

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();
}
}
}

0
投票

你必须在findViewByIdview实例上调用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;
    }
}

更新:根据作者的代码。

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");

        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");

                }
            }
        });

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        new PlayerTask().execute(stream);

        return view;

    }

    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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.