-1

After I c/p code to fragment, my function won't set the string inside the app. But it worked at first without fragment, and I can't find the problem. App runs normally, but it won't output setText string on screen, while string is visible with it default string

public class MainFragment extends FragmentActivity{

    EditText editNumber;
    Button calculate;
    TextView result;

    int two = 2;
    int seven = 7;
    int inputNumber;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //find elements
        editNumber = (EditText) findViewById(R.id.editNumber);
        calculate = (Button) findViewById(R.id.calculateButton);
        result = (TextView) findViewById(R.id.resultText);

        //listener
        editNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    inputNumber = Integer.parseInt(editNumber.getText().toString());

              }
        });//onClick

        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText("Output: " + (inputNumber / two) + seven);
            }
        });//onclick
    }//onCreate
}//main

[code]

robigroza
  • 559
  • 1
  • 5
  • 22

2 Answers2

0

In FragmentActivity you should define a Fragment object, insert it into the Activity's frame and display things in it. Please refer to:

http://developer.android.com/guide/components/fragments.html

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
0

Are you sure your MainFragment is a fragment? It extends activity: FragmentActivity. If it is just a typo issue then you have to provide your layout in the onCreateView() method of the fragment. Something like this:

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.activity_main, container, false);
    result = (TextView) v.findViewById(R.id.resultText);
    return v;
}

Finding a view in a fragment in other method than onCreateView() you can call:

getView().findViewById(R.id.resultText);
Blehi
  • 1,990
  • 1
  • 18
  • 20