Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnkit Khatri
Courses Plus Student 810 PointsUnfortunately App Stopped!
final RelativeLayout rl= (RelativeLayout)findViewById(R.id.rl1);
final TextView funFact = (TextView)findViewById(R.id.funFact1) ;
Button showFacts = (Button)findViewById(R.id.button);
final Random random_gen = new Random();
View.OnClickListener hi= new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] s1={"ffgd","terrgdf","regerg","rtgergf"};
String s="";
int ran = random_gen.nextInt(s1.length);
s=s1[ran];
String[] s2=
{"#c25975", // mauve
"#e15258", // red
"#f9845b", // orange
"#838cc7", // lavender
"#7d669e", // purple
"#53bbb4", // aqua
"#51b46d", // green
"#e0ab18", // mustard
"#637a91", // dark gray
"#f092b0", // pink
"#b7c0c7" };
int ran1 = random_gen.nextInt(s2.length);
int s3=Integer.parseInt(s2[ran1]);
rl.setBackgroundColor(s3);
funFact.setText(s);
}
};
showFacts.setOnClickListener(hi);
App stopped working after i tried to change the background color using these codes.
4 Answers
Harry James
14,780 PointsHey Ankit!
It looks like you've wrote all of this code inside of your OnClickListener. You will want to move s1
, s2
and s3
out of the OnClickListener().
Also, if you still get crashes after this, could you please post a Logcat of your error here? To do that, paste the contents of your LogCat here after you have ran the app as shown on the image below:
Image available in full quality here
As you can see, I have set the Log Level to error so that only the error will be displayed.
If you can't see what I can, make sure to click on the Android tab on the bottom and the Devices | logcat tab.
Hope it helps and if you run into more issues after this, give me a shout again :)
Ankit Khatri
Courses Plus Student 810 Points@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout rl= (RelativeLayout)findViewById(R.id.rl1);
final TextView funFact = (TextView)findViewById(R.id.funFact1) ;
Button showFacts = (Button)findViewById(R.id.button);
final Random random_gen = new Random();
final String[] s1={"ffgd","terrgdf","regerg","rtgergf"};
String[] s2=
{"#c25975", // mauve
"#e15258", // red
"#f9845b", // orange
"#838cc7", // lavender
"#7d669e", // purple
"#53bbb4", // aqua
"#51b46d", // green
"#e0ab18", // mustard
"#637a91", // dark gray
"#f092b0", // pink
"#b7c0c7" };
int ran1 = random_gen.nextInt(s2.length);
final int s3=Integer.parseInt(s2[ran1]);
View.OnClickListener hi= new View.OnClickListener() {
@Override
public void onClick(View v) {
int ran = random_gen.nextInt(s1.length);
String s="";
s=s1[ran];
rl.setBackgroundColor(s3);
funFact.setText(s);
}
};
showFacts.setOnClickListener(hi);
code after moving s1, s2, and s3 out of onClickListener()
LogCat errors
10-03 00:21:21.824 1934-1934/com.examples.www.fun E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.examples.www.fun, PID: 1934
java.lang.NumberFormatException: Invalid int: "#c25975"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at com.examples.www.fun.MainActivity$1.onClick(MainActivity.java:49)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-03 00:23:22.466 2418-2418/com.examples.www.fun E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.examples.www.fun, PID: 2418
java.lang.NumberFormatException: Invalid int: "#51b46d"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at com.examples.www.fun.MainActivity$1.onClick(MainActivity.java:45)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-03 01:02:04.951 2527-2527/com.examples.www.fun E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.examples.www.fun, PID: 2527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examples.www.fun/com.examples.www.fun.MainActivity}: java.lang.NumberFormatException: Invalid int: "#7d669e"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NumberFormatException: Invalid int: "#7d669e"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at com.examples.www.fun.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Harry James
14,780 PointsHey again Ankit!
The problem lies at this line:
int s3=Integer.parseInt(s2[ran1]);
Instead of parsing an integer here, you want to parse a color instead with the parseColor() method. Note that this will still return an int however, it will be correctly parsed as a color. Therefore, we just need to change the method, not the return type.
Hope it helps and again, if you run into any more problems, be sure to post a new Logcat of the issue :)
Ankit Khatri
Courses Plus Student 810 PointsIts working now. Thank You!!