Call Button does not work

Asked

Viewed 184 times

0

Hello, after trying all the tutorials and some more I decided I can’t 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, no error, no warning,...

This is the 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 added the permissions to Manifest, can you help me figure out why it’s not working? Thank you

1 answer

0


I don’t 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.

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

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

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;
    }
}
  • Thank you very much, solved my problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.