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

标签 java android android-fragments

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

1) onclicklistener 中 (TextView textView = (TextView) view.findViewById(R.id.qtea); 行出错 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/

相关文章:

android - 打开和关闭 android 时抽屉导航滞后

java - onActivityResult 未从修复的警报对话框中触发

java - 如何...使用 MigLayout

android - 从导出的统一项目 Activity 中检测 Android Studio 中的触摸事件

java - 应用程序被终止时服务正在重新启动

Android 错误 - FragmentTransaction 类型中的方法 replace(int, Fragment) 不适用于参数 (int, ListFragment)

java - 无法启动 Activity ComponentInfo{MainFrag} : java. lang.NullPointerException

java - 如何用spring正确发布DDD领域事件?

java - Android 可以接听空中来电吗?

java - 使用 Feign 将 POST 数据作为请求参数包含在内