2016-10-27 8 views
1

携帯電話のフォルダにあるオーディオファイルを表示しようとしています。 この問題は、2台のアンドロイド4.4の電話機でテストしようとしたときに発生します。 アンドロイド6.0では、まったく問題ありません。AndroidビューのオーディオファイルNumberFormatException

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

誰もが知っている、私を助けてください。あなた

答えて

0

をありがとうございエラーは、この行にある:

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

は、どういうわけか「currSong」オブジェクトは、期間値を持ちません。
つまり、currSong.getDuration()を実行すると、nullが返されます。
あなたが実際にnullである長いを解析しようとすると、それは私はあなたが実際に歌の期間がnullであるかどうかを確認するためにLong.parseLongを実行する前に、印刷を取ることをお勧めNumberFormatException

がスローされます。 また、あなたはtry-catch句にあなたのコードをラップすることができます

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

よろしく、

+1

あなたは私の人生の救世主です、非常に多くの@Ricardo、ありがとうございました。 –

関連する問題