2016-04-22 8 views
1

サイドバーからオプションをクリックするとアンドロイドアプリケーションが表示されます。フラグメントに移動し、クリック可能なラジオボタンを持つ別のフラグメントに移動します。これらをクリックすると、いくつかのテキストフィールドを含むポップアップウィンドウが作成されます。PopupWindow内のレイアウト内のリソースにアクセスできない、オブジェクトがヌル参照されている

基本的に、これはフローがどのようになる、

活動である - 私は一例では、このpopupWindow内のリソースにアクセスしようとすると> PopupWindow

- >フラグメント1 - >フラグメント2 :ラインdamageComponenetAutoCompleteTextview.requestFocus()で

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

。それは私にエラーを与える、nullオブジェクト参照、私はそれがリソース上で呼び出したとき、私は言及しています間違ったビューによるものだと考えてい

上で仮想メソッドを呼び出すための

試み。誰かが私が間違っていることを教えてくれることを願っています。前もって感謝します。

これは、私が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); 

} 

これは私がポップアップに使用しているXMLです。

<?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> 

+0

xmlファイルも含めてください。 – aProperFox

+0

'R.id.popup_damage_component_item'が別のビューに属しているようですが、初期化行を' someView.findViewById(R.id.popup_damage_component_item);に変更する必要があるかもしれません。 –

+0

@aProperFox申し訳ありません、XMLを今追加しました。 – k9yosh

答えて

1

あなたのdamageComponenetAutoCompleteTextviewはあなたがトップで膨張popupWindowに属しているようです。

ので、同様

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

およびその他の関連要素に

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

を変更してみてください。

関連する問題