当前位置: 代码迷 >> Android >> 类的初始化的有关问题
  详细解决方案

类的初始化的有关问题

热度:13   发布时间:2016-05-01 18:02:50.0
类的初始化的问题
有下面代码:功能很简单就是点击图片,自动更换的功能。
public class TdwuActivity extends Activity
{ final ImageView imageView = new ImageView(this);//如果将imageview放在这里会出现问题 static int[] images = new int[]
{ R.drawable.ajax, R.drawable.classic, R.drawable.ee, R.drawable.java };
int currentimage = 0;
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
final ImageView imageView = new ImageView(this);//在这里则没有事情 super.onCreate(savedInstanceState);
setContentView(R.layout.main);
  LinearLayout layout = (LinearLayout) findViewById(R.id.mysql);

layout.addView(imageView);
imageView.setImageResource(images[0]);
imageView.setOnClickListener(new OnClickListener()
{

public void onClick(View v)
{
imageView.setImageResource(images[currentimage + 1]);
currentimage++;
if (currentimage == 3)
{
currentimage = 0;
}

}
});

}
}



怎么回事那?抛出的异常是空异常。

------解决方案--------------------
final ImageView imageView = new ImageView(this);//如果将imageview放在这里会出现问题
把final去掉
  相关解决方案