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

General Discussion

jason zheng
jason zheng
1,520 Points

Need help, my emulator keep telling me " unfortunately, crystal ball has stopped. Idk what i did wrong

here is my code.

package com.example.crystalball;

import java.util.Random;

import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

final TextView answerLabel =(TextView)findViewById(R.id.textView2); Button answers =(Button)findViewById(R.id.button1); answers.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
    String [] answers = {
            "It is certain",
            "It is decidely so",
            " All sign say Yes",
            " My reply is no",
            "It is not there yet",
            "Better not tell you yet",
            "cant answer now"
    };

    String answer = " ";
    Random Generator = new Random ();
    int randonNumber = Generator.nextInt(answers.length);

    answer = answers[randonNumber];
    answerLabel.setText(answer);
}
});

} @Override public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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); } }

XTML

<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" />

<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" android:text="Enlighten Me" />

Logcat

06-05 02:30:45.476: D/gralloc_goldfish(1107): Emulator without GPU emulation detected. 06-05 02:30:52.396: I/Choreographer(1107): Skipped 43 frames! The application may be doing too much work on its main thread. 06-05 02:32:08.656: D/AndroidRuntime(1152): Shutting down VM 06-05 02:32:08.656: W/dalvikvm(1152): threadid=1: thread exiting with uncaught exception (group=0xb2aebba8) 06-05 02:32:08.666: E/AndroidRuntime(1152): FATAL EXCEPTION: main 06-05 02:32:08.666: E/AndroidRuntime(1152): Process: com.example.crystalball, PID: 1152 06-05 02:32:08.666: E/AndroidRuntime(1152): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.NullPointerException 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.os.Handler.dispatchMessage(Handler.java:102) 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.os.Looper.loop(Looper.java:136) 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-05 02:32:08.666: E/AndroidRuntime(1152): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 02:32:08.666: E/AndroidRuntime(1152): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 02:32:08.666: E/AndroidRuntime(1152): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-05 02:32:08.666: E/AndroidRuntime(1152): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-05 02:32:08.666: E/AndroidRuntime(1152): at dalvik.system.NativeStart.main(Native Method) 06-05 02:32:08.666: E/AndroidRuntime(1152): Caused by: java.lang.NullPointerException 06-05 02:32:08.666: E/AndroidRuntime(1152): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:31) 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.app.Activity.performCreate(Activity.java:5231) 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 02:32:08.666: E/AndroidRuntime(1152): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 02:32:08.666: E/AndroidRuntime(1152): ... 11 more 06-05 02:32:24.396: I/Process(1152): Sending signal. PID: 1152 SIG: 9 06-05 02:33:23.326: D/AndroidRuntime(1199): Shutting down VM 06-05 02:33:23.326: W/dalvikvm(1199): threadid=1: thread exiting with uncaught exception (group=0xb2aebba8) 06-05 02:33:23.336: E/AndroidRuntime(1199): FATAL EXCEPTION: main 06-05 02:33:23.336: E/AndroidRuntime(1199): Process: com.example.crystalball, PID: 1199 06-05 02:33:23.336: E/AndroidRuntime(1199): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.NullPointerException 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.os.Handler.dispatchMessage(Handler.java:102) 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.os.Looper.loop(Looper.java:136) 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-05 02:33:23.336: E/AndroidRuntime(1199): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 02:33:23.336: E/AndroidRuntime(1199): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 02:33:23.336: E/AndroidRuntime(1199): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-05 02:33:23.336: E/AndroidRuntime(1199): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-05 02:33:23.336: E/AndroidRuntime(1199): at dalvik.system.NativeStart.main(Native Method) 06-05 02:33:23.336: E/AndroidRuntime(1199): Caused by: java.lang.NullPointerException 06-05 02:33:23.336: E/AndroidRuntime(1199): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:31) 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.app.Activity.performCreate(Activity.java:5231) 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 02:33:23.336: E/AndroidRuntime(1199): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 02:33:23.336: E/AndroidRuntime(1199): ... 11 more 06-05 02:33:29.066: I/Process(1199): Sending signal. PID: 1199 SIG: 9 06-05 02:38:16.366: D/AndroidRuntime(1247): Shutting down VM 06-05 02:38:16.366: W/dalvikvm(1247): threadid=1: thread exiting with uncaught exception (group=0xb2aebba8) 06-05 02:38:16.376: E/AndroidRuntime(1247): FATAL EXCEPTION: main 06-05 02:38:16.376: E/AndroidRuntime(1247): Process: com.example.crystalball, PID: 1247 06-05 02:38:16.376: E/AndroidRuntime(1247): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.NullPointerException 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.os.Handler.dispatchMessage(Handler.java:102) 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.os.Looper.loop(Looper.java:136) 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-05 02:38:16.376: E/AndroidRuntime(1247): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 02:38:16.376: E/AndroidRuntime(1247): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 02:38:16.376: E/AndroidRuntime(1247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-05 02:38:16.376: E/AndroidRuntime(1247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-05 02:38:16.376: E/AndroidRuntime(1247): at dalvik.system.NativeStart.main(Native Method) 06-05 02:38:16.376: E/AndroidRuntime(1247): Caused by: java.lang.NullPointerException 06-05 02:38:16.376: E/AndroidRuntime(1247): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:31) 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.app.Activity.performCreate(Activity.java:5231) 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 02:38:16.376: E/AndroidRuntime(1247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 02:38:16.376: E/AndroidRuntime(1247): ... 11 more 06-05 02:40:40.296: D/AndroidRuntime(1291): Shutting down VM 06-05 02:40:40.296: W/dalvikvm(1291): threadid=1: thread exiting with uncaught exception (group=0xb2aebba8) 06-05 02:40:40.306: E/AndroidRuntime(1291): FATAL EXCEPTION: main 06-05 02:40:40.306: E/AndroidRuntime(1291): Process: com.example.crystalball, PID: 1291 06-05 02:40:40.306: E/AndroidRuntime(1291): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.NullPointerException 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.os.Handler.dispatchMessage(Handler.java:102) 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.os.Looper.loop(Looper.java:136) 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-05 02:40:40.306: E/AndroidRuntime(1291): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 02:40:40.306: E/AndroidRuntime(1291): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 02:40:40.306: E/AndroidRuntime(1291): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-05 02:40:40.306: E/AndroidRuntime(1291): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-05 02:40:40.306: E/AndroidRuntime(1291): at dalvik.system.NativeStart.main(Native Method) 06-05 02:40:40.306: E/AndroidRuntime(1291): Caused by: java.lang.NullPointerException 06-05 02:40:40.306: E/AndroidRuntime(1291): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:31) 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.app.Activity.performCreate(Activity.java:5231) 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 02:40:40.306: E/AndroidRuntime(1291): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 02:40:40.306: E/AndroidRuntime(1291): ... 11 more 06-05 02:40:53.716: I/Process(1291): Sending signal. PID: 1291 SIG: 9 06-05 02:41:19.226: D/gralloc_goldfish(1339): Emulator without GPU emulation detected. 06-05 02:43:14.516: D/AndroidRuntime(1383): Shutting down VM 06-05 02:43:14.516: W/dalvikvm(1383): threadid=1: thread exiting with uncaught exception (group=0xb2aebba8) 06-05 02:43:14.526: E/AndroidRuntime(1383): FATAL EXCEPTION: main 06-05 02:43:14.526: E/AndroidRuntime(1383): Process: com.example.crystalball, PID: 1383 06-05 02:43:14.526: E/AndroidRuntime(1383): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.NullPointerException 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.os.Handler.dispatchMessage(Handler.java:102) 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.os.Looper.loop(Looper.java:136) 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-05 02:43:14.526: E/AndroidRuntime(1383): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 02:43:14.526: E/AndroidRuntime(1383): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 02:43:14.526: E/AndroidRuntime(1383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-05 02:43:14.526: E/AndroidRuntime(1383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-05 02:43:14.526: E/AndroidRuntime(1383): at dalvik.system.NativeStart.main(Native Method) 06-05 02:43:14.526: E/AndroidRuntime(1383): Caused by: java.lang.NullPointerException 06-05 02:43:14.526: E/AndroidRuntime(1383): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:31) 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.app.Activity.performCreate(Activity.java:5231) 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 02:43:14.526: E/AndroidRuntime(1383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 02:43:14.526: E/AndroidRuntime(1383): ... 11 more 06-05 02:45:48.016: D/AndroidRuntime(1427): Shutting down VM 06-05 02:45:48.016: W/dalvikvm(1427): threadid=1: thread exiting with uncaught exception (group=0xb2aebba8) 06-05 02:45:48.026: E/AndroidRuntime(1427): FATAL EXCEPTION: main 06-05 02:45:48.026: E/AndroidRuntime(1427): Process: com.example.crystalball, PID: 1427 06-05 02:45:48.026: E/AndroidRuntime(1427): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.NullPointerException 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.os.Handler.dispatchMessage(Handler.java:102) 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.os.Looper.loop(Looper.java:136) 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-05 02:45:48.026: E/AndroidRuntime(1427): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 02:45:48.026: E/AndroidRuntime(1427): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 02:45:48.026: E/AndroidRuntime(1427): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-05 02:45:48.026: E/AndroidRuntime(1427): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-05 02:45:48.026: E/AndroidRuntime(1427): at dalvik.system.NativeStart.main(Native Method) 06-05 02:45:48.026: E/AndroidRuntime(1427): Caused by: java.lang.NullPointerException 06-05 02:45:48.026: E/AndroidRuntime(1427): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:29) 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.app.Activity.performCreate(Activity.java:5231) 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 02:45:48.026: E/AndroidRuntime(1427): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 02:45:48.026: E/AndroidRuntime(1427): ... 11 more