我对 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; }}
1 回答
德玛西亚99
TA贡献1770条经验 获得超3个赞
根据您需要实现按钮功能的注释。我假设 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
添加回答
举报
0/150
提交
取消