我是新來的Android編程和我有而傳遞Parcelable的一個ArrayList片段的問題。 這是推出的活動(工作好!)其中feedlist是parcelable 音樂的ArrayList。安卓:傳遞數據(臨時演員)的片段
Intent in = new Intent(context, ListMusicsActivity.class);
in.putExtra("arrayMusic", feedList);
activity.startActivity(in);
片段活動onCreate()方法:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymusiclist);
if(savedInstanceState != null)
{
ListMusicFragment frag = new ListMusicFragment();
frag.setArguments(getIntent().getExtras());
}
}
所述片段代碼:
public class ListMusicFragment extends SherlockFragment{
private ArrayList<Music> listMusics = new ArrayList<Music>();
private ListView listMusic;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
listMusics = (ArrayList<Music>) getArguments().getSerializable("arrayMusic");
View view = inflater.inflate(R.layout.musiclistview, container, false);
listMusic = (ListView) view.findViewById(R.id.musicListView);
listMusic.setAdapter(new MusicBaseAdapter(getActivity(), listMusics));
return view;
}
}
我認爲這個問題是在該行
listMusics = (ArrayList<Music>) getArguments().getSerializable("arrayMusic");
最後這是我的音樂課:
public class Music implements Parcelable{
private String url;
private String artist;
private String title;
private String duration;
private String info_url;
private String lyrics;
public Music(String url, String artist, String title,
String duration, String lyrics, String info_url)
{
this.url = url;
this.artist = artist;
this.title = title;
this.duration = duration;
this.lyrics = lyrics;
this.info_url = info_url;
}
public Music(Parcel in)
{
url = ParcelUtils.readString(in);
artist = ParcelUtils.readString(in);
title = ParcelUtils.readString(in);
duration = ParcelUtils.readString(in);
info_url = ParcelUtils.readString(in);
lyrics = ParcelUtils.readString(in);
}
public String getUrl()
{
return url;
}
public String getArtist()
{
return artist;
}
public String getTitle()
{
return title;
}
public String getDuration()
{
return duration;
}
public String getLyrics()
{
return lyrics;
}
public String getInfo()
{
return info_url;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
ParcelUtils.writeString(dest, url);
ParcelUtils.writeString(dest, artist);
ParcelUtils.writeString(dest, title);
ParcelUtils.writeString(dest, duration);
ParcelUtils.writeString(dest, lyrics);
ParcelUtils.writeString(dest, info_url);
}
public static final Parcelable.Creator<Music> CREATOR =
new Parcelable.Creator<Music>() {
public Music createFromParcel(Parcel in)
{
return new Music(in);
}
public Music[] newArray(int size)
{
return new Music[size];
}
};
}
當我運行這段代碼我得到的問題是在片段onCreateView()方法一個顯示java.lang.NullPointerException。 如果有人指出我在正確的方向看到我失敗的地方,我將非常感激。
編輯:問題解決了:我只是需要加入這一行的片段活動onCreate()方法(othwewise的getArguments()將返回null):
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, frag).commit();
並添加此的片段代碼:
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
if(bundle != null)
{
listMusics = bundle.getParcelableArrayList("arrayMusic");
listMusic.setAdapter(new MusicBaseAdapter(getActivity(), listMusics));
}
}
其中,listMusics是Parcelable
音樂的ArrayList
。
第一步是打開LogCat窗口,查看異常並查看導致NullPointerException的原因。如果你無法弄清楚,那麼把異常中的堆棧跟蹤放入你的問題中,這樣可以有人提供幫助。 – Brian 2013-03-13 17:30:07
BrianV:感謝您的提示!這是我執行程序時得到的LogCat! http://pastebin.com/ycVcXLFf – pluralism 2013-03-13 17:35:01
看起來像你在你的視圖佈局文件中有一個錯誤:**引起:android.view.InflateException:二進制XML文件行#10:錯誤膨脹類片段** – Brian 2013-03-13 17:36:55