当我尝试运行它说的代码时,应用程序不幸停止了。请帮助我编写代码。下面是我想要解决方案的代码。
package gaurav.android;
import java.util.*;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class EventActivity extends Activity {
/** Called when the activity is first created. */
View colorregion;
int[] colorchoices = {Color.BLACK , Color.BLUE, Color.CYAN};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
colorregion = findViewById(R.id.color_region);
Button clbutton = (Button)findViewById(R.id.color_button);
clbutton.setOnClickListener((OnClickListener) new ColorRandomizer());
}
private void regioncolor(int color)
{
colorregion.setBackgroundColor(color);
}
private class ColorRandomizer implements onClickListener
{
public void onClick(View v)
{
Random r= new Random();
int index= r.nextInt(colorchoices.length);
regioncolor(colorchoices[index]);
}
}
}