Potřeboval jsem přenést do fragmentu object. Použil jsem k tomu bundle.putSerializable(). přenášení dat do fragmentu fungovalo perfektně, ale když jsem potom spustil jinou Activitu nastal error:
Eclipse mi ukáže, že je problém v na tomto řádku: Bundle--> parcel.restoreAllowFds(oldAllowFds);
výpis z debugeru
test [Android Application]
DalvikVM [localhost:8603]
Thread [<1> main] (Suspended (exception RuntimeException))
<VM does not provide monitor information>
Bundle.writeToParcel(Parcel, int) line: 1702
Parcel.writeBundle(Bundle) line: 636
ActivityManagerProxy.activityStopped(IBinder, Bundle, Bitmap, CharSequence) line: 2467
ActivityThread$StopInfo.run() line: 3098
Handler.handleCallback(Message) line: 733
ActivityThread$H(Handler).dispatchMessage(Message) line: 95
Looper.loop() line: 136
ActivityThread.main(String[]) line: 5017
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 515
ZygoteInit$MethodAndArgsCaller.run() line: 779
ZygoteInit.main(String[]) line: 595
NativeStart.main(String[]) line: not available [native method]
Thread [<10> Binder_2] (Running)
Thread [<9> Binder_1] (Running)
Zdrojový kód:
mainActivity
public class MainActivity extends Activity implements Serializable {
private static final long serialVersionUID = 1L;
private List<String> arrayOfGuest; //hold thing which are in listView
private ListView NameList;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private ArrayAdapter<String> arrayAdapter;
private Database database;
/*****************************************************
* OnCreate method
*****************************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this.deleteDatabase("Database.db");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Init();
//Set default fragment.
fragmentTransaction = fragmentManager.beginTransaction(); //Start of transaction.
Fragment fragment=new DefaultFragment();
fragmentTransaction.add(R.id.content_layout, fragment); // add fragment to layout.
fragmentTransaction.commit(); //end of transaction.
//Listener for NameList (ListView). The listener change fragments on screen.
NameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
fragmentTransaction = fragmentManager.beginTransaction(); //Start of transaction.
Fragment fragment=GuestFragment.newInstance(database, NameList.getItemAtPosition(position).toString(), MainActivity.this);
fragmentTransaction.replace(R.id.content_layout, fragment);
fragmentTransaction.addToBackStack(null); //add fragment tag (id) for latest editing.
fragmentTransaction.commit(); //end of transaction.
}
});
//create long listener for deleting guest
NameList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {
//create dialog for deleting guest
AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(MainActivity.this);
dialogBuilder.setTitle("Potvdit smazání?");
dialogBuilder.setPositiveButton("Smazat", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
database.DeleteGuest(NameList.getItemAtPosition(position).toString(), 0);
updateListView(); //update array with name of guest
setDefaultFragment();
}
});
dialogBuilder.setNegativeButton("Zrušit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Host nebyl smazán.", Toast.LENGTH_LONG).show();
}
});
dialogBuilder.show();
return false;
}
});
}
@Override
protected void onPause() {
super.onPause();
database.close(); //close database because another activity will be using it
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.actin_bar_options_list_of_food:
//start activity with offer
Intent intent = new Intent(this, OfferActivity.class);
startActivity(intent);
break;
case R.id.actin_bar_options_app:
break;
}
return true;
}
/*****************************************************
* Method init all staff
*****************************************************/
private void Init(){
database=new Database(this);
fragmentManager = getFragmentManager();
NameList=(ListView) findViewById(R.id.list_view);
arrayOfGuest = new ArrayList<String>();
updateListView(); //update ListView
}
/*****************************************************
* Method for add new guest, it will show a dialog with writing name
*****************************************************/
public void AddGuest(View view){
AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(MainActivity.this); //create dialog builder
final EditText ET=new EditText(this);
dialogBuilder.setTitle("Jméno hosta?"); // set title
dialogBuilder.setView(ET); //add EditText
//Positive button
dialogBuilder.setPositiveButton("Přidat", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
database.AddGuest(ET.getText().toString()); // add guest to database
updateListView(); //update array with name of guest
}
});
//negative button
dialogBuilder.setNegativeButton("Zrušit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Host Nepřidán", Toast.LENGTH_LONG).show();
}
});
dialogBuilder.show(); //show the dialog
}
/*****************************************************
* Method add ActionBar
*****************************************************/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mif=getMenuInflater();
mif.inflate(R.menu.main_x, menu);
return super.onCreateOptionsMenu(menu);
}
/*****************************************************
* Method, which take guest from database and paste it to array
*****************************************************/
public void updateListView(){
arrayOfGuest.clear();
ArrayList<HashMap<String, String>> Guests=database.GetAllGuests();
for(HashMap<String, String> x:Guests)
arrayOfGuest.add(x.get(database.GUEST_NAME));
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayOfGuest );
NameList.setAdapter(arrayAdapter);
}
public void setDefaultFragment(){
//Set default fragment.
fragmentTransaction = fragmentManager.beginTransaction(); //Start of transaction.
Fragment defaultFragment=new DefaultFragment();
fragmentTransaction.replace(R.id.content_layout, defaultFragment); // add fragment to layout.
defaultFragment=null;
fragmentTransaction.commit(); //end of transaction.
}
}
GuestFragment
package com.wesikprograming.cisnik;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.wesikprograming.cisnik.database.Database;
import com.wesikprograming.cisnik.listViewAdapters.OrderListViewAdapter;
public class GuestFragment extends Fragment{
private TextView nameTextView;
private ListView orderListView;
private String guestName;
private Button addButton, payButton;
private Database database;
private MainActivity mainActivity;
private boolean isActived=false;
private static final String DATABASE_KEY = "describable_key";
private static final String GUEST_NAME_KEY = "guest_name_key";
private static final String MEIN_ACTIVITY_KEY = "main_activity";
/*****************************************************
* create fragment and put inside bundle variable. Reason why I am doing it here with static method, is that I have here final recognize String name like DATABASE_KEY
***************************************************/
public static GuestFragment newInstance(Database database, String guestName, MainActivity mainActivity) {
GuestFragment fragment = new GuestFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(DATABASE_KEY, database);
bundle.putString(GUEST_NAME_KEY, guestName);
bundle.putSerializable(MEIN_ACTIVITY_KEY, mainActivity);
fragment.setArguments(bundle);
bundle=null;
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.guest_fragment, container, false); //create fragment from XML file
//put data to variable from bundle
database=(Database) getArguments().getSerializable(DATABASE_KEY);
mainActivity=(MainActivity) getArguments().getSerializable(MEIN_ACTIVITY_KEY);
guestName= getArguments().getString(GUEST_NAME_KEY);
nameTextView=(TextView) view.findViewById(R.id.guest_name_id);
orderListView=(ListView) view.findViewById(R.id.guest_list_view);
addButton=(Button)view.findViewById(R.id.guest_fragment_add_button);
payButton=(Button)view.findViewById(R.id.guest_fragment_pay_button);
nameTextView.setText(guestName);
UpdateListView(); //set adapter for listView
//add item to order dialog
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(getActivity());
dialogBuilder.setTitle("přidat položku");
//create adapter for listView in dialog
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1); //create adapter for dialog
adapter.addAll(database.getAllOffersItemsName()); //add items to adapter
dialogBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() { //set adapter in dialog a make listener
public void onClick(DialogInterface dialog, int position) {
if(!database.AddItemToOrders(database.GetGuestID(guestName), database.GetItemID(adapter.getItem(position).toString())))
Toast.makeText(getActivity(), "Položka se již v seznamu vyskytuje. Ale byla zařazena do objednávek", Toast.LENGTH_LONG).show();
else
Toast.makeText(getActivity(), "Položka byla přidána do seznamu", Toast.LENGTH_LONG).show();
UpdateListView();
}
});
dialogBuilder.setPositiveButton("Zrušit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialogBuilder.setNegativeButton("Položka není v seznamu", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialogBuilder.show();
}
});
//charge order
payButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG).show();
}
});
return view;
}
/*****************************************************
* set things list view in fragment
***************************************************/
public void UpdateListView(){
List<OrderItem> list=new ArrayList<OrderItem>(); // this hold information about orders
ArrayList<HashMap<String, Integer>> Guests=database.GetGuestOffer(database.GetGuestID(guestName)); //get guest order
//create items for listView. Take them form database
for(HashMap<String, Integer> x: Guests){
list.add(new OrderItem(database.GetItemNameByID(x.get(database.ITEM_ID)), x.get(database.ITEM_COUNT)));
}
//create custom adapter for listView
OrderListViewAdapter GLVA=new OrderListViewAdapter(getActivity(), list, database, guestName, this);
orderListView.setAdapter(GLVA);
}
@Override
public void onStart() {
super.onStart();
UpdateListView();
}
}
OfferActivity
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.wesikprograming.cisnik.database.Database;
import com.wesikprograming.cisnik.listViewAdapters.OfferListViewAdapter;
public class OfferActivity extends Activity {
private ListView OfferListView;
private Database database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_activity);
init();
UpdataListView();
}
public void init(){
OfferListView = (ListView) findViewById(R.id.offer_listview);
database = new Database(getApplicationContext()); //get database instance
}
public void UpdataListView() {
List<OfferItem> list = new ArrayList<OfferItem>(); //list for items for listView
ArrayList<HashMap<String, String>> OfferArrayList = database.GetOffer(); //get guest offer form databse
//fill list with items from database
for (HashMap<String, String> x : OfferArrayList) {
list.add(new OfferItem(x.get(database.ITEM_NAME), x.get(database.ITEM_PRICE)));
}
//set custom adapter to listView
OfferListViewAdapter OFLA=new OfferListViewAdapter(this, list);
OfferListView.setAdapter(OFLA);
}
public void addItem(View view){
final View dialogView = this.getLayoutInflater().inflate(R.layout.offer_add_dialog, null);
AlertDialog.Builder dialogBuilder= new AlertDialog.Builder(this);
dialogBuilder.setTitle("Přidat položku do nabídky");
dialogBuilder.setView(dialogView);
dialogBuilder.setPositiveButton("Přidat", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText name=(EditText) dialogView.findViewById(R.id.offer_dialog_name);
EditText price=(EditText) dialogView.findViewById(R.id.offer_dialog_price);
if(name.getText().toString().trim().length() > 0 && price.getText().toString().trim().length() > 0){
database.addItemToOffer(name.getText().toString(), Integer.parseInt(price.getText().toString()), 0);
UpdataListView();
Toast.makeText(getApplicationContext(), "Položka přidána", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(), "Položka nebyla přidána!", Toast.LENGTH_LONG).show();
}
});
dialogBuilder.setNegativeButton("Zrušit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Položka nebyla přidána!", Toast.LENGTH_LONG).show();
}
});
dialogBuilder.show();
}
}