0

我正在尝试将字符串的值显示到 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>

日志:

01-26 22:29:16.599: I/Process(531): Sending signal. PID: 531 SIG: 9
01-26 22:29:28.679: D/AndroidRuntime(602): Shutting down VM
01-26 22:29:28.679: W/dalvikvm(602): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-26 22:29:28.750: E/AndroidRuntime(602): FATAL EXCEPTION: main
01-26 22:29:28.750: E/AndroidRuntime(602): java.lang.NullPointerException
01-26 22:29:28.750: E/AndroidRuntime(602):  at    com.example.musicplayer.MainActivity.onListItemClick(MainActivity.java:71)
01-26 22:29:28.750: E/AndroidRuntime(602):  at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
01-26 22:29:28.750: E/AndroidRuntime(602):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
01-26 22:29:28.750: E/AndroidRuntime(602):  at android.widget.ListView.performItemClick(ListView.java:3513)
01-26 22:29:28.750: E/AndroidRuntime(602):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
01-26 22:29:28.750: E/AndroidRuntime(602):  at android.os.Handler.handleCallback(Handler.java:587)
01-26 22:29:28.750: E/AndroidRuntime(602):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 22:29:28.750: E/AndroidRuntime(602):  at android.os.Looper.loop(Looper.java:123)
01-26 22:29:28.750: E/AndroidRuntime(602):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-26 22:29:28.750: E/AndroidRuntime(602):  at java.lang.reflect.Method.invokeNative(Native Method)
01-26 22:29:28.750: E/AndroidRuntime(602):  at java.lang.reflect.Method.invoke(Method.java:507)
01-26 22:29:28.750: E/AndroidRuntime(602):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-26 22:29:28.750: E/AndroidRuntime(602):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-26 22:29:28.750: E/AndroidRuntime(602):  at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

2

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

final TextView textchange = (TextView) v.findViewById(R.id.current_song_name);
// Scope findVIewById to the the View ^^^ 

(如果这不是答案,请指出哪一行是 71。)

于 2013-01-26T17:15:58.427 回答
0

也试试这个:

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

并确保“位置”

于 2013-01-26T17:36:18.560 回答
0

您正在尝试从您的列表活动中执行此操作,

final TextView textchange = (TextView)findViewById(R.id.current_song_name);
textchange.setText(songName);

但是这个视图是在你在current_song.javaActivity 中使用的 xml 中声明的。

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

songName将with bundle extras的值传递给current_song有意图的活动,然后songName在您的current_song活动中设置它。

从您的列表活动中:

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);
}

在您的 current_song 活动中:

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);
}
于 2013-01-26T18:14:47.463 回答