0

I am trying to display the value of a string to a text View in java class. But when i run my app it is showing that my application has stopped unexpectedly. It is not working properly please suggest me a best way to do this.

My code:

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

}

Layout 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>

Log:

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)
Anu0042
  • 253
  • 4
  • 12

3 Answers3

2

Hmm, if those three lines are in onListItemClick(), try:

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

(If this isn't the answer, please indicate which line is 71.)

Sam
  • 86,580
  • 20
  • 181
  • 179
  • line no 71 is textchange.setText(songName); – Anu0042 Jan 26 '13 at 17:42
  • Did you try my suggestion? Also you may need to do this before calling `startActivity()`. – Sam Jan 26 '13 at 17:49
  • yes but is not working i am not getting the error but the page is not xml page do not contain song name – Anu0042 Jan 26 '13 at 18:06
  • I'm sorry I didn't understand what you said... Are you trying to display the song name in your `current_song` Activity? – Sam Jan 26 '13 at 18:12
  • You cannot access the layout of another Activity. You must [pass the song name to `current_song`](http://stackoverflow.com/q/2091465/1267661), the easiest way is to use a Bundle. – Sam Jan 26 '13 at 18:22
0

Try this too :

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

and make sure about the "position"

Oubaida AlQuraan
  • 1,706
  • 1
  • 18
  • 19
0

You are trying to do this from your list activity,

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

But this view is declared in the xml which you are using in current_song.java Activity.

You can't use view objects of one activity to other.

Pass the value of songName with bundle extras to current_song activity with intent and then set that songName in your current_song activity.

From your List activity :

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

In your current_song activity :

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);
}
Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • thanks a lot that was great. 1 more help can you tell me how to move this text like marque in html. I am asking this kind of silly question bec i am totally new to Android. please help me. – Anu0042 Jan 26 '13 at 18:38
  • @Anu0042 see this link for marquee like text [ http://stackoverflow.com/a/14067328/556975 ]. – Pratik Sharma Jan 26 '13 at 18:40