这是咖啡馆管理的代码,我在Tablayout的一个片段中添加我面临的3个错误
1)行中的错误(TextView textView =(TextView)view.findViewById(R.id.qtea); in on on on on on on on on on on on on 2)只是在显示价格方法中查看错误 3)返回视图
public class TabFragment1 extends Fragment {
int counttea = 0;
int countsamosa = 0;
and so on
int teaprice = 0;
int samosaprice = 0;
int macroniprice = 0;
and so on
int totalprice = 0;
int sum = teaprice + samosaprice + macroniprice + biryaniprice + pulawoprice + rotiprice + parathaprice + chickenbiryaniprice + chickenqormaprice + lobyaprice + namkeenprice + sandwichprice + chanaprice + shawarmaprice;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
//For tea
Button btnTea = (Button) view.findViewById(R.id.btntea);
btnTea.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counttea = counttea + 1;
teaprice = counttea * 20;
TextView textView = (TextView) view.findViewById(R.id.qtea);
textView.setText("" + counttea);
showpricetea(teaprice);
showtotalPrice(sum);
}
});
//For samosa
Button btnsam = (Button) view.findViewById(R.id.btnsamosa);
btnsam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
countsamosa = countsamosa + 1;
samosaprice = countsamosa * 10;
TextView textView = (TextView) view.findViewById(R.id.qsamosa);
textView.setText("" + countsamosa);
showpricesamosa(samosaprice);
showtotalPrice(sum);
}
});
//For macroni
Button btnmacroni = (Button) view.findViewById(R.id.btnmacroni);
btnmacroni.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
countmacroni = countmacroni + 1;
macroniprice = countmacroni * 50;
TextView textView = (TextView) view.findViewById(R.id.qmacroni);
textView.setText("" + countmacroni);
showpricemacroni(macroniprice);
showtotalPrice(sum);
}
});
and so on
}
public void showpricesamosa(int price) {
TextView textView1 = (TextView) view.findViewById(R.id.psamosa);
textView1.setText("" + price);
}
public void showpricetea(int price) {
TextView textView1 = (TextView) view.findViewById(R.id.ptea);
textView1.setText("" + price);
}
public void showpriceroti(int price) {
TextView textView1 = (TextView) view.findViewById(R.id.proti);
textView1.setText("" + price);
}
and so on
public void showtotalPrice(int price) {
TextView textView2 = (TextView) view.findViewById(R.id.showtotalprize);
int total = teaprice + samosaprice + macroniprice + biryaniprice + pulawoprice + rotiprice + parathaprice + chickenbiryaniprice + chickenqormaprice + lobyaprice + namkeenprice + sandwichprice + chanaprice + shawarmaprice;
textView2.setText("" + total);
}
public void clickreset(View view) {
counttea = 0;
countsamosa = 0;
countmacroni = 0;
countbiryani = 0;
and so on
teaprice = 0;
samosaprice = 0;
macroniprice = 0;
and so on
//for tea
TextView tea1 = (TextView) view.findViewById(R.id.qtea);
tea1.setText("00");
TextView tea2 = (TextView) view.findViewById(R.id.ptea);
tea2.setText("00");
//for samosa
TextView sam1 = (TextView) view.findViewById(R.id.qsamosa);
sam1.setText("00");
TextView sam2 = (TextView) view.findViewById(R.id.psamosa);
sam2.setText("00");
//for macroni
TextView mac1 = (TextView) view.findViewById(R.id.qmacroni);
mac1.setText("00");
and so on
}
return view;
}
答案 0 :(得分:2)
不要初始化onClick
内的观看次数。在onCreateView
上初始化视图,并在public void clickreset(View view)
方法上使用它。
将以下textViews
移动为全局变量。
TextView tea1; //global variable
TextView tea2;
TextView sam1;
TextView sam2;
TextView mac1;
在onCreatView()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
tea1 = (TextView) view.findViewById(R.id.qtea);
tea2 = (TextView) view.findViewById(R.id.ptea);
sam1 = (TextView) view.findViewById(R.id.qsamosa);
sam2 = (TextView) view.findViewById(R.id.psamosa);
mac1 = (TextView) view.findViewById(R.id.qmacroni);
}
希望有所帮助:)
答案 1 :(得分:1)
//For macroni
Button btnmacroni = (Button) view.findViewById(R.id.btnmacroni);
btnmacroni.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
countmacroni = countmacroni + 1;
macroniprice = countmacroni * 50;
TextView textView = (TextView) view.findViewById(R.id.qmacroni);
textView.setText("" + countmacroni);
showpricemacroni(macroniprice);
showtotalPrice(sum);
}
});
return view;
} //before the end of onCreateView body, you should return view.
//这里你应该写回程视图