1

I have an android application when clicked on an option from a side bar it goes to a fragment, and then into another fragment which has clickable radio buttons. When clicked on these it will create a popup window with some text fields in it.

Basically this is how the flow goes,

Activity --> Fragment 1 --> Fragment 2 --> PopupWindow

When i try to access the resources inside this popupWindow, in example:

damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
damageComponenetAutoCompleteTextview.requestFocus();

in the line damageComponenetAutoCompleteTextview.requestFocus(); it gives me an error,

Attempt to invoke virtual method on a null object reference

I believe it's due to a wrong view i'm referring when calling on the Resources. Hope someone could point me to what i'm doing wrong. Thanks in Advance.

This is the method i'm using to create the popupWindow.

public void showDamagedItemEntryPopup(RadioButton radioButton, View view){

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ViewGroup viewG = ((ViewGroup)view.findViewById(R.id.damaged_comp_popup));

    //I tried passing the root view to inflate() method instead of passing it null. Didn't work.
    //View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);

    View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);

    final PopupWindow popupWindow = new PopupWindow(
            popupView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    popupWindow.setAnimationStyle(R.style.popupAnimation);

    //I tried setting the content view manually but didnt work        
    //popupWindow.setContentView(view.findViewById(R.id.damaged_comp_popup));

    Button buttonClose = (Button)popupView.findViewById(R.id.close_add_component_btn);

    // Close button damaged item popop window
    buttonClose.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }
    });


    originalAmount = (EditText)this.findViewById(R.id.popup_add_component_original_amount);
    customerContribution = (EditText)this.findViewById(R.id.popup_percentage);
    quantity = (EditText)this.findViewById(R.id.popup_quantity);
    finalAmount = (EditText)this.findViewById(R.id.popup_add_component_final_amount);
    remarks = (EditText)this.findViewById(R.id.popup_add_component_remarks);

    // Item Spinner
    itemSpinnerArray = new ArrayList<String>();
    itemSpinnerArray.add("Select Item");

    // Status Spinner
    ArrayList<String> statusSpinnerArray = new ArrayList<String>();
    statusSpinnerArray.add("MR");
    statusSpinnerArray.add("DR");
    statusSpinnerArray.add("SP");

    // Error comes at this point initially. When these Resource access line codes are commented, the popup works fine.

    damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
    damageComponenetAutoCompleteTextview.requestFocus();
    ArrayAdapter<String> itemSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, itemSpinnerArray);
    damageComponenetAutoCompleteTextview.setThreshold(1);
    damageComponenetAutoCompleteTextview.setAdapter(itemSpinnerArrayAdapter);

    damageComponenetAutoCompleteTextview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //int index = cityNames.indexOf(actvCity.getText().toString());
            itemSpinnerValue = (String) parent.getItemAtPosition(position);
            Log.d("SK-->", "----------------------------------------------------------");
            Log.d("SK-->","itemSpinnerValue: " + itemSpinnerValue);
        }
    });

    statusSpinner = (Spinner)this.findViewById(R.id.popup_status_spinner);
    ArrayAdapter<String> statusSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, statusSpinnerArray);
    statusSpinner.setAdapter(statusSpinnerArrayAdapter);

    //Creating a text Watcher
    TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            //here, after we introduced something in the EditText we get the string from it
            //String answerString = originalAmount.getText().toString();

            if (originalAmount.getText().toString().trim().equals("") || customerContribution.getText().toString().trim().equals("")
                    || quantity.getText().toString().trim().equals("")) {

                // Error , one or more editText are empty

            }
            else
            {
                calculateFinalAmount();
            }

            //and now we make a Toast
            //modify "yourActivity.this" with your activity name .this
            //Toast.makeText(yourActivity.this,"The string from EditText is: "+answerString,0).show();

        }
    };

    // Adding Text Watcher to our text boxes
    originalAmount.addTextChangedListener(textWatcher);
    customerContribution.addTextChangedListener(textWatcher);
    quantity.addTextChangedListener(textWatcher);

    // Show the popup
    popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

}

This is the XML i'm using for the popup.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/damaged_comp_popup"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/popup_wire">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="2dp"
    android:background="@color/popup_background">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <AutoCompleteTextView
            android:id="@+id/popup_damage_component_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="5dp"
            android:hint="@string/damaged_componenet_item_string"/>

        <Spinner
            android:id="@+id/popup_status_spinner"
            android:layout_width="122dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/popup_damage_component_item"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="5dp" />

        <EditText
            android:id="@+id/popup_add_component_original_amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/popup_damage_component_item"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="22dp"
            android:layout_toRightOf="@+id/popup_status_spinner"
            android:ems="10"
            android:hint="@string/original_amount_string"
            android:inputType="number" />

        <EditText
            android:id="@+id/popup_percentage"
            android:layout_width="52dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/popup_status_spinner"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="2dp"
            android:layout_marginTop="22dp"
            android:ems="10"
            android:hint="@string/percentage_string"
            android:inputType="number" />

        <TextView
            android:id="@+id/popup_percentageMark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/popup_percentage"
            android:layout_toRightOf="@+id/popup_percentage"
            android:text="@string/percentage_string"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp" />

        <EditText
            android:id="@+id/popup_quantity"
            android:layout_width="46dp"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/popup_percentage"
            android:layout_alignBottom="@+id/popup_percentage"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@+id/popup_percentageMark"
            android:ems="10"
            android:hint="@string/quantity_string"
            android:inputType="number" />

        <EditText
            android:id="@+id/popup_add_component_final_amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/popup_quantity"
            android:layout_alignBottom="@+id/popup_quantity"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@+id/popup_quantity"
            android:ems="10"
            android:hint="@string/final_amount_string"
            android:inputType="number" />

        <EditText
            android:id="@+id/popup_add_component_remarks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/popup_percentage"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="22dp"
            android:ems="10"
            android:hint="@string/remarks_string"
            android:inputType="text|textMultiLine" />

        <Button
            android:id="@+id/add_component_btn"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="200dp"
            android:layout_below="@+id/popup_add_component_remarks"
            android:background="@drawable/correct"
            android:layout_marginBottom="15dp"
            android:onClick="onSaveItem"/>

        <Button
            android:id="@+id/close_add_component_btn"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/popup_add_component_remarks"
            android:layout_toRightOf="@+id/add_component_btn"
            android:layout_marginLeft="10dp"
            android:background="@drawable/cancel"/>

    </RelativeLayout>

</LinearLayout>

k9yosh
  • 858
  • 1
  • 11
  • 31

1 Answers1

2

It seems your damageComponenetAutoCompleteTextview belongs to the popupWindow which you inflated at top.

So try changing

 damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);

To

damageComponenetAutoCompleteTextview = (AutoCompleteTextView) popupView.findViewById(R.id.popup_damage_component_item);

And other relevant elements as well.

k9yosh
  • 858
  • 1
  • 11
  • 31
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68