0

I have a listview.On click of particular item of listview,i am opening a layout from bottom which has 3 buttons and on click of any of the 3 buttons i want to place a call,but when i click on any of the button i am getting null object reference even after creating objects of those button

public class CallActivity extends ListActivity {

    private Button sales,service,insurance;

    private static final String[] items={"Dahisar Brach","Borivali West ASC","Nagpada ASC"
            ,"Borivali East ASC","Kandivali West Workshop","Kandivali West"};

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_call);

        sales=findViewById(R.id.sales);
        service=findViewById(R.id.service);
        insurance=findViewById(R.id.insurance);

        setListAdapter(new ArrayAdapter<String>(this,
                R.layout.list_items,
                items));

    }

    @Override
    public void onListItemClick(ListView parent, View v, int position,
                                long id) {

        final Dialog mBottomSheetDialog = new Dialog(CallActivity.this, R.style.MaterialDialogSheet);
        mBottomSheetDialog.setContentView(R.layout.view); // your custom view.
        mBottomSheetDialog.setCancelable(true);
        mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
        mBottomSheetDialog.show();

        if(position==0){
            sales.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri call = Uri.parse("tel:" + "02228921114");
                    Intent callNumber = new Intent(Intent.ACTION_DIAL, call);
                    callNumber = Intent.createChooser(callNumber, "Call Number");
                    startActivity(callNumber);
                }
            });

            service.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri call = Uri.parse("tel:" + "101");
                    Intent callNumber = new Intent(Intent.ACTION_DIAL, call);
                    callNumber = Intent.createChooser(callNumber, "Call Number");
                    startActivity(callNumber);
                }
            });


            insurance.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri call = Uri.parse("tel:" + "100");
                    Intent callNumber = new Intent(Intent.ACTION_DIAL, call);
                    callNumber = Intent.createChooser(callNumber, "Call Number");
                    startActivity(callNumber);
                }
            });
        }


    }
}

view(layout file)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="To whom you want to call?"
        android:gravity="center"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Sales"
        android:gravity="center"
        android:id="@+id/sales"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Service"
        android:gravity="center"
        android:id="@+id/service"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Insurance"
        android:id="@+id/insurance"
        android:gravity="center"
        />
</LinearLayout>

What am i doing wrong,Please help.I even tried creating the object in onListItemClick method,but i am getting the same error. FYI, I am using compile 'com.android.support:appcompat-v7:26.+', so i don't need to typecast my button

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

You are using custom view for Dialog. So, your Button reference is not available in onCreate(). So you have to do something like this:

Dialog dialog = new Dialog(YourActivity.this, android.R.style.Theme_Light_NoTitleBar_Fullscreen);
View alertView = getLayoutInflater().inflate(R.layout.your_custom_layout, null, false);
Button button = (Button) alertView.findViewById(R.id.buttonId);
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
1

Buttons sales,servies etc are not part of activity_call layout , they are part of your dialog layout so you need to apply findViewById on dialog reference.

do this

final Dialog mBottomSheetDialog = new Dialog(CallActivity.this, R.style.MaterialDialogSheet);
mBottomSheetDialog.setContentView(R.layout.view); // your custom view.

sales     =  mBottomSheetDialog.findViewById(R.id.sales);
service   =  mBottomSheetDialog.findViewById(R.id.service);
insurance  = mBottomSheetDialog.findViewById(R.id.insurance);

mBottomSheetDialog.setCancelable(true);
mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
mBottomSheetDialog.show();

instead of this

    sales=findViewById(R.id.sales);
    service=findViewById(R.id.service);
    insurance=findViewById(R.id.insurance);
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • This also works thanks man,I have upvoted your answer,but Satan answered first so i accepted his answer –  Sep 10 '17 at 07:09
  • it's ok but i answered because previously satan's answer was using `AlertDialog.BuilderDialog` (he modified after my answer) and also extra code of inflater so instead i would prefer a small change in current code , happy coding – Pavneet_Singh Sep 10 '17 at 07:12
  • yeah right..... –  Sep 10 '17 at 07:14