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 trial
james doneghy
710 PointsMy android's app's button does not work in my crystal ball app. Help me.
In my code, I followed all of the directions on the video but my button still doesn't function. Here is what I wrote.. PLEASE HELP!
package com.example.crystalball;
import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our View variables and assign then the Views from the layout file
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// The button was clicked, so update the answer label with an answer
String answer = "";
// Randomly select one of three answers: Yes, No, or Maybe
Random randomGenerator = new Random(); // Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(3);
answer = Integer.toString(randomNumber);
// Update the label with our dynamic answer
answerLabel.setText(answer);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
10 Answers
Steve Hunter
57,712 PointsWhat error do you get? Can you post your LogCat?
james doneghy
710 PointsAt the bottom it says that its supposed to be (R.Activity.menu, menu); But it will only let me do R.menu.main, menu); I think it has to do with the imports but i'm not sure
Jeffrey Wambugu
8,548 Pointshi, the code is alright you need to show us the error you are having in the logcat.
james doneghy
710 PointsHello Jeffrey,
Thank you for responding. Here is the output from my logcat:
06-01 20:52:41.150: E/dalvikvm(1261): Method Lcom/example/crystalball/MainActivity;.getTitle overrides final Landroid/app/Activity;.getTitle 06-01 20:52:41.150: W/dalvikvm(1261): failed creating vtable 06-01 20:52:41.160: W/dalvikvm(1261): Link of class 'Lcom/example/crystalball/MainActivity;' failed 06-01 20:52:41.160: D/AndroidRuntime(1261): Shutting down VM 06-01 20:52:41.160: W/dalvikvm(1261): threadid=1: thread exiting with uncaught exception (group=0xb3aaeba8) 06-01 20:52:41.310: E/AndroidRuntime(1261): FATAL EXCEPTION: main 06-01 20:52:41.310: E/AndroidRuntime(1261): Process: com.example.crystalball, PID: 1261 06-01 20:52:41.310: E/AndroidRuntime(1261): java.lang.VirtualMachineError 06-01 20:52:41.310: E/AndroidRuntime(1261): at dalvik.system.DexFile.defineClassNative(Native Method) 06-01 20:52:41.310: E/AndroidRuntime(1261): at dalvik.system.DexFile.defineClass(DexFile.java:222) 06-01 20:52:41.310: E/AndroidRuntime(1261): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:215) 06-01 20:52:41.310: E/AndroidRuntime(1261): at dalvik.system.DexPathList.findClass(DexPathList.java:322) 06-01 20:52:41.310: E/AndroidRuntime(1261): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54) 06-01 20:52:41.310: E/AndroidRuntime(1261): at java.lang.ClassLoader.loadClass(ClassLoader.java:497) 06-01 20:52:41.310: E/AndroidRuntime(1261): at java.lang.ClassLoader.loadClass(ClassLoader.java:457) 06-01 20:52:41.310: E/AndroidRuntime(1261): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 06-01 20:52:41.310: E/AndroidRuntime(1261): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112) 06-01 20:52:41.310: E/AndroidRuntime(1261): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-01 20:52:41.310: E/AndroidRuntime(1261): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-01 20:52:41.310: E/AndroidRuntime(1261): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-01 20:52:41.310: E/AndroidRuntime(1261): at android.os.Handler.dispatchMessage(Handler.java:102) 06-01 20:52:41.310: E/AndroidRuntime(1261): at android.os.Looper.loop(Looper.java:136) 06-01 20:52:41.310: E/AndroidRuntime(1261): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-01 20:52:41.310: E/AndroidRuntime(1261): at java.lang.reflect.Method.invokeNative(Native Method) 06-01 20:52:41.310: E/AndroidRuntime(1261): at java.lang.reflect.Method.invoke(Method.java:515) 06-01 20:52:41.310: E/AndroidRuntime(1261): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-01 20:52:41.310: E/AndroidRuntime(1261): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-01 20:52:41.310: E/AndroidRuntime(1261): at dalvik.system.NativeStart.main(Native Method)
Jeffrey Wambugu
8,548 PointsOk could you do me a favour and go to window -> open perspective and open the DDMS view. there under the devices tab you will see a small drop down and at the bottom of that drop down you will see reset adb. once it is reset clear the logcat then relaunch the application and if it still does not work send me the logcat message again.
james doneghy
710 Points06-01 21:34:57.570: A/ProcessStats(381): ProcessState dead: name=com.android.systemui pkg=com.android.keyguard uid=10007 common.name=com.android.systemui 06-01 21:34:57.570: A/ProcessStats(381): android.util.Log$TerribleFailure: ProcessState dead: name=com.android.systemui pkg=com.android.keyguard uid=10007 common.name=com.android.systemui 06-01 21:34:57.570: A/ProcessStats(381): at android.util.Log.wtf(Log.java:290) 06-01 21:34:57.570: A/ProcessStats(381): at android.util.Slog.wtfStack(Slog.java:86) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.internal.app.ProcessStats$ProcessState.ensureNotDead(ProcessStats.java:2495) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.internal.app.ProcessStats$ProcessState.setState(ProcessStats.java:2593) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.internal.app.ProcessStats$ProcessState.setState(ProcessStats.java:2578) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.server.am.ActivityManagerService.setProcessTrackerState(ActivityManagerService.java:15348) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.server.am.ActivityManagerService.updateOomAdjLocked(ActivityManagerService.java:15722) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.server.am.ActivityStack.destroyActivityLocked(ActivityStack.java:2803) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.server.am.ActivityStackSupervisor.activityIdleInternalLocked(ActivityStackSupervisor.java:1916) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.server.am.ActivityStackSupervisor$ActivityStackSupervisorHandler.activityIdleInternal(ActivityStackSupervisor.java:2592) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.server.am.ActivityStackSupervisor$ActivityStackSupervisorHandler.handleMessage(ActivityStackSupervisor.java:2610) 06-01 21:34:57.570: A/ProcessStats(381): at android.os.Handler.dispatchMessage(Handler.java:102) 06-01 21:34:57.570: A/ProcessStats(381): at android.os.Looper.loop(Looper.java:136) 06-01 21:34:57.570: A/ProcessStats(381): at com.android.server.am.ActivityManagerService$AThread.run(ActivityManagerService.java:1870)
Jeffrey Wambugu
8,548 Pointssorry james doneghy i have run out of ideas. Ben Jakuben could you help us out here if you get a chance?
Steve Hunter
57,712 PointsJames - would you be able to zip the project and send it over? I can have a look in Eclipse to see if I can replicate the error.
Steve.
james doneghy
710 Pointshow do i post my file onto here
Steve Hunter
57,712 PointsI don't think you can.
Email me steve.hunter@bigdogconsultants.co.uk
Copy in help@teamtreehouse.com too - they may assist!
james doneghy
710 Pointsjames doneghy
710 PointsAt the bottom it says that its supposed to be (R.Activity.menu, menu); But it will only let me do R.menu.main, menu); I think it has to do with the imports but i'm not sure