gpt4 book ai didi

java - 如何在 Fragment Activity 中设置 Java 代码

转载 作者:搜寻专家 更新时间:2023-11-01 08:25:35 25 4
gpt4 key购买 nike

这是我在 Tablayout 的一个 fragment 中添加的咖啡馆管理代码,我遇到了 3 个错误

1) 在线错误 (TextView textView = (TextView) view.findViewById(R.id.qtea); 在 onclicklistener 2) 仅查看显示价格方法中的错误 3)返回 View

    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;


}

最佳答案

不要在 onClick 中初始化 View 。在 onCreateView 中初始化 View ,并在 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);
}

希望对你有帮助:)

关于java - 如何在 Fragment Activity 中设置 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45622212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com