user3025060
user3025060

Reputation: 15

Fragment activity crashes when launced

My fragment always crashes when opened. I think the problem is caused by the button onClickListener. This fragment should make a call if the device has a phone, and show an alert dialog if it doesn't.

here's the code:

public class HosFrag extends Fragment  {


@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view =  inflater.inflate(R.layout.hosfrag,
            container, false);

    ImageButton bt_callhos = (ImageButton) getView().findViewById(R.id.callhos);
    bt_callhos.setOnClickListener(new View.OnClickListener()
    {

        boolean hasTelephony = getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
        public void onClick(View v)
        {
            if (hasTelephony == true) {

                String url = "tel:25633061";
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                startActivity(intent);
            }
            else {
                new AlertDialog.Builder(getActivity())

                        .setTitle("لا يوجد هاتف")
                        .setIcon(R.drawable.alerticon)
                        .setMessage("الجهاز لا يملك هاتف")
                        .setNeutralButton("عودة",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int which) { }
                                })
                        .show();
            }

        }
    });

    return view;
}






}

Upvotes: 1

Views: 103

Answers (1)

Shamil Sakib
Shamil Sakib

Reputation: 160

Instead of this line

ImageButton bt_callhos = (ImageButton) getView().findViewById(R.id.callhos);

Use this

ImageButton bt_callhos = (ImageButton) view.findViewById(R.id.callhos);

Upvotes: 1

Related Questions