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