當我試圖運行它的代碼說,應用程序已停止不幸。請幫助我與我的編碼。以下是我想要解決方案的代碼。應用程序強行關閉運行。
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]);
}
}
}
的main.xml和日誌貓請........ –
第一問 – Trikaldarshi
之前,你應該調試代碼粘貼到您的logcat顯示你得到了什麼樣的錯誤 –