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 Build a Simple Android App with Java Creating the Screen Layout Getting to Know Our Tools

the android emulator error

the android emulator wont wont open up my project codes stating "Unfortunately, fun fact has stopped" hence funfact is my taski am working on

3 Answers

Hi Hamza,

Can you paste in the output from the Logcat - somewhere in there will be the logs of what caused your app to fail.

Steve.

this is the error that appears in the logcat, could you support me on this?
02-10 20:01:31.843 3974-3974/com.hamza.funfact E/AndroidRuntime: FATAL EXCEPTION: main Process: com.hamza.funfact, PID: 3974 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hamza.funfact/com.hamza.funfact.funfactsactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.hamza.funfact.funfactsactivity.onCreate(funfactsactivity.java:32) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread

and this is my code:

package com.hamza.funfact;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class funfactsactivity extends AppCompatActivity {
// Declare our view varaiabels
    private TextView facttextview;
    private Button showfactbuttom;

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

   // assign t he views from layout files to the corresponding variables
        facttextview = findViewById(R.id.facttextview);

    showfactbuttom= findViewById(R.id.showfactbutton);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String fact="ostriches run faster than horses";
                facttextview.setText(fact);

            }
        };
    showfactbuttom.setOnClickListener(listener);
    }
}

Got that - I need your XML too, please.

Thanks,

Steve.

I need the layout file, not the manifest.

Hi Hamza,

Right, you have a NullPointerException at line 32 of your funfactsactivity file. Can you post your code? I'll need that activity code and the XML layout file too as the problem is relating to one of your buttons.

Steve.

this is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hamza.funfact">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".funfactsactivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Could you paste in the layout file where you defined your TextView and Button in the XML.

this is the layout file as per your request

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_bright" android:padding="50dp" tools:context="com.hamza.funfact.funfactsactivity">

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_bright"
    android:text="Did You Know!"
    android:textColor="#b0ffffff"
    android:textColorLink="#c8ffffff"
    android:textSize="24sp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:text="Ants stretch when they wake up in the morning!"
    android:textColor="@android:color/background_light"
    android:textSize="24sp" />

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@android:color/holo_blue_dark"
    android:text="Another Fun Fact!"
    android:textColor="@android:color/background_light" />

</RelativeLayout>

Good - that's where your error is.

In your code you are trying to link the layout to the code using:

showfactbuttom= findViewById(R.id.showfactbutton);

That's fine, but your button isn't called showfactbutton - it is just called button:

<Button
    android:id="@+id/button"

So, change the above Java code to:

showfactbuttom= findViewById(R.id.button);

That should fix the problem.

Steve.

not all hero's wear cape, thanks a billion Steve much appropriated :D

No problem! :+1:

greetings Steve, one more thing, on the onclick view statement once i click the button on the app, it doesn't change and closes the on its own, is there away to fix this? would appropriate it

Sorry for the slow response; I have been out with friends; Saturday night! Home now.

What is your app doing when you load it up. Does the 'fact' not appear when you hit the button?

Steve.

Greetings Steve! sorry for the delay been busy, its great to take some time to have some fun!, and no worries its handled, the id of factTextview = (TextView) findViewById(R.id.facttextView); isnt correct and should be (R.id.textView) as what i have in the xml layer folder, thanks anyway :D