我對 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
提交
取消