当我试图运行它所说的代码时,应用程序已经停止了。请帮我编写代码。下面是我想要解决方案的代码。
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]);
}
}
}
答案 0 :(得分:1)
更改您的代码
private class ColorRandomizer implements onClickListener
{
public void onClick(View v)
{
Random r= new Random();
int index= r.nextInt(colorchoices.length);
regioncolor(colorchoices[index]);
}
}
到此
private class ColorRandomizer implements OnClickListener {
public void onClick(View v) {
Random r = new Random();
int index = r.nextInt(colorchoices.length);
regioncolor(colorchoices[index]);
}
}
它不是onClickListener
OnClickListener
答案 1 :(得分:0)
package com.test;
import java.util.Random;
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(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]);
}
}
}