我在 Song.java 中创建了函数,但无法调用 getSinger()
和 getTitle()
来自宋和getmImageId()
来自 Song,我有 MainActivity,我在其中创建了启动 SongDisplay Activity 的 Intent 。
那么如何为 seekBar 显示歌曲的持续时间(以分钟为单位)。
Song.java
public class Song {
private String mTitle;
private String mDuration;
private String mSinger;
private int mImageId;
public Song(String title, String duration, String singer,int imageId) {
mTitle = title;
mDuration = duration;
mSinger = singer;
mImageId = imageId;
}
public String getTitle() {
return mTitle;
}
public String getDuration() {
return mDuration;
}
public String getSinger() { return mSinger; }
public int getmImageId() {
return mImageId;
}
}
SongDisplay.java
public class SongDisplay extends AppCompatActivity {
ImageView imageDisplay;
TextView singerText;
TextView songName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
imageDisplay = (ImageView) findViewById(R.id.image_display);
singerText = (TextView)findViewById(R.id.singer_text);
singerText.setText(String .valueOf(getSinger()));
songName = (TextView)findViewById(R.id.title_text);
}
}
最佳答案
需要有一个使用 new 运算符和构造函数创建的 Song
类的实例。这将允许您调用类上的方法以通过它们的访问器检索存储在字段中的值。检索到的值将特定于该类的特定实例。如果您需要另一首歌曲,它将是该类的一个单独实例。
例如:
@Override
protected void onCreate(Bundle savedInstanceState) {
//Instance created here
Song song = new Song("Imagine", "3:14", "Beatles", 1);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
imageDisplay = (ImageView) findViewById(R.id.image_display);
singerText = (TextView)findViewById(R.id.singer_text);
//method invoked on instance here
singerText.setText(song.getSinger());
songName = (TextView)findViewById(R.id.title_text);
}
如果您有兴趣了解更多信息,我在 lesson 中介绍了类实例在我的个人博客上找到。
关于java - 我正在准备一个音乐应用程序以及如何将函数从 Song.java 调用到 SongDisplay.java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48421179/