6

编码

SngList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView a, View v, int position, long id) {


        Intent intent = new Intent(getActivity(), NowPlaying.class);
        intent.putExtra("Data1",Songinfo);
        intent.putExtra("Data2",position);
        startActivity(intent);

    }
});

接收类中的代码

Intent i = getIntent();
ArrayList<SongDetails> Songinfo2 = (ArrayList<SongDetails>)i.getSerializableExtra("Data1"); 
position=i.getIntExtra("Data2", 1);

歌曲详细信息的代码

package sourcecode.jazzplayer;

import java.io.Serializable;

import android.graphics.Bitmap;

public class SongDetails implements Serializable{
    Bitmap icon ;
    String song;
    String Artist; 
    String Album;
    String Path;
   int icLauncher;

    public String getSong() {
        return song;
    }

    public void setSong(String song) {
        this.song = song;
    }

    public String getArtist() {
        return Artist;
    }

    public void setArtist(String Artist) {
        this.Artist = Artist;
    }

    public Bitmap getIcon() {
        return icon;
    }

    public void setIcon(Bitmap bitmap) {
        this.icon = bitmap;
    }

    public String getPath2() {
        return Path;
    }

    public void setPath2(String Path) {
        this.Path = Path;
    }

    public String getAlbum() {
        return Album;
    }

    public void setAlbum(String Album) {
        this.Album = Album;
    }

    public void setIcon(int icLauncher) {
        this.icLauncher = icLauncher;
    }
}

整个代码:

public class FragmentSongs extends  Fragment implements Serializable {
    AdapterView.AdapterContextMenuInfo info;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {

        View view = inflater.inflate(R.layout.fragment_song, container, false);
        ListView SngList = (ListView) view.findViewById(R.id.SongList);
        registerForContextMenu(SngList);
        File f=new File("/system/");
        //File f=new File("/sdcard/Music");
        int j=0;int i=0;



         final ArrayList<SongDetails> Songinfo = getSongsFromDirectory(f);

        if (Songinfo.size()>0)
        {

            for( j=0; j<Songinfo.size();j++)
            {
                for ( i=j+1 ; i<Songinfo.size(); i++)
                { 
                    SongDetails a=Songinfo.get(i);
                    SongDetails b=Songinfo.get(j);
                    if(a.getSong().toLowerCase().compareTo(b.getSong().toLowerCase())<0)
                    {   

                        Songinfo.set(i,b );
                        Songinfo.set(j,a);
                    }
                }

            }

            SngList.setOnItemClickListener(new OnItemClickListener() {
                 public void onItemClick(AdapterView a, View v, int position, long id) {


                       Intent intent = new Intent(getActivity(), NowPlaying.class);
                       intent.putExtra("Data1",Songinfo);
                       intent.putExtra("Data2",position);
                       startActivity(intent);

                             }
                     });

           SngList.setAdapter(new CustomAdapter(Songinfo));
           return view;
        }
        else return null;

    }



        public ArrayList<SongDetails> getSongsFromDirectory(File f)
        {MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            ArrayList<SongDetails> songs = new ArrayList<SongDetails>();
            Bitmap bitmap2; 
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ab);

            float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
            float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());

            bitmap2 = Bitmap.createScaledBitmap(bmp, (int) ht_px, (int) wt_px, true);



           byte[] rawArt = null;
            Bitmap art;
            BitmapFactory.Options bfo=new BitmapFactory.Options();
            if (!f.exists() || !f.isDirectory()) 

            {    
                return songs;
            }
            File[] files = f.listFiles(new Mp3Filter());
            for(int i=0; i<files.length; i++) 
            { 

                if (files[i].isFile())
                { 


                 //mmr.setDataSource(files[i].getPath());
                // rawArt = mmr.getEmbeddedPicture();
                    SongDetails detail=new SongDetails(); 
                //if ( rawArt != null) 

                //{ 
                    //bitmap2=BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
                    //bitmap2 = Bitmap.createScaledBitmap(bitmap2, (int) ht_px, (int) wt_px, true);

                    //detail.setIcon(bitmap2);
                //}//else 
            //{     
                    detail.setIcon(bitmap2);
                //} 

                    detail.setSong(files[i].getName()); 
          // detail.setArtist(files[i].getName());
          // detail.setAlbum(files[i].getName());

             //detail.setArtist(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)); 

          // detail.setAlbum(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)); 
           detail.setPath2( files[i].getPath()) ;
           songs.add(detail); 
                }
                else if (files[i].isDirectory())
                { 
                songs.addAll(getSongsFromDirectory(files[i])); 
                } 

            }       return songs;
    }
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
        {
                        super.onCreateContextMenu(menu, v, menuInfo);      
                       info = (AdapterContextMenuInfo) menuInfo;
                       menu.add(Menu.NONE, v.getId(), 0, "Play");
                       menu.add(Menu.NONE, v.getId(), 0, "Delete");
                       menu.add(Menu.NONE, v.getId(), 0, "Queue Item");                  
               }

        @Override
        public boolean onContextItemSelected(MenuItem item) {
                if (item.getTitle() == "Play") {

                       }
                 else if (item.getTitle() == "Delete") {

                       }

                 else if (item.getTitle() == "Queue Item") {

                       }
                 else     {
                       return false;
                       }
               return true;
               }}


        class Mp3Filter implements FileFilter
        {
            public boolean accept(File file)
            {
            return (file.isDirectory()||file.getName().endsWith(".apk")|| file.getName().endsWith(".Mp3"));
            }
        }

日志

java.lang.RuntimeException: Parcelable 遇到写入可序列化对象的 IOException (name = sourcecode.jazzplayer.SongDetails)

4

5 回答 5

9

好的,我为您实现了其中的一部分。您必须添加 SongDetails 类的所有其他属性:

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SongDetails Songinfo1 = new SongDetails();
    Songinfo1.setSong("song1");

    SongDetails Songinfo2 = new SongDetails();
    Songinfo2.setSong("song2");

    ArrayList<SongDetails> list = new ArrayList<SongDetails>();
    list.add(Songinfo1);
    list.add(Songinfo2);

    Intent intent = new Intent(this, SecondActivity.class);
    intent.putParcelableArrayListExtra("Data1", list);
    intent.putExtra("Data2", 1);
    startActivity(intent);

}

在检索歌曲的活动中,使用以下命令:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_asdf);

    ArrayList<SongDetails> songs = getIntent().getParcelableArrayListExtra("Data1");

    for(SongDetails songDetails : songs) {
        Log.i("", songDetails.getSong());
    }
}

您的 SongDetails 类应如下所示:

歌曲详情:

public class SongDetails implements Parcelable {
    Bitmap icon;
    String song;
    String Artist;
    String Album;
    String Path;
    int icLauncher;

    public SongDetails() {
    }

    public SongDetails(Parcel in) {
        String[] data = new String[1];
        in.readStringArray(data);
        this.song = data[0];
    }

    public String getSong() {
        return song;
    }

    public void setSong(String song) {
        this.song = song;
    }

    public String getArtist() {
        return Artist;
    }

    public void setArtist(String Artist) {
        this.Artist = Artist;
    }

    public Bitmap getIcon() {
        return icon;
    }

    public void setIcon(Bitmap bitmap) {
        this.icon = bitmap;
    }

    public String getPath2() {
        return Path;
    }

    public void setPath2(String Path) {
        this.Path = Path;
    }

    public String getAlbum() {
        return Album;
    }

    public void setAlbum(String Album) {
        this.Album = Album;
    }

    public void setIcon(int icLauncher) {
        this.icLauncher = icLauncher;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStringArray(new String[] { this.song });
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public SongDetails createFromParcel(Parcel in) {
            return new SongDetails(in);
        }

        public SongDetails[] newArray(int size) {
            return new SongDetails[size];
        }
    };
}
于 2013-08-09T14:47:25.377 回答
1

根据https://forums.bignerdranch.com/t/challenge-saving-state-notserializableexception-error/8018/5,您应该从Parcelable. 就我而言,我有一个 DialogFragment,其接口 MyCallback 扩展了 Serializable。在newInstance(MyCallback callback)我使用的构建器中:

Bundle args = new Bundle();
args.putSerializable(key, callback);

这导致了一个例外。然后我重写了 MyCallback 以扩展Parcelable并在调用此 DialogFragment 时向回调添加了一些方法。至少,它不会在主页按钮或屏幕关闭时崩溃。

也改为:

Bundle args = new Bundle();
args.putParcelable(key, callback);
于 2016-09-09T11:20:13.427 回答
0

在我的例子中,实际类 Order(实现 Serializable)中有一个嵌套的 Delivery 类,它没有被序列化,一旦 Delivery 类也实现了 Serializable,异常就消失了。我根本不需要实现 parcelable 。希望这对某人有帮助!

于 2020-05-21T15:19:37.647 回答
0

我知道您的问题出在哪里,那就是位图 您需要做的就是在发送到意图之前解码您的位图

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.thumbsup);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        largeIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] byteArray = stream.toByteArray();

然后意图发送以下对象

intent.putExtras("remindermessage",object);

如果不是关于位图,那么你应该寻找其他可能占用更多空间的东西并在发送到意图之前对其进行解码

于 2017-08-25T11:58:04.463 回答
-1

从另一个活动中获取数组列表。

      ArrayList<SongDetails> list = new ArrayList<SongDetails>();
              list=  getIntent().getStringArrayListExtra("Data1");

将数组列表传递给另一个活动。

intent.putStringArrayListExtra("Data1", Songinfo);

要拆分数组列表:

        for(SongDetails name: list)
        {
        String yoursong= name.song;
        }

希望这会给你一些解决方案。

于 2013-08-09T13:41:27.650 回答