我在一個基於用戶輸入的片段「最近」中顯示了一個列表視圖。現在,我想將該列表視圖作爲對象數組傳遞給容器活動(主活動)(我使用接口傳遞數據),然後當用戶單擊MainActivity中的按鈕時,該數組列表將傳遞給另一個活動顯示爲ListView。當我運行應用程序時,它顯示錯誤「應用程序已停止」。在logcat中也沒有顯示錯誤。請幫助。無法在新的活動中傳遞對象數組
1. My relevant code of interface in Fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false); mListView=(ListView)view.findViewById(R.id.myListView);
placeList.add(new Place("Shoppers Stop (1st Floor)", "Get 10% Off on Clothes & Jewellery", "shop1"));
placeList.add(new Place("Dominos (2nd Floor)", "Get 10% Off on Chef Special Pizzas", "shop2"));
placeList.add(new Place("More MegaStore (3rd Floor)", "Wednesday Bazaar, Get min 10% Off","shop4"));
mPlaces=placeList.toArray(new Place[placeList.size()]);//Convert arraylist to array
passData(mPlaces);//Pass Array of objects for interface
mPlaceAdapter = new PlaceAdapter(getActivity(), R.layout.row, mPlaces);
if (mListView != null) {
mListView.setAdapter(mPlaceAdapter);\\Creating listview in fragment
}
// Inflate the layout for this fragment
return view;
}
//Inetrface definition
public interface OnDataPass {
public void onDataPass(Place[] data);
}
OnDataPass dataPasser;
@Override
public void onAttach(Context context) {
super.onAttach(context);
dataPasser = (OnDataPass) context;
}
@Override
public void onDetach() {
super.onDetach();
dataPasser=null;
}
public void passData(Place[] data) {
dataPasser.onDataPass(data);
}
}
2. Main Activity (Container of fragement):
@Override
public void onDataPass(Place []data) {
Collections.addAll(allOffers,data);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.all:
Intent intent = new Intent(MainActivity.this, AllOffersActivity.class);
intent.putExtra("AllOffers", (ArrayList<Place>)allOffers);
getApplication().startActivity(intent);
return true;
default: return super.onOptionsItemSelected(item);
}
}
3. AllOffersActivity Class (where again I want to show listviews):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alloffers);
onePlaces=(ArrayList<Place>)getIntent().getSerializableExtra("AllOffers");
if(onePlaces!=null) {
mPlaces = onePlaces.toArray(new Place[onePlaces.size()]);//To store data into an array
}
allPlaceAdapter = new PlaceAdapter(AllOffersActivity.this, R.layout.row, mPlaces);
allListView=(ListView)findViewById(R.id.allListView);
if (allListView != null) {
allListView.setAdapter(allPlaceAdapter);
}
}
}
4. AndroidManifest.xml:
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".activity.AllOffersActivity"/>
- 圖像-i)的圖像,以顯示在片段列表視圖,並且當用戶點擊在底部「明星」圖標,新的活動應該顯示列表視圖這是越來越在片段 所示
你如何將場地從一個活動傳遞給另一個活動?你可以追加這個行動的源代碼嗎? – user1185087