Call Button does not work

0
Hello, after trying all the tutorials and some more I decided that I can not even solve the problem, I want to create a button that redirects to a call to a certain number happens is that the button does nothing, neither error nor warning, ...

This is Java:

    public class APizzaria extends Fragment {

    private Button button;
    View inflatedView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        this.inflatedView = inflater.inflate(R.layout.pizzaria_1, container,false);
        super.onCreate(savedInstanceState);

        button = (Button) inflatedView.findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {

                String number = "23454568678";
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" +number));
                startActivity(intent);
            }
        });

        View android = inflater.inflate(R.layout.pizzaria_1, container, false);

        return android;
    }
}

This is the layout:

        <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/TextView06"
        android:text="@string/Telefone" />

And I've added the permissions in Manifest, can you help me figure out why it's not working? Thanks

    
asked by anonymous 02.08.2014 / 12:39

1 answer

0

I do not understand why this line:
this.inflatedView = inflater.inflate(R.layout.pizzaria_1, container,false); .

Delete it and move the line:
View android = inflater.inflate(R.layout.pizzaria_1, container, false);
to this place.

Change line:
button = (Button) inflatedView.findViewById(R.id.button);
for button = (Button) android.findViewById(R.id.button);

I think the problem is that onClick is assigned to the View button of the inflatedView variable but what is returned by the method is the android variable .

The code should look like this:

public class APizzaria extends Fragment {

    private Button button;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View android = inflater.inflate(R.layout.pizzaria_1, container, false);

        button = (Button) android.findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {

                String number = "23454568678";
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" +number));
                startActivity(intent);
            }
        });

        return android;
    }
}
    
02.08.2014 / 14:46