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

时间:2013-01-26 17:14:08

标签: android textview

我正在尝试将字符串的值显示在java类的文本View中。但是,当我运行我的应用程序时,它显示我的应用程序意外停止。它工作不正常请告诉我最好的方法。

我的代码:

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)

3 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

试试这个:

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

并确保“位置”

答案 2 :(得分:0)

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

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

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

您无法将一项活动的视图对象用于其他活动。

将带有附加内容的songName的值传递给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);
}