-2

I am having listview with employeename and radiobutton. I am using setonclicklistner for radioButton but i am getting nullpointer exception and app got crashes. Can someone help me with this problem?

delivery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater inflater1 = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v1 = inflater1.inflate(R.layout.activity_employees_list_for_pop_up, null);
            RadioButton employeechecked = (RadioButton) v1.findViewById(R.id.employeeChecked);
            final Button ok = (Button) v1.findViewById(R.id.do_ok);
            Button cancle = (Button) v1.findViewById(R.id.do_cancle);
            ok.setEnabled(false);
            listView = (ListView) v1.findViewById(R.id.employeePopUpList);
            employeePopUpAdapter = new EmployeePopUpAdapter(ct, employeeIdNameBeans);

            //enable ok button if listitem is checked
            employeechecked.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos=listView.getCheckedItemPosition();
                    if (pos > -1)
                    {
                        ok.setEnabled(true);
                    }
                    else if (pos <= -1)
                    {
                        ok.setEnabled(false);
                    }

                }
            });

error I am getting nullpointer exception when i click on textview

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
  at com.example.jithendra.businessstorestart.adapters.DeliveryOrdersListAdapter$3.onClick(DeliveryOrdersListAdapter.java:300)
  at android.view.View.performClick(View.java:4756)
  at android.view.View$PerformClick.run(View.java:19749)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5221)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Here is layout file

<LinearLayout
    android:id="@+id/lv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/employeeId"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/loading"
        android:textColor="@color/TextcolorforBlack"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/empFullName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="@string/loading"
        android:textColor="@color/TextcolorforBlack"
        android:textSize="20dp" />

    <RadioButton
        android:id="@+id/employeeChecked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/TextcolorforBlack"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_weight="0"
        android:background="@color/TextcolorforBlack" />

</LinearLayout>

This is list I am inflating it in listview. In this listview I am having radiobutton. If radiobutton check I need to enable button

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

1 Answers1

0

employeechecked button view seems to be null,

RadioButton employeechecked = (RadioButton) v1.findViewById(R.id.employeeChecked);

Check whether R.id.employeeChecked exists in the layout you inflated R.layout.activity_employees_list_for_pop_up

Andrew Lam
  • 1,371
  • 17
  • 35