我正在创建一个应用程序,其中我有三个选项卡和相应的片段。但是现在我想添加一个选项卡但是当我添加它时没有问题但是当我运行我的应用程序时它会抛出nullpointerexception。我不知道为什么会这样。
请告诉我哪里错了。
这是我的活动,其中包含所有标签:
public class MainActivity extends Activity {
RelativeLayout rl;
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
ActionBar.Tab TabOrder , TabCart,TabHistory;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getOverflowMenu();
rl = (RelativeLayout) findViewById(R.id.mainLayout);
//fragMentTra = getFragmentManager().beginTransaction();
ActionBar actionbar = getActionBar();
actionbar.setTitle("Select To Order");
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// bar.addTab(bar.newTab().setText("ORDER").setTabListener((TabListener) this));
// bar.addTab(bar.newTab().setText("CART").setTabListener(this));
// bar.addTab(bar.newTab().setText("HISTORY").setTabListener(this));
//TabOrder = actionbar.newTab().setText("DEAL");
TabOrder = actionbar.newTab().setText("ORDER");
TabCart = actionbar.newTab().setText("CART");
TabHistory = actionbar.newTab().setText("HISTORY");
//Fragment FragmentDeal = new FragmentDeal();
Fragment FragmentOrder = new FragmentOrder();
Fragment FragmentCart = new FragmentCart();
Fragment FragmentHistory = new FragmentHistory();
//TabOrder.setTabListener(new MyTablistenerClass(FragmentDeal));
TabOrder.setTabListener(new MyTablistenerClass(FragmentOrder));
TabCart.setTabListener(new MyTablistenerClass(FragmentCart));
TabHistory.setTabListener(new MyTablistenerClass(FragmentHistory));
//actionbar.addTab(TabDeal);
actionbar.addTab(TabOrder);
actionbar.addTab(TabCart);
actionbar.addTab(TabHistory);
}
private 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();
}
}
FragmentOrder
@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;
}
}
});
return view;
}
}
OrderAdapter
private 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,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[] names={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
FragmentHistory
@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;
}
}
FragmentCart
@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();
return layout;
}
}
FragmentDeal
@SuppressLint("ValidFragment")
public class FragmentDeal extends Fragment{
@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 listView = (GridView) layout.findViewById(R.id.GridDeal);
listView.setAdapter(new DealAdapter());
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
return layout;
// TODO Auto-generated method stub
}
}
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
};
private String[] names1={"ab","cd","ef","gh"};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
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
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
FragmentTransaction fragMentTra;
if (tab.getText().equals("DEAL")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
FragmentDeal Fram1 = new FragmentDeal();
//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) {
}
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) {
}
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) {
}
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) {
// TODO Auto-generated method stub
}
}
}
在添加tabdeal时我面临问题.FragmentDeal是它各自的片段。如果有人能看到我错在哪里告诉我。 感谢。
答案 0 :(得分:1)
//TabOrder = actionbar.newTab().setText("DEAL");
TabOrder = actionbar.newTab().setText("ORDER");
您已将两个标签设置为TabOrder
。将第一个设置为TabDeal
。
编辑:
当然,您还需要在此处初始化TabDeal
:
ActionBar.Tab TabOrder , TabCart,TabHistory;
顺便说一句,通常只有类名以大写字母开头。这给我带来了一些困惑。