Android 查看音频文件 NumberFormatException

Android View Audio Files NumberFormatException

我正在尝试查看 phone 上文件夹中的一些音频文件。 当我尝试使用 2 android 4.4 phones 进行测试时会发生此问题。 用android6.0,完全没有问题。 这是我的视图文件:

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


}

这是执行时得到的错误:

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

谁知道,请帮帮我。谢谢

错误在这一行:

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

不知何故 'currSong' 对象在持续时间内没有值。
这意味着当您执行 currSong.getDuration() 时,它返回 null.
当您尝试解析一个实际上是 null 的 long 时,它会抛出一个 NumberFormatException

我建议您在执行 Long.parseLong 之前先打印一张,以确认歌曲的时长是否为空。 此外,您可以将代码包装在 try-catch 子句中:

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

此致,