我对Java还是比较陌生,希望能提供一些帮助。我试图在一个片段中使用方法(handleSelection方法),但该方法似乎不起作用,而是强调未使用该方法。
public class TrendingFragment extends Fragment {
Song selectedSong;
public void handleSelection(View view)
{
String resourceId = AppUtil.getResourceId(getActivity(),view);
selectedSong = songCollection.searchById(resourceId);
AppUtil.popMessage(getActivity(), "Streaming song: " + selectedSong.getTitle());
sendDataToActivity(selectedSong);
}
public void sendDataToActivity (Song song)
{
Intent intent = new Intent(getActivity(), PlaySongActivity.class);
intent.putExtra("id", song.getId());
intent.putExtra("title", song.getTitle());
intent.putExtra("artist", song.getartist());
intent.putExtra("fileLink" ,song.getFileLink());
intent.putExtra("coverArt", song.getCoverArt());
startActivity(intent);
}
private SongCollection songCollection = new SongCollection();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_trending, container, false);
return v;
}
}
答案 0 :(得分:1)
基于注释,您需要实现按钮的功能。我假设Button在Fragment的视图内。这应该是您的oncreateview函数。
View v = inflater.inflate(R.layout.fragment_trending, container, false);
button = findViewById(R.id...):
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handleSelection(view);
}
});
return v;
您需要在Fragment中实现一个按钮变量,然后为按钮输入正确的ID