Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 由于NullPointerException,应用程序意外停止_Android_Textview - Fatal编程技术网

Android 由于NullPointerException,应用程序意外停止

Android 由于NullPointerException,应用程序意外停止,android,textview,Android,Textview,我试图在java类中向文本视图显示字符串的值。但当我运行我的应用程序时,它显示我的应用程序意外停止。它不能正常工作,请建议我一个最好的方法来做到这一点 我的代码: protected void onListItemClick(ListView l, View v, int position, long id){ currentPosition = position; playSong(MEDIA_PATH + songs.get(position)); //Toast.

我试图在java类中向文本视图显示字符串的值。但当我运行我的应用程序时,它显示我的应用程序意外停止。它不能正常工作,请建议我一个最好的方法来做到这一点

我的代码:

protected void onListItemClick(ListView l, View v, int position, long id){
    currentPosition = position;
    playSong(MEDIA_PATH + songs.get(position));

    //Toast.makeText(getApplicationContext(), songs.get(position).toString(), Toast.LENGTH_SHORT).show();

    Intent in = new Intent(this, current_song.class);
    startActivity(in);

    //Toast.makeText(getApplicationContext(), "End of Song List", Toast.LENGTH_SHORT).show();
    final String songName = songs.get(position).toString();
    final TextView textchange = (TextView)findViewById(R.id.current_song_name);
    textchange.setText(songName);

}
布局xml:

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >

<Button 
    android:id="@+id/play_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play"
    />

<TextView 
    android:id="@+id/current_song_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

    </LinearLayout>

嗯,如果这三行在
onListItemClick()
中,请尝试:

final TextView textchange = (TextView) v.findViewById(R.id.current_song_name);
// Scope findVIewById to the the View ^^^ 
(如果这不是答案,请指出哪一行是71。)

也尝试一下:

final String songName = songs.get(position).toString();

并确保“位置”

您正试图从列表活动中执行此操作

final TextView textchange = (TextView)findViewById(R.id.current_song_name);
textchange.setText(songName);
但是这个视图是在您在
current_song.java
活动中使用的xml中声明的

不能将一个活动的视图对象用于其他活动

通过bundle extras将
songName
的值传递给
current_-song
活动,然后在
current_-song
活动中设置该
songName

从您的列表活动:

protected void onListItemClick(ListView l, View v, int position, long id){
    currentPosition = position;
    final String songName = songs.get(position).toString();
    playSong(MEDIA_PATH + songs.get(position));
    Intent in = new Intent(this, current_song.class);
    in.putExtra("song_name",songName );
    startActivity(in);
}
Bundle extras = getIntent().getExtras();

if (extras != null) {
    String songName = extras.getString("song_name");
    TextView textchange = (TextView)findViewById(R.id.current_song_name);
    textchange.setText(songName);
}
在您当前的歌曲活动中:

protected void onListItemClick(ListView l, View v, int position, long id){
    currentPosition = position;
    final String songName = songs.get(position).toString();
    playSong(MEDIA_PATH + songs.get(position));
    Intent in = new Intent(this, current_song.class);
    in.putExtra("song_name",songName );
    startActivity(in);
}
Bundle extras = getIntent().getExtras();

if (extras != null) {
    String songName = extras.getString("song_name");
    TextView textchange = (TextView)findViewById(R.id.current_song_name);
    textchange.setText(songName);
}

看到
com.example.musicplayer.MainActivity.onListItemClick的全部内容会很高兴,包括哪一行是第71行第71行是textchange.setText(songName)@Anu0042能否显示当前歌曲.java活动使用的xml文件?@Anu0042请查看我编辑的答案以获取解决问题的完整示例。第71行是textchange.setText(歌曲名称);你试过我的建议了吗?在调用
startActivity()
之前,您可能需要执行此操作。是的,但不起作用我没有收到错误,但页面不是xml页面不包含歌曲名称我很抱歉,我不理解您所说的。。。您是否试图在您的
当前歌曲
活动中显示歌曲名称?您无法访问其他活动的布局。你必须这样做,最简单的方法是使用一个捆绑包。非常感谢,太棒了。1更多帮助你能告诉我如何在html中像移动字幕一样移动此文本吗。我在问这样一个愚蠢的问题,因为我对安卓系统完全陌生。请帮助我。@Anu0042请查看此链接以获取类似字幕的文本[]。