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

Android

fun facts crashes on avd

after i set the on click listener for the button and write the next fun fact which is "did you know ostriches run faster then horses" i run the app and on the avd it says "unfortunately fun facts has stopped working

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

When the app crashes, it should have some information in the logcat view in Android Studio. (If you are missing the logcat view, click on the tab "6: Android" at the bottom and then make sure the "Devices | logcat" tab is selected.)

Let us know what the errors are in there and we can help you troubleshoot.

Hey Ben thanks for the help. So this is what im getting on my logcat:

09-24 11:42:23.200      847-847/com.example.mers.funfacts2 D/AndroidRuntime﹕ Shutting down VM
09-24 11:42:23.200      847-847/com.example.mers.funfacts2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a79ba8)
09-24 11:42:23.220      847-847/com.example.mers.funfacts2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.mers.funfacts2, PID: 847
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mers.funfacts2/com.example.mers.funfacts2.FunFactsActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.mers.funfacts2.FunFactsActivity.onCreate(FunFactsActivity.java:31)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Thanks! Okay, so this is a lot of information, but the lines we are most interested are these:

Caused by: java.lang.NullPointerException
            at com.example.mers.funfacts2.FunFactsActivity.onCreate(FunFactsActivity.java:31)

That tells us the problem and where it occurred. Check out line 31 in FunFactsActivity.java. If you can't figure out what the error is, paste all your code in here from FunFactsActivity.java and we'll help you troubleshoot. (If you paste it in, let us know which line is line 31.)

Ok so i tried fixing it by deleting line 31 and the app opens up but the button doesnt click to go to the next fun fact and then when i put that line back in the app crashes. I have watched the video again to see if my code looks like yours and it does so im not sure what im doing wrong.... So this is my code and i put "line#31" to let you know which line is 31. Note: I named my button "next" button

package com.example.mers.funfacts2;

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 FunFactsActivity extends Activity {

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

    // Declare our view variables and assign the view from the layout file
    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button nextButton = (Button) findViewById(R.id.nextButton);
        View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // The button was clicked, so update the fact label with new fact
            String fact = "Ostriches can run faster then horses";
            factLabel.setText(fact);

        }
    };
"line#31"    nextButton.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.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);
    }
}
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Things look okay there. Can you paste in the contents of activity_fun_facts.xml? Perhaps an ID or something is wrong in there.

Once again thank you so much for the help Ben i really appreciate it

<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=".FunFactsActivity"
    android:background="@android:color/holo_blue_bright">

    <TextView
        android:text="did you know"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#80ffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ants stretch when they wake up"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textColor="@android:color/white" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textSize="24sp"
        android:background="#fffdffff" />

</RelativeLayout>
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Okay, it looks like your IDs are mismatched. In your Java you are trying to set the Button variable using nextButton as the ID:

Button nextButton = (Button) findViewById(R.id.nextButton);

But in your XML, the Button has the ID of just button:

 <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/button"

To fix it, change R.id.nextButton to R.id.button in the Java line and you should be set. :)

this is the part where i get the error

// Declare our view variables and assign the view from the layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button nextButton;
    nextButton = (Button) findViewById(R.id.button);
    View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // The button was clicked, so update the fact label with new fact
        String fact = "Ostriches can run faster then horses";
        factLabel.setText(fact);

    }
};
    Button.setOnClickListener(listener);

on (Button) findViewById(R.id.button) has a red line under it and also under that where it says Button.setOnClickListener (listener) the word setOnClickListener is red

Ok so i just did that and now i got an error

package com.example.mers.funfacts2;

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 FunFactsActivity extends Activity {

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

    // Declare our view variables and assign the view from the layout file
    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button nextButton;
        Button = (Button) findViewById(R.id.button);
        View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // The button was clicked, so update the fact label with new fact
            String fact = "Ostriches can run faster then horses";
            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.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);
    }

    private class Button {
    }
}
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Sorry, I missed this reply in here. The ID I was referring to was the R.id.button part. You need to change this:

Button nextButton;
Button = (Button) findViewById(R.id.button);

to this:

Button nextButton = (Button) findViewById(R.id.nextButton);

nextButton will be the variable you use in the rest of your code. So you'll need to change this line, for example:

Button.setOnClickListener(listener);

to this instead:

nextButton.setOnClickListener(listener);