2017-12-09 9 views
3

どういうわけか私のacitivity_main.xmlは、ビューBottomSelectElement(カスタムビュー)を持っていますが、私は活動でそれを見つけることはできませんが、私は何かを見つけることができます。findViewById()カスタムビューを見つけたが、それは、レイアウトに存在しない

(不要な部分を削除)activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="it.bachmann.studytimer.ui.MainActivity"> 

    <!-- THIS IS THE VIEW I CANNOT FIND IN THE MainActivity.java (BottomSelectElement) --> 
    <it.bachmann.studytimer.ui.elements.BottomSelectElement 
     android:id="@+id/customBottomSelect" 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 
     android:layout_gravity="bottom|center" /> 

    <com.github.clans.fab.FloatingActionMenu 

</android.support.design.widget.CoordinatorLayout> 

activity_main.xml

public class BottomSelectElement extends ConstraintLayout { 

    private Spinner spinner; 

    public BottomSelectElement(Context context) { 
     super(context); 

      init(); 
     } 

     public BottomSelectElement(Context context, AttributeSet attributeSet) { 
      super(context); 
      init(); 
     } 

     private void init() { 
      inflate(getContext(), R.layout.bottom_select_element, this); 
      spinner = findViewById(R.id.spinner); 
      List<String> categories = Arrays.asList("foo", "bar", "baz"); 
      ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_text, categories); 
      adapter.setDropDownViewResource(R.layout.spinner_text_checked); 
      spinner.setAdapter(adapter); 
     } 

     public Spinner getSpinner() { 
      return spinner; 
     } 
    } 

し、最終的に見つけることができなかった私のMainActivity.javaに完全に示されている私のカスタムビュークラスcustomBottomSelectが、それが何かを見つけました。

public class MainActivity extends AppCompatActivity { 

    private final String TAG = this.getClass().getSimpleName(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     BottomSelectElement bottomSelectElement = findViewById(R.id.customBottomSelect); 
     Log.d(TAG, "onStart: bottomSelectElement " + bottomSelectElement); // this returns null, althought it should exist! 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 
} 

Androidスタジオを数回再起動し、再構築してクリーニングしました。それはちょうどこのidを見つけるように見えません。

+0

**正確なエラーは何ですか? – Zoe

+0

@Konradは、あなたが(コンテキストコンテキスト、属性セットattrsに、int型defStyle)パラメータを持つ2つだけコンストラクタ、第三1を実装している – tamtom

+0

両方のクラスに欠けているの輸入を提供することができます。 *これはxmlからカスタムビューを適切に膨張させるために必要なものですか? – 0X0nosugar

答えて

4

コンストラクタで、あなたはsuper(context)代わりのsuper(context, attrs)を持っていたので。

を使用すると、IDなどの属性に合格しない場合、ビューは何のIDを持っていませんので、そのIDを使用して検索可能ではない、理にかなっています。 :-)

回答回答:findViewById() returns null for custom component in layout XML, not for other components

+0

私はちょうど解決策を見つけましたが、ありがとう!あなたがそれを記述したのとまったく同じでした。 – Konrad

関連する問題