引起:java.lang.NullPointerException:尝试调用虚方法

时间:2016-02-05 07:52:50

标签: android image dialog

我正在尝试将图像插入到对话框内的linearlayout。但是在运行应用程序时,我得到了nullpointer异常错误。 我想知道如何在对话框中获取视图对象。

这是我在oncreate函数中的代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compinfo);
    dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.activity_compinfopopup);
    dialog.getWindow().getAttributes().width = AbsListView.LayoutParams.MATCH_PARENT;
    dialog.getWindow().getAttributes().height = AbsListView.LayoutParams.WRAP_CONTENT;
    dialog.show();
    myGallery = (LinearLayout) findViewById(R.id.mygallery);
    myGallery.addView(insertPhoto("http://www.a.com/demos/img/home/01.jpg"));
    myGallery.addView(insertPhoto("http://www.a.com/demos/img/home/02.jpg"));
    myGallery.addView(insertPhoto("http://www.a.com/demos/img/home/02.jpg"));
    myGallery.addView(insertPhoto("http://www.a.com/demos/img/home/02.jpg"));
}

请帮忙

1 个答案:

答案 0 :(得分:1)

myGallery =(LinearLayout)dialog.findViewById(R.id.mygallery);