【发布时间】:2023-03-18 18:48:01
【问题描述】:
我是 android studio 的新手,如有任何误解,请多多包涵。我想在我的 fragment_lyrics.xml 文件中名为 lyrics_view 的文本视图中显示一串音乐歌词。我环顾四周并遵循了一些类似的示例,但没有任何运气解决我的问题。这是我的一些代码。当我点击播放按钮时,一首歌曲通过 Spotify api 播放,我还想使用 musixMatch api 将这些歌词显示到 lyrics_view。如果需要,我可以提供更多代码。我什至要正确显示这个字符串吗?还是有完全不同的方式我应该这样做?感谢您为长帖子付出的努力。
playSongButton() 在我的 MainActivity 类中。它负责播放歌曲并调用 calculateLyrics() 方法。
public void playSongButton(View view) {
try {
mPlayer.playUri(null, "spotify:track:15vzANxN8G9wWfwAJLLMCg", 0, 0);
//where i believe i am having issues
TextView textView = null;
TextView displayTextView = (TextView) findViewById(R.id.lyrics_view);
String lyricString = songLyrics.calculateLyrics();
displayTextView.setText(lyricString);
} catch (Exception e) {
e.printStackTrace();
}
}
CalculateLyrics() 属于 SongLyrics 类
public SongLyrics(String songName, String artistName) {
this.songName = "Paris";
this.artistName = "The Chainsmokers";
}
public String calculateLyrics() {
try {
musixMatch = new MusixMatch(key.getKey());
track = musixMatch.getMatchingTrack(songName, artistName);
trackData = track.getTrack();
trackId = trackData.getTrackId();
lyrics = musixMatch.getLyrics(trackId);
lyricString = lyrics.getLyricsBody();
} catch (Exception e) {
e.printStackTrace();
}
return lyricString;
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1"
app:layout_collapseParallaxMultiplier="1.0">
<EditText
android:id="@+id/songName"
android:layout_width="0dp"
android:layout_height="56dp"
android:ems="10"
android:hint="@string/enter_song"
android:inputType="text"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
android:layout_marginTop="88dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="6dp" />
<EditText
android:id="@+id/artistName"
android:layout_width="0dp"
android:layout_height="55dp"
android:ems="10"
android:hint="@string/enter_artist"
android:inputType="text"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Display1"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
tools:layout_editor_absoluteX="0dp"
app:layout_constraintTop_toBottomOf="@+id/songName" />
<TextView
android:text="Enter Song Info"
android:layout_width="wrap_content"
android:layout_height="69dp"
android:id="@+id/enterSongInfo"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="80dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="80dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="1.0"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<Button
android:text="Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playButton"
android:onClick="playSongButton"
android:layout_marginStart="296dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="296dp"
android:layout_marginTop="88dp"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:text="Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pauseButton"
android:onClick="pauseSongButton"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/playButton"
android:layout_marginStart="296dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="296dp" />
<fragment
android:id="@+id/lyrics"
android:layout_width="353dp"
android:layout_height="329dp"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginTop="208dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.71"
class="com.example.joeberg.jams.LyricsFragment"/>
/>
</android.support.constraint.ConstraintLayout>
fragment_lyrics.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<TextView
android:text="Lyrics"
android:layout_width="93dp"
android:layout_height="59dp"
android:id="@+id/lyrics_heading"
class="com.example.joeberg.jams.LyricsFragment"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="280dp"
android:textColor="@color/common_google_signin_btn_text_dark_focused" />
<TextView
class="com.example.joeberg.jams.LyricsFragment"
android:layout_width="335dp"
android:layout_height="271dp"
android:id="@+id/lyrics_view"
app:layout_constraintTop_toBottomOf="@+id/lyrics_heading"
android:textAppearance="@style/TextAppearance.AppCompat"
android:layout_marginStart="32dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="32dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintHorizontal_bias="0.52" />
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
我看到你写了太多无关的文字,你不必添加代码的想法,只从技术上描述问题,反正我几乎可以确定问题不在设置中文本到文本视图,但它位于
calculateLyrics()。你调试过代码吗?它总是返回值吗? -
你是对的,这就是问题所在。感谢您指出这一点
-
确保 calculateLyrics() 返回正确的内容。如果有一些例外。所以 TextView 不显示任何内容。
-
@jimbray 当我调试它时,它说 track、trackData、trackId 和歌词为空。但是我可以在eclipse中运行相同的代码,并成功将歌词打印到控制台