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 trialTimmy Yong
Courses Plus Student 2,254 PointsUnfortunately your App has been stopped
Here is my main Activity code. It keeps showing that annoying error your has been stopped. Although i have no errors. I may have changed some names of what Mr. Ben asked to follow but wasnt my choice it was the auto import ..Like showFactButton to button. and Facttextview to action_setting
package com.FunFacts.truefunfacts;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class MyTrueFunFactsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_true_fun_facts);
final TextView FactLabel = (TextView)findViewById(R.id.action_settings);
Button button = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String fact = " New iPhone bends";
FactLabel.setText(fact);
}
};
button.setOnClickListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_true_fun_facts, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2 Answers
Daniel Hartin
18,106 PointsHi Timmy
Okay spotted 3 mistakes, 1 will result in an error. (Possibly?)
Okay in your MyTrueFunFactsActivity class paste in the following code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_true_fun_facts);
final TextView FactLabel = (TextView) findViewById(R.id.factArea);
Button button = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
FactLabel.setText(FactBook.getfact());
}
};
button.setOnClickListener(listener);
}
I have changed the id of the text view to match the XML file below and set the setText() method to set something more useful (a randomly chosen fact from your factbook class)
The fact book class is correct only I would remove the line: fact = randomNumber + ""; as it is doing nothing you set the value of fact in the next line to something else.
The XML file should be as below
<TextView
android:text="Did you know ?"
android:id="@+id/factArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#50ffffff" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tap for another Fact"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="47dp"
android:textSize="24sp"
android:background="@android:color/white" />
Hopefully this should now work once you paste the code in. Make sure to save all files to make sure the errors go away. If you get anymore trouble just give me a shout back here
Thanks
Daniel
Daniel Hartin
18,106 PointsHi Timmy
I think your problem is the one you have already commented on.
action_setting is more commonly used in the action bar(I think this is incorrect). the button id and factlabel id must match exactly what is declared in your XML file called activity_my_true_fun_facts.xml
look in your xml file and find the Button and TextView elements. Look at the lines that begin with android:id="@+id/" whatever comes after the / is the name of your id, this must be exactly the same as what is declared in your java code, failure to do so will result in an error and close the app. It is probably the case that intellisense recommended the incorrect layout for you.
I hope this solves it for you but if not please paste your XML code here as well and we can take a look
Thanks Daniel
Timmy Yong
Courses Plus Student 2,254 PointsThanks Daniel for your reply
Here is the code of the main activity, the class, and the xml file
package com.FunFacts.truefunfacts;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;
import java.util.Random;
public class MyTrueFunFactsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_true_fun_facts);
final TextView FactLabel = (TextView)findViewById(R.id.action_settings);
Button button = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
FactLabel.setText("");
}
};
button.setOnClickListener(listener);
}
}
package com.FunFacts.truefunfacts;
import java.util.Random;
/**
-
Created by Timmy on 9/26/2014. */ public class FactBook {
public String getfact(){ String[] facts = {"“A goal is not always meant to be reached, it often serves simply as something to aim at.”", "“The habit of persistence is the habit of victory “", "“Little minds are tamed and subdued by misfortune; but great minds rise above them”", "“Limitations live only in our minds but if we use our imaginations our possibilities become limitless.”", "“Failure is the opportunity to begin again more intelligently “", "“Every adversity, every failure , every heartache carries with it the seed of an equal or greater benefit “", "“We are all in the same boat in a stormy sea and we owe each other a terrible loyalty “", "“We tend to forget that happiness doesn’t come as a result of getting something we don’t have, but rather of recognizing and appreciating what we do have.”", "“I know for sure that what we dwell on is who we become “", "“Your focus determines your reality “" }; String fact = ""; Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(facts.length); fact = randomNumber + ""; fact =facts [randomNumber]; return fact;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MyTrueFunFactsActivity" android:background="#ff51b46d">
<TextView android:text="Did you know ?" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:textColor="#50ffffff" />
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Tap to another Fact" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="47dp" android:textSize="24sp" android:background="@android:color/white" />
</RelativeLayout>
Timmy Yong
Courses Plus Student 2,254 PointsTimmy Yong
Courses Plus Student 2,254 PointsThank you so much. It works fine now. There is still something im wondering about why "Did you know sentence " disappears once I tap the button ? any clue for this
Timmy Yong
Courses Plus Student 2,254 PointsTimmy Yong
Courses Plus Student 2,254 PointsOh I guess figured it out . Because i didnt create a text view on the XML file Thanks A lot again :)
Daniel Hartin
18,106 PointsDaniel Hartin
18,106 PointsHi Timmy,
Sorry I forgot about the small header at the top in this app, there should be 2 TextViews in your code. I have made a simple layout below which will work although it's not very pretty, not sure where you are with the course you may want to go over the initial layout again to get the layout the same as show in the video.
This XML code can be pasted straight in and will work fine.
Sorry for the initial confusion
Hope this helps
Daniel
Timmy Yong
Courses Plus Student 2,254 PointsTimmy Yong
Courses Plus Student 2,254 PointsThank you so much. It worked and now im working on the second project .Blog Reader But im facing problem and dunno why is it because im using android Studio instead of eclipse or is there another reason for this ? ADB is not responding. You can wait more, or kill "adb.exe" process manually and click Restart.
This problem keeps occurring when I try to run the APP. Please Help
P.S. Mr.Ben said try to run the app after we have created master detail activity ..Im not using "eclipse"
Daniel Hartin
18,106 PointsDaniel Hartin
18,106 PointsHi Timmy
Sorry this problem doesn't sound like anything I've come across before. I don't think it is related to your java code otherwise it would simply error out.
If anything, I would put it down to the emulator (if your not testing on a device) one thing that I noticed is that if when you set-up the device in the AVD manager try lowering the number in the RAM as anything above 512mb on windows machines causes problems.
Perhaps try a real device to try and eliminate the possibility of your JAVA code causing the problem (if it works on a device the code is fine and the problem is definitely the emulator) in that case either use a real device or try using genymotion (you can search for this although Ben/treehouse has a great section devoted to genymotion in the Android tools section, so skip to this if you need to know more).
Hope this was of some help
Daniel