java “构造函数未定义”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6262116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:05:18  来源:igfitidea点击:

" The constructor is undefined " error

javaandroidconstructor

提问by Samuel

My Code:

我的代码:

public class ViewsActivity extends Activity implements OnClickListener  
{
    //
    private Integer[] ImgIds = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4 };
    private LinearLayout mLinearLayout;
    private Gallery g;
    private MediaPlayer mp;
    private ImageView i;
    private Thread th;
    private Runnable viewUpdate;
    private int m_index = 0;


    private OnClickListener mImageListener = new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {

            setContentView(R.layout.main);
            g = (Gallery) findViewById(R.id.gallery1);
            g.setAdapter(new ImageAdapter(v.getContext()));
            g.setOnItemClickListener(mGalleryClickListener);
            g.setOnItemSelectedListener(mGallerySelectedListener);
        }
    };

    private OnItemSelectedListener mGallerySelectedListener = new OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView adapter, View v, int i, long lng) 
        {

            String descr = "";
            switch(i)
            {
            case 0:
                descr = "scena1";
                break;
            case 1:
                descr = "scena2";
                break;
            case 2:
                descr = "scena3";
                break;
            case 3:
                descr = "scena4";
                break;
            case 4:
                descr = "scena5";
                break;
            case 5:
                descr = "scena6";
                break;
            case 6:
                descr = "scena7";
                break;
            case 7: 
                descr = "scena8";
                break;
            case 8:
                descr = "scena9";
                break;
            case 9:
                descr = "scena10";
                break;
            }
            Toast.makeText(ViewsActivity.this, descr, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView arg0) 
        {

        }
    };

    private OnItemClickListener mGalleryClickListener = new OnItemClickListener()
    {

        public void onItemClick(AdapterView parent, View v, int position, long id) 
        {

            switch(position)
            {
            case 0:
                i.setImageResource(R.drawable.pic1);
                break;
            case 1:
                i.setImageResource(R.drawable.pic2);
                break;
            case 2:
                i.setImageResource(R.drawable.pic3);
                break;
            case 3:
                i.setImageResource(R.drawable.pic4);
                break;
            case 4:
                i.setImageResource(R.drawable.pic5);
                break;
            case 5:
                i.setImageResource(R.drawable.pic6);
                break;
            case 6:
                i.setImageResource(R.drawable.pic7);
                break;
            case 7:
                i.setImageResource(R.drawable.pic8);
                break;
            case 8:
                i.setImageResource(R.drawable.pic9);
                break;
            case 9: 
                i.setImageResource(R.drawable.pic10);
                break;
            }




            setContentView(mLinearLayout);
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mp = MediaPlayer.create(this, R.raw.song);
        mp.start();
        Gallery g = (Gallery) findViewById(R.id.gallery1);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView parent, View v, int position, long id) { 
                if (position >= ImgIds.length) { 
                    position = position % ImgIds.length; 
                } 
              //  Toast.makeText(this, ""+position, Toast.LENGTH_SHORT).show();
        }
        });

    }
        //
        public class ImageAdapter extends BaseAdapter { 
            int mGalleryItemBackground; 
            private Context mContext; 

            public ImageAdapter(Context c) { 
                mContext = c; 
                TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
                mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 

                a.recycle(); 
            } 

            public int getCount() { 
                return Integer.MAX_VALUE; 
            } 

            public Object getItem(int position) { 
                if (position >= ImgIds.length) { 
                    position = position % ImgIds.length; 
                } 
                return position; 
            } 

            public long getItemId(int position) { 
                if (position >= ImgIds.length) { 
                    position = position % ImgIds.length; 
                } 
                return position; 
            } 

            public View getView1(int position, View convertView, ViewGroup parent) { 
                ImageView i = new ImageView(mContext); 
                if (position >= ImgIds.length) { 
                    position = position % ImgIds.length; 
                } 
                i.setImageResource(ImgIds[position]); 
                i.setLayoutParams(new LayoutParams(80, 80)); 
                i.setScaleType(ImageView.ScaleType.FIT_XY); 
                i.setBackgroundResource(mGalleryItemBackground); 
                return i; 
            } 

            public int checkPosition(int position) { 
                if (position >= ImgIds.length) { 
                    position = position % ImgIds.length; 
                }

                return position; 




      final Button buttonavanti = (Button) findViewById(R.id.Avanti);
        buttonavanti.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                g.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);


            }
        }); 

        final Button buttonindietro = (Button) findViewById(R.id.Indietro);
        buttonindietro.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
                g.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
            }
        });



        final Button buttonexit = (Button) findViewById(R.id.Exit);
        buttonexit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();   
                }
        });


        mLinearLayout = new LinearLayout(this);

        i = new ImageView(this);
        i.setImageResource(R.drawable.pic1);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        mLinearLayout.addView(i);

        mLinearLayout.setOnClickListener(mImageListener);




    }


    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        return null;
    }

    }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
}

I've got an "the constructor is undefined" error with mLinearLayout = new LinearLayout(this)and with i = new ImageView(this).

我有一个“构造函数未定义”的错误mLinearLayout = new LinearLayout(this)i = new ImageView(this)

回答by Jon

Looks like you're calling LinearLayout(Context context)from ImageAdapter, so you can't use this. You need to pass a Context and thisin ImageAdapter does not refer to a Context. Use your locally declared variable mContext

看起来你是LinearLayout(Context context)从 ImageAdapter调用的,所以你不能使用this. 您需要传递一个 Context 并且this在 ImageAdapter 中不引用一个 Context。使用本地声明的变量mContext

mLinearLayout = new LinearLayout(mContext)
i = new ImageView(mContext).

回答by ngesh

This will work..

这将工作..

mLinearLayout = new LinearLayout(ViewsActivity.this);

回答by keyboardsurfer

This is the same as just creating a View or Layout without the parameters layout_width and layout_height.

这与仅创建没有参数 layout_width 和 layout_height 的视图或布局相同。

You need to call the constructor that also takes LayoutParams as a parameter and provide it with data.

您需要调用同样以 LayoutParams 作为参数的构造函数并为其提供数据。