findViewById找不到以编程方式生成的视图的视图

时间:2014-03-09 20:02:19

标签: android

当我致电findViewById()获取按钮(以ImageView的形式)时,它会返回null。我查看了these suggestions,我可以告诉你

  • findViewById()setContentView()之后被称为
  • 只有一个布局:activity_main.xml
  • 我没有使用ExpandableListView
  • findViewById()topbar.findViewById()都返回null

我还有什么可能忽略的?

activity_main.xml中

<LinearLayout>
 <GridView
   android:id="@+id/topbar_grid">
 </GridView>
</LinearLayout>

Mainactivity.java

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initializeTopBar();
        initializeMenu();
    }

    private void initializeTopBar() {
        GridView topbar = (GridView) findViewById(R.id.topbar_grid);
        TopBarAdapter topBarAdapter = new TopBarAdapter(this);
        topbar.setAdapter(topBarAdapter);

        ImageView playButton = (ImageView) findViewById(topBarAdapter.playButtonId);
        // Debugging shows playButton is null here
        // Removing the below statement results in the app 
        // being displayed just fine
        playButton.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                onPlayButtonClick();
            }
        }); 
    }

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

TopBarAdapter.java

public class TopBarAdapter extends BaseAdapter {
    private Context context;
    private View[] views = new View[2];

    public int playButtonId = 2000;
    public int trackLabelId = 2001;

    public TopBarAdapter(Context c){
        context = c;
        createTrackLabel();
        createPlayButton();
    }

    private void createPlayButton(){
        ImageView playButton = new ImageView(context);
        playButton.setImageResource(R.drawable.play_icon);
        playButton.setScaleType(ScaleType.CENTER_CROP);
        playButton.setLayoutParams(new GridView.LayoutParams(50, 50));
        playButton.setId(playButtonId);
        views[1] = playButton;
    }




    @Override
    public int getCount() {
        return views.length;
    }

    @Override
    public Object getItem(int position) {
        return views[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return views[position];
    }
}

2 个答案:

答案 0 :(得分:2)

您应该在顶部栏上设置OnItemClickListener

topbar.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) {

    }
});

答案 1 :(得分:0)

playButton是否添加到您的布局?它来自哪里? 使用findViewById方法的最佳方法是依赖Android-generater ID,而不是

     Button playButton = (Button) findViewById(topBarAdapter.playButtonId);

你应该依赖

按钮playButton =(Button)findViewById(R.id.playButtonId);

鉴于带有<Button/> id的{​​{1}}实际上是playButtonId指定的当前视图布局的一部分。在寻找R.layout.activity_main孩子的身份证时,您应该致电

topBarAdapter