- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的代码:
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/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!