java - 将活动复制到片段
问题描述
我对java很陌生,所以请原谅我的错误或误解。我正在尝试将我的代码行从活动转移到片段。但是,当我将粘贴复制到我的片段时出现错误。
我编辑的片段页面中的一些错误包括无法解析方法、无法解析构造函数和无法解析符号
活动代码
public class MainActivity extends AppCompatActivity
{
private SongCollection songCollection = new SongCollection();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void handleSelection(View view)
{
String resourceId = AppUtil.getResourceId(this, view);
Song selectedSong = songCollection.searchById(resourceId);
AppUtil.popMessage(this, "Streaming song: " + selectedSong.getTitle());
sendDataToActivity(selectedSong);
}
public void sendDataToActivity (Song song)
{
Intent intent = new Intent (this, 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);
}
}
未经编辑的片段代码
/**
* A simple {@link Fragment} subclass.
*/
public class TrendingFragment extends Fragment {
public TrendingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_trending, container, false);
return v;
}
}
编辑的片段代码
/**
* A simple {@link Fragment} subclass.
*/
public class TrendingFragment extends Fragment {
public TrendingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_trending, container, false);
public void handleSelection(View view)
{
String resourceId = AppUtil.getResourceId(this, view);
Song selectedSong = songCollection.searchById(resourceId);
AppUtil.popMessage(this, "Streaming song: " + selectedSong.getTitle());
sendDataToActivity(selectedSong);
}
public void sendDataToActivity (Song song)
{
Intent intent = new Intent (this, 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);
}
return v;
}
}
解决方案
您在方法中声明方法。这可能会有所帮助。
Song selectedSong;
public void handleSelection(View view)
{
String resourceId = AppUtil.getResourceId(this, view);
selectedSong = songCollection.searchById(resourceId);
AppUtil.popMessage(this, "Streaming song: " + selectedSong.getTitle());
sendDataToActivity(selectedSong);
}
public void sendDataToActivity (Song song)
{
Intent intent = new Intent (this, 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);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_trending, container, false);
handleSelection(v);
sendDataToActivity(selectedSong)
return v;
}
推荐阅读
- azure-maps - Azure Maps - 多边形边界框
- matlab - MATLAB 计算重叠的球体
- android - 片段内的 YouTubePlayerSupportFragment
- flutter - 无法在 Flutter 中为容器设置背景颜色
- ios - iOS:通过 UIActivityViewController 共享不适用于 UIActivityItemSource
- javascript - 根据 Id 映射对象数组
- qt - 在 QML 中控制带纹理的 3D 对象不透明度
- javascript - 使用 JS 读取 JSON 文件中的更改
- vue.js - 无法在 vue-nativescript 上使 vue-devtools 独立应用程序正常工作
- ibm-midrange - 可以在 COBOL400 中读取 SQLView 吗?