Search code examples
javaandroidgetview

Android View Audio Files NumberFormatException


I am trying to view some audio files in a folder on my phone. This problem happen when i trying to test with 2 android 4.4 phones. With android 6.0, there is no problem at all. This is my View file:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view = convertView;
    final ViewHolder holder;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.song, null);

        holder = new ViewHolder();

        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    holder.name=(TextView)view.findViewById(R.id.song_title);
    holder.artist=(TextView)view.findViewById(R.id.song_artist);
    holder.time=(TextView)view.findViewById(R.id.song_duration);
    holder.img_play=(LinearLayout)view.findViewById(R.id.playmusic_btn);
    holder.rb=(RadioButton)view.findViewById(R.id.radiobutton);
    holder.rb.setVisibility(View.GONE);
    final Song currSong = (Song)songs.get(position);
    holder.name.setText(currSong.getTitle());
    holder.artist.setText(currSong.getArtist());
    long l = Long.parseLong(currSong.getDuration());
    String obj1 = String.valueOf((l % 60000L) / 1000L);
    String obj2 = String.valueOf(l / 60000L);
    if (obj1.length() == 1)
    {
        holder.time.setText((new StringBuilder("0")).append(((String) (obj2))).append(":0").append(((String) (obj1))).toString());
    } else
    {
        holder.time.setText((new StringBuilder("0")).append(((String) (obj2))).append(":").append(((String) (obj1))).toString());
    }
    holder.img_play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            File view1 = new File(currSong.getPath());
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.setDataAndType(Uri.fromFile(view1), "audio/*");
            mContext.startActivity(intent);
        }
    });


    return view;
}

public class ViewHolder {

    public TextView name,artist,time;
    LinearLayout img_play;
    RadioButton rb;


}

This is the error obtained when executing :

10-27 23:22:59.324: E/AndroidRuntime(16003): java.lang.NumberFormatException: Invalid long: "null"

Anyone know, please help me. Thank you


Solution

  • The error is on this line :

    long l = Long.parseLong(currSong.getDuration());
    

    Somehow the 'currSong' object has no value for the duration.
    This means that when you perform currSong.getDuration(), it is returning null.
    When you try to parse a long that is in fact a null, it will throw a NumberFormatException

    I suggest you take a print before executing the Long.parseLong to confirm if in fact the duration of the song is null. Additionally, you can wrap your code on a try-catch clause :

    long l;
    try {
        l = Long.parseLong(currSong.getDuration());
    } catch(NumberFormatException e){
        e.printStackTrace();
    }
    

    Regards,