-2

It is continuity to the question, android:onClick = "functionName" did not recognize the function name in Fragment subclass. As I exceeded word limit on that, I am asking a new question. I changed my code suggested on forum by different people. Please see the code below

When I used setOnClickListener on button inside the OnCreateView, the app crashing immediately after opening. When I used android:onClick in xml file (Scenario before changing the code) this didn't recognize the method at all.

// my java code for fragment subclass

package com.example.siv.mahalaxmipetroleums;


        import android.os.Bundle;
        import android.support.v4.app.Fragment;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;

public class DipToVol extends Fragment {

    Button clearButton11,calcButton11;

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_one, container, false);



        clearButton11 = (Button) getView().findViewById(R.id.clearBtn1);
        calcButton11 = (Button) getView().findViewById(R.id.calcBtn1);

        calcButton11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int dipHSD = 0, dipMS = 0;
                final short radHSD = 1219;
                final short lenHSD = 6810;
                final short radMS = 999;
                final short lenMS = 6804;
                double volHSD, volMS,volHSD1, volMS1;

                EditText dipH = (EditText) view.findViewById(R.id.dipHSD1);
                EditText dipM = (EditText) view.findViewById(R.id.dipMS1);

                TextView volH = (TextView) view.findViewById(R.id.volHSD1);
                TextView volM = (TextView) view.findViewById(R.id.volMS1);
                try {
                    dipHSD = Integer.parseInt(dipH.getText().toString());
                    volHSD = calVol(radHSD, lenHSD, dipHSD);
                    volHSD1 = Math.round(volHSD);
                    volH.setText(volHSD1 + "");
                }
                catch (NumberFormatException e) {
                    volH.setText("");
                }
                try{ dipMS = Integer.parseInt(dipM.getText().toString());
                    volMS = calVol(radMS, lenMS, dipMS);
                    volMS1 = Math.round(volMS);
                    volM.setText(volMS1 + "");
                }catch(NumberFormatException e){volM.setText("");}

            }
        });

        clearButton11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText dipH = (EditText) view.findViewById(R.id.dipHSD1);
                EditText dipM = (EditText) view.findViewById(R.id.dipMS1);

                TextView volH = (TextView) view.findViewById(R.id.volHSD1);
                TextView volM = (TextView) view.findViewById(R.id.volMS1);

                volH.setText("");
                volM.setText("");
                dipH.setText("");
                dipM.setText("");
            }
        });



            return view;
    }


    public double calVol(short rad, short len, int height) {
        double area;
        area = (Math.PI * Math.pow(rad, 2) / 2) - Math.pow(rad, 2) * Math.asin(1 - (height / (double)rad)) - (rad - height) * Math.sqrt(height * (2 * rad - height));
        return area*len*0.000001;

    }

}
Community
  • 1
  • 1
Siva Prasad
  • 115
  • 1
  • 13

4 Answers4

3

Your getView() is not ready yet. The view is not attached to the activity because it was not returned.

Use view.findViewById instead

Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
0
public class DipToVol extends Fragment {

    Button clearButton11,calcButton11;

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);

        clearButton11 = (Button) view.findViewById(R.id.clearBtn1);
        calcButton11 = (Button) view.findViewById(R.id.calcBtn1);

        calcButton11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int dipHSD = 0, dipMS = 0;
                final short radHSD = 1219;
                final short lenHSD = 6810;
                final short radMS = 999;
                final short lenMS = 6804;
                double volHSD, volMS,volHSD1, volMS1;

                EditText dipH = (EditText) view.findViewById(R.id.dipHSD1);
                EditText dipM = (EditText) view.findViewById(R.id.dipMS1);

                TextView volH = (TextView) view.findViewById(R.id.volHSD1);
                TextView volM = (TextView) view.findViewById(R.id.volMS1);
                try {
                    dipHSD = Integer.parseInt(dipH.getText().toString());
                    volHSD = calVol(radHSD, lenHSD, dipHSD);
                    volHSD1 = Math.round(volHSD);
                    volH.setText(volHSD1 + "");
                }
                catch (NumberFormatException e) {
                    volH.setText("");
                }
                try{ dipMS = Integer.parseInt(dipM.getText().toString());
                    volMS = calVol(radMS, lenMS, dipMS);
                    volMS1 = Math.round(volMS);
                    volM.setText(volMS1 + "");
                }catch(NumberFormatException e){volM.setText("");}

            }
        });

        clearButton11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText dipH = (EditText) view.findViewById(R.id.dipHSD1);
                EditText dipM = (EditText) view.findViewById(R.id.dipMS1);

                TextView volH = (TextView) view.findViewById(R.id.volHSD1);
                TextView volM = (TextView) view.findViewById(R.id.volMS1);

                volH.setText("");
                volM.setText("");
                dipH.setText("");
                dipM.setText("");
            }
        });



            return view;
    }


    public double calVol(short rad, short len, int height) {
        double area;
        area = (Math.PI * Math.pow(rad, 2) / 2) - Math.pow(rad, 2) * Math.asin(1 - (height / (double)rad)) - (rad - height) * Math.sqrt(height * (2 * rad - height));
        return area*len*0.000001;

    }

}
Bhavin Kevadiya
  • 224
  • 3
  • 16
0

Try not using getView in fragment. Instead use view variable or getActivity:

clearButton11 = (Button) view.findViewById(R.id.clearBtn1);
calcButton11 = (Button) view.findViewById(R.id.calcBtn1);

Or use getActivity:

clearButton11 = (Button) getActivity().findViewById(R.id.clearBtn1);
calcButton11 = (Button) getActivity().findViewById(R.id.calcBtn1);
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
0

Thank You All. Specially Pedro Oliveira,W4R10CK and Bhavin Kevadiya. It worked

public class Fragment_One extends Fragment {

    private static EditText dipH,dipM;
    private static TextView volH,volM;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_one, container, false);


             dipH = (EditText) view.findViewById(R.id.dipHSD1);
             dipM = (EditText) view.findViewById(R.id.dipMS1);

             volH = (TextView) view.findViewById(R.id.volHSD1);
             volM = (TextView) view.findViewById(R.id.volMS1);

            final Button calcBtn11  =
                    (Button) view.findViewById(R.id.calcBtn1);

            calcBtn11.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                   clickFun1();
                }
            });
            final Button clearBtn11  =
                    (Button) view.findViewById(R.id.clearBtn1);
            clearBtn11.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    clearFun1();
                }
            });

            return view;

    }

        public void clickFun1 ()
        {
                short dipHSD = 0, dipMS = 0;
                final short radHSD = 1219;
                final short lenHSD = 6810;
                final short radMS = 999;
                final short lenMS = 6804;
                double volHSD, volMS;


                try {
                        dipHSD = Short.parseShort(dipH.getText().toString());
                        Liquid_Volume dtvh = new Liquid_Volume(radHSD,lenHSD,dipHSD);
                        volHSD = dtvh.getVolume();
                        volHSD = Math.round(volHSD);
                        volH.setText(volHSD + "");
                }
                catch (NumberFormatException e) {
                        volH.setText("");
                }
                try{ dipMS = Short.parseShort(dipM.getText().toString());
                        Liquid_Volume dtvm = new Liquid_Volume(radMS,lenMS,dipMS);
                        volMS = dtvm.getVolume();
                        volM.setText(volMS + "");
                }catch(NumberFormatException e){volM.setText("");}

        }


        public void clearFun1(){

                volH.setText("");
                volM.setText("");
                dipH.setText("");
                dipM.setText("");
        }

}
Siva Prasad
  • 115
  • 1
  • 13