应用程序强行关闭了运行。 [英] Application Forcefully closed on run.
本文介绍了应用程序强行关闭了运行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我试图运行code,它说,该应用程序已停止不幸。请帮助我与我的编码。下面是我要解决的code。
包gaurav.android;
导入的java.util。*;
进口android.app.Activity;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
公共类EventActivity延伸活动{
/ **第一次创建活动时调用。 * /
查看colorregion;
INT [] colorchoices = {Color.BLACK,Color.BLUE,Color.CYAN};
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
colorregion = findViewById(R.id.color_region);
按钮clbutton =(按钮)findViewById(R.id.color_button);
clbutton.setOnClickListener((OnClickListener)新ColorRandomizer());
}
私人无效regioncolor(INT颜色)
{
colorregion.setBackgroundColor(颜色);
}
私有类ColorRandomizer实现onClickListener
{
公共无效的onClick(视图v)
{
随机R =新的随机();
INT指数= r.nextInt(colorchoices.length);
regioncolor(colorchoices [指数]);
}
}
}
解决方案
更改code
私有类ColorRandomizer实现onClickListener
{
公共无效的onClick(视图v)
{
随机R =新的随机();
INT指数= r.nextInt(colorchoices.length);
regioncolor(colorchoices [指数]);
}
}
本
私有类ColorRandomizer实现OnClickListener {
公共无效的onClick(视图v){
随机R =新的随机();
INT指数= r.nextInt(colorchoices.length);
regioncolor(colorchoices [指数]);
}
}
它不是 onClickListener
它的 OnClickListener
When I tried to run the code it says , the application has stopped unfortunately .Please help me with my coding . Below is the code for which I want the solution .
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]);
}
}
}
解决方案
Change your code
private class ColorRandomizer implements onClickListener
{
public void onClick(View v)
{
Random r= new Random();
int index= r.nextInt(colorchoices.length);
regioncolor(colorchoices[index]);
}
}
to this
private class ColorRandomizer implements OnClickListener {
public void onClick(View v) {
Random r = new Random();
int index = r.nextInt(colorchoices.length);
regioncolor(colorchoices[index]);
}
}
its not onClickListener
its OnClickListener
这篇关于应用程序强行关闭了运行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文