Java 在textview上显示字符串不起作用

Java 在textview上显示字符串不起作用,java,android,xml,textview,Java,Android,Xml,Textview,我是android studio的新手,请容忍我的任何误解。我想在名为歌词视图的文本视图中显示一串音乐歌词,该文本视图位于我的fragment\u lyres.xml文件中。我已经环顾了很多地方,并遵循了一些类似的例子,但没有任何运气解决我的问题。这是我的一些代码。当我点击播放按钮时,一首歌曲会通过Spotify api播放,我还想使用musixMatch api将这些歌词显示到歌词视图中。如果需要,我可以提供更多的代码。我是否要正确显示这个字符串?还是有一种完全不同的方式?感谢您在这篇长文章中

我是android studio的新手,请容忍我的任何误解。我想在名为歌词视图的文本视图中显示一串音乐歌词,该文本视图位于我的fragment\u lyres.xml文件中。我已经环顾了很多地方,并遵循了一些类似的例子,但没有任何运气解决我的问题。这是我的一些代码。当我点击播放按钮时,一首歌曲会通过Spotify api播放,我还想使用musixMatch api将这些歌词显示到歌词视图中。如果需要,我可以提供更多的代码。我是否要正确显示这个字符串?还是有一种完全不同的方式?感谢您在这篇长文章中付出的努力

playSongButton(),它位于我的MainActivity类中。它负责播放歌曲并调用calculateLyrics()方法

计算歌词()哪个在歌词类中

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;
}
活动\u 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>
<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>

/>
fragment_-lyris.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>
<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>

您需要将查询结果过滤到musixmatch。当我使用艺术家ChainStampers和track Paris运行curl命令时,您将收到musixmatch返回的20首曲目(大部分是俱乐部混音)。您必须编写一些代码,以便使用用户文档中提到的过滤器参数(f_uu输入参数)查找/过滤结果,使其成为您想要显示的结果


卷曲: curl--url“


部分输出:
{“消息”:{“标题”:{“状态代码”:200,“执行时间”:0.019639015197754,“可用”:20},“正文”:{“曲目列表”:[{“曲目id”:173322884,“曲目名称”:“巴黎(混合)”,“曲目名称翻译列表”:[],“曲目评级”:1,“公共曲目id”:96669304,“器乐”:0,“明确”:0,“有歌词”:0,“有字幕”:0,“有丰富的同步”:0,“最喜欢的曲目”:0专辑编号:32901374,“专辑名称”:“让我们跳舞,第二卷(DJ混音)”,“艺术家编号”:26490468,“艺术家名称”:“烟民”、“曲目分享”url:“https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed?utm_source=application&utm_campaign=api&utm_medium=,“跟踪\编辑\ url”:https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed/edit?utm_source=application&utm_campaign=api&utm_medium=“限制”:0,“更新时间”:“2019-06-12T15:47:28Z”,“主要流派”:{“音乐流派列表”:[]}}},{“曲目”:{“曲目id”:126251071,“曲目名称”:“巴黎(Sbm X Geru混音)”,“曲目名称翻译列表”:[],“曲目评级”:5,”commontrack_id“:69979021,“器乐”:0,“显式”:0,“有歌词”:1,“有字幕”:0,“有丰富的内容”

您需要过滤对musixmatch的查询结果。当我与艺术家Chainsmokers和track Paris一起运行curl命令时,您会收到musixmatch返回的20首曲目(大部分是俱乐部混音)。您必须编写一些代码,以使用用户文档中提到的筛选参数(f_u输入参数)查找/筛选结果,并将其筛选为要显示的结果


卷曲: curl--url“


部分输出:
{“消息”:{“标题”:{“状态代码”:200,“执行时间”:0.019639015197754,“可用”:20},“正文”:{“曲目列表”:[{“曲目id”:173322884,“曲目名称”:“巴黎(混合)”,“曲目名称翻译列表”:[],“曲目评级”:1,“公共曲目id”:96669304,“器乐”:0,“明确”:0,“有歌词”:0,“有字幕”:0,“有丰富的同步”:0,“最喜欢的曲目”:0专辑编号:32901374,“专辑名称”:“让我们跳舞,第二卷(DJ混音)”,“艺术家编号”:26490468,“艺术家名称”:“烟民”、“曲目分享”url:“https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed?utm_source=application&utm_campaign=api&utm_medium=,“跟踪\编辑\ url”:https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed/edit?utm_source=application&utm_campaign=api&utm_medium=“限制”:0,“更新时间”:“2019-06-12T15:47:28Z”,“主要流派”:{“音乐流派列表”:[]}}},{“曲目”:{“曲目id”:126251071,“曲目名称”:“巴黎(Sbm X Geru混音)”,“曲目名称翻译列表”:[],“曲目评级”:5,”commontrack_id:69979021,“器乐”:0,“显式”:0,“有歌词”:1,“有字幕”:0我看到你写了太多不相关的文本,你不必添加代码的概念,只需从技术上描述问题,无论如何,我几乎可以肯定问题不在于将文本设置为文本视图,而在于
calculateLyrics()
。您调试了代码吗?它是否总是返回值?您是对的,这就是问题所在。感谢您指出这一点,确保CalculateRYRICS()正确返回正确的内容。如果其中有一些异常。因此TextView不会显示任何内容。@jimbray当我调试它时,它会说track、trackData、trackId和歌词为空。但是我可以在eclipse中运行相同的代码并将歌词成功打印到控制台。我看到您编写了太多不相关的文本,您不必添加代码仅从技术上描述了问题,无论如何,我几乎可以肯定问题不在于将文本设置为文本视图,而在于
calculateryrics()
。您调试了代码吗?它是否总是返回值?您是对的,这就是问题所在。感谢您指出这一点,确保calculateryrics()正确返回正确的内容。如果其中有一些异常。因此TextView不会显示任何内容。@jimbray当我调试它时,它会说track、trackData、trackId和歌词为空。但我可以在eclipse中运行相同的代码,并将歌词成功打印到控制台Hi Constantin,每个部分都由可见的行分隔。我假设您正在讲话关于我在最后包含的JSON数据示例?这就是JSON的外观,并且有多种工具可以对其进行格式化。因为JSON数据就像关系数据库中的关系数据库,所以很难进行格式化。它不意味着可读?它是数据输出。嗨,康斯坦丁,每个部分都由可见的行分隔。我假设你是在对我在最后包含的JSON数据样本说话吗?