gpt4 book ai didi

java - "The constructor is undefined "错误

转载 作者:太空狗 更新时间:2023-10-29 16:24:14 26 4
gpt4 key购买 nike

我的代码:

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

}
}

mLinearLayout = new LinearLayout(this)i = new ImageView(this) 出现“构造函数未定义”错误。

最佳答案

看起来您正在从 ImageAdapter 调用 LinearLayout(Context context),因此您不能使用 this。您需要传递一个上下文,并且 ImageAdapter 中的 this 不引用上下文。使用您本地声明的变量 mContext

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

关于java - "The constructor is undefined "错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6262116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com