私のアプリケーションにはタブがあり、それに関連するフラグメントがあります。私のアプリはポートレイト モードで正常に動作していますが、向きをランドスケープ モードに変更するとアプリがクラッシュします。
アプリが両方の向きで正常に動作するようにするにはどうすればよいですか。
これが私の主な活動です:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity {
public MainActivity() {
super();
// TODO Auto-generated constructor stub
}
RelativeLayout rl;
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
ActionBar.Tab TabOrder , TabCart,TabHistory,TabDeals;
private String settingfirstname;
private List<SettingDetail> settinglistdetail;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
getOverflowMenu();
rl = (RelativeLayout) findViewById(R.id.mainLayout);
ActionBar actionbar = getActionBar();
actionbar.setTitle("Select To Order");
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
TabDeals = actionbar.newTab().setText("DEALS");
TabOrder = actionbar.newTab().setText("ORDER");
TabCart = actionbar.newTab().setText("CART");
TabHistory = actionbar.newTab().setText("HISTORY");
Fragment FragmentDeals = new FragmentDeals();
Fragment FragmentOrder = new FragmentOrder();
Fragment FragmentCart = new FragmentCart();
Fragment FragmentHistory = new FragmentHistory();
TabDeals.setTabListener(new MyTablistenerClass(FragmentDeals));
TabOrder.setTabListener(new MyTablistenerClass(FragmentOrder));
TabCart.setTabListener(new MyTablistenerClass(FragmentCart));
TabHistory.setTabListener(new MyTablistenerClass(FragmentHistory));
actionbar.addTab(TabDeals);
actionbar.addTab(TabOrder);
actionbar.addTab(TabCart);
actionbar.addTab(TabHistory);
// //for pop up window
// AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
// MainActivity.this);
//
// // set title
// //alertDialogBuilder.setTitle("Your Title");
//
// // set dialog message
// alertDialogBuilder
// .setMessage("Do You want to switch to Scan using QR code?")
// .setCancelable(false)
// .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog,int id) {
// // if this button is clicked, close
// // current activity
// MainActivity.this.finish();
// }
// })
// .setNegativeButton("No",new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog,int id) {
// // if this button is clicked, just close
// // the dialog box and do nothing
// dialog.cancel();
// }
// });
//
// // create alert dialog
// AlertDialog alertDialog = alertDialogBuilder.create();
//
// // show it
// alertDialog.show();
// FragmentOrder Fram1;
// FragmentHistory Fram2;
DatabaseManager db = new DatabaseManager(MainActivity.this);
settinglistdetail = db.getAllSettingsDetailfirstname();
for (SettingDetail d : settinglistdetail) {
settingfirstname = d.getFirstname();
}
Toast.makeText(MainActivity.this, "settingfirstname : " + settingfirstname, Toast.LENGTH_LONG).show();
//DatabaseManager db = new DatabaseManager(MainActivity.this);
ProductDetail p = new ProductDetail();
p.setProductid(001);
p.set_name("abc");
p.setImage(null);
p.setPrice(123);
p.setQuantity(2);
//db.insertProductDetail(p);
ProductDetail p1 = new ProductDetail();
p1.setProductid(002);
p1.set_name("xyz");
p1.setImage(null);
p1.setPrice(345);
p1.setQuantity(2);
//db.insertProductDetail(p1);
ProductDetail p2 = new ProductDetail();
p2.setProductid(003);
p2.set_name("ghi");
p2.setImage(null);
p2.setPrice(678);
p2.setQuantity(2);
//db.insertProductDetail(p2);
ProductDetail p3 = new ProductDetail();
p3.setProductid(004);
p3.set_name("mno");
p3.setImage(null);
p3.setPrice(789);
p3.setQuantity(2);
//db.insertProductDetail(p3);
ScanId i = new ScanId();
i.setScanid(1);
//db.insertScanId(i);
SettingDetail s1 = new SettingDetail();
s1.setFirstname("sweety");
s1.setLastname("yede");
s1.setPhonenumber(957983914);
s1.setEmailaddress("sweety.yede@gmail.com");
s1.setAddressline1("flatno.1");
s1.setAddressline2("wadgaonsheri");
s1.setLocality("WadgaonSheri");
s1.setCity("Pune");
s1.setPincode(411056);
//db.insertSettingsDetail(s1);
SettingDetail s2 = new SettingDetail();
s2.setFirstname("priti");
s2.setLastname("yede");
s2.setPhonenumber(957983914);
s2.setEmailaddress("sweety.yede@gmail.com");
s2.setAddressline1("flatno.1");
s2.setAddressline2("wadgaonsheri");
s2.setLocality("WadgaonSheri");
s2.setCity("Pune");
s2.setPincode(411056);
//db.insertSettingsDetail(s2);
SettingDetail s3 = new SettingDetail();
s3.setFirstname("nikhil");
s3.setLastname("waghaye");
s3.setPhonenumber(957983914);
s3.setEmailaddress("sweety.yede@gmail.com");
s3.setAddressline1("flatno.1");
s3.setAddressline2("wadgaonsheri");
s3.setLocality("WadgaonSheri");
s3.setCity("Pune");
s3.setPincode(411056);
//db.insertSettingsDetail(s3);
init();
super.onCreate(savedInstanceState);
}
public void init() {
// TODO Auto-generated method stub
}
public void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressLint({ "ValidFragment", "NewApi" })
public class FragmentOrder extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//View view = inflater.inflate(R.layout.g, null);
View view = inflater.inflate(R.layout.gridview,null);
final GridView listView = (GridView) view.findViewById(R.id.mainGrid);
listView.setAdapter(new OrderAdapter());
//listView.setSelection(setselected,true);
// listView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
//
// listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
//
//
// @Override
// public boolean onActionItemClicked(ActionMode mode,
// MenuItem item) {
// // TODO Auto-generated method stub
// return true;
// }
//
// @Override
// public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// mode.setTitle("Select Items");
// mode.setSubtitle("One item selected");
// return true;
//
// }
//
// @Override
// public void onDestroyActionMode(ActionMode mode) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// // TODO Auto-generated method stub
// return false;
// }
//
// @Override
// public void onItemCheckedStateChanged(ActionMode mode,
// int position, long id, boolean checked) {
//
// //listView.setLongClickable(false);
// int selectCount = listView.getCheckedItemCount();
// switch (selectCount) {
// case 1:
// mode.setSubtitle("One item selected");
// break;
// default:
// mode.setSubtitle("" + selectCount +"items selected");
// break;
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
return view;
}
}
public class OrderAdapter extends BaseAdapter {
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//CheckableLayout l;
View myView = convertView;
LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.grid_items_ontap, null);
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image_OnTap);
iv.setImageResource(mThumbIds[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.grid_item_text_onTap);
tv.setText(names[position] );
return myView;
}
}
private Integer[] mThumbIds = {
R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car
};
private String[] names={"Vegetable","Fruits","Groceries","ij","kl"};
@SuppressLint("ValidFragment")
public class FragmentHistory extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_fragmenthistory,
(ViewGroup)
findViewById(R.id.layout_root_history));
ListView lv = (ListView) layout.findViewById(R.id.listViewHistory);
lv.setAdapter(new HistoryListViewAdapter(MainActivity.this));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("ORDERID");
dlg.setPositiveButton("REORDER",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton("EDIT AND ORDER", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
ListView listView = new ListView(MainActivity.this);
listView.setAdapter(new DialogListAdapter(MainActivity.this));
dlg.setView(listView);
//((Dialog) dlg).setCanceledOnTouchOutside(true);
// show it
dlg.show();
}
});
return layout;
}
}
@SuppressLint("ValidFragment")
public class FragmentCart extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_fragmentcart,
(ViewGroup)
findViewById(R.id.layout_root_cart));
ListView lv = (ListView) layout.findViewById(R.id.listViewCart);
lv.setAdapter(new CartListViewAdapter(MainActivity.this));
//lv.invalidateViews();
// ActionBar actionBar = getActionBar();
// setHasOptionsMenu(true);
// actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
return layout;
}
}
@SuppressLint("ValidFragment")
public class FragmentDeals extends Fragment implements Checkable{
private boolean mChecked;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragmentdeal,
(ViewGroup)
findViewById(R.id.layoutdeal));
//View view = inflater.inflate(R.layout.griddeal,null);
final GridView mGrid = (GridView) layout.findViewById(R.id.GridDeal);
mGrid.setAdapter(new DealAdapter());
mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onActionItemClicked(ActionMode mode,
MenuItem item) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Select Items");
mode.setSubtitle("One item selected");
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
//listView.setLongClickable(false);
int selectCount = mGrid.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount +"items selected");
break;
}
}
});
return layout;
// TODO Auto-generated method stub
}
@Override
public boolean isChecked() {
// TODO Auto-generated method stub
return mChecked;
}
@SuppressWarnings("deprecation")
@Override
public void setChecked(boolean checked) {
// TODO Auto-generated method stub
mChecked = checked;
layout.setBackgroundDrawable(checked ? getResources().getDrawable(
R.drawable.bground) : null);
}
@Override
public void toggle() {
// TODO Auto-generated method stub
setChecked(!mChecked);
}
}
private class DealAdapter extends BaseAdapter {
@Override
public int getCount() {
return mThumbIds1.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//CheckableLayout l;
View myView = convertView;
LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.griddealitems, null);
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.grid_deal_image);
iv.setImageResource(mThumbIds1[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.grid_deal_text);
tv.setText(names1[position] );
return myView;
}
private Integer[] mThumbIds1= {
R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car
};
private String[] names1={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
// if (menu.getItemId()==FragmentCart) {
// //Log.e(TAG, " - onCreateOPTIONMenu");
// inflater.inflate(R.menu.fragmentcart, menu);
// MenuItem cancel = menu.findItem(R.id.cancel_booking_accepted);
// //cancel.getIcon().setAlpha(Constants.ENABLED_OPACITY);
// //menu.findItem(R.id.my_trips_switch).getIcon().setAlpha(Constants.ENABLED_OPACITY);
// }
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.action_settings:
Intent intentForSettings = new Intent(MainActivity.this, SettingsMenu.class);
startActivity(intentForSettings);
return true;
case R.id.action_info:
Intent intentForInformation = new Intent(MainActivity.this,InformationMenu.class);
startActivity(intentForInformation);
return true;
case R.id.action_ContactUs:
Intent dial = new Intent();
String no = "9579839314";
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"+ no));
startActivity(dial);
return true;
}
return false;
}
public class MyTablistenerClass implements android.app.ActionBar.TabListener {
Fragment fragment1;
@SuppressLint("NewApi")
public MyTablistenerClass(Fragment fragment){
this.fragment1 = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@SuppressLint("NewApi")
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
FragmentTransaction fragMentTra;
if (tab.getText().equals("DEALS")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("Deals");
FragmentDeals Fram1 = new FragmentDeals();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), Fram1);
fragMentTra.commit();
}
else if (tab.getText().equals("ORDER")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("Select Category");
//actionBar.setIcon(R.id.search);
FragmentOrder Fram2 = new FragmentOrder();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), Fram2);
fragMentTra.commit();
}
else if(tab.getText().equals("CART")){
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("Shopping Cart");
FragmentCart fram3 = new FragmentCart();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), fram3);
fragMentTra.commit();
}
else if(tab.getText().equals("HISTORY")){
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("History");
FragmentHistory fram4 = new FragmentHistory();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), fram4);
fragMentTra.commit();
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
}
これはエラーです。
11-12 14:06:06.700: E/AndroidRuntime(27733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.scantoorder1/com.example.scantoorder1.MainActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.scantoorder1.MainActivity$FragmentDeals: make sure class name exists, is public, and has an empty constructor that is public
11-12 14:06:06.700: E/AndroidRuntime(27733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
11-12 14:06:06.700: E/AndroidRuntime(27733): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
ありがとう。