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 Basic Android Programming Adding the OnClick Method

prateek parasher
PLUS
prateek parasher
Courses Plus Student 445 Points

appnotworking

04-07 21:24:44.048 18995-18995/com.lion.prateek.superfunfacts E/AndroidRuntime: FATAL EXCEPTION: main Process: com.lion.prateek.superfunfacts, PID: 18995 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lion.prateek.superfunfacts/com.lion.prateek.superfunfacts.FunFactsLion}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) at android.app.ActivityThread.access$800(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5052) 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:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.lion.prateek.superfunfacts.FunFactsLion.onCreate(FunFactsLion.java:31) at android.app.Activity.performCreate(Activity.java:5248) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)  at android.app.ActivityThread.access$800(ActivityThread.java:141)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5052)  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:793)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)  at dalvik.system.NativeStart.main(Native Method)  04-07 21:24:44.078 18995-18995/? I/Process: Sending signal. PID: 18995 SIG: 9

Kourosh Raeen
Kourosh Raeen
23,733 Points

Can you post the activity code?

prateek parasher
prateek parasher
Courses Plus Student 445 Points

THIS IS .JAVA

package com.lion.prateek.superfunfacts;

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

public class FunFactsLion extends AppCompatActivity { // Declare our view variables private TextView mFactTextView; private Button mShowFactButton;

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

    mFactTextView = (TextView) findViewById(R.id.factTextView);
    mShowFactButton = (Button) findViewById(R.id.showFactButton);

    View.OnClickListener listener = (new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String fact = "The World's Oldest Dress was found in Egypt and it is 5,000 years old.";
            mFactTextView.setText(fact);

        }
    });
    mShowFactButton.setOnClickListener(listener);
prateek parasher
prateek parasher
Courses Plus Student 445 Points

THIS IS .XML code

<?xml version="1.0" encoding="utf-8"?> <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="com.lion.prateek.superfunfacts.FunFactsLion" android:background="@android:color/holo_purple">

<TextView
    android:text="Did you know ?"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:textStyle="bold|italic"
    android:textSize="24sp"
    android:id="@+id/textView"
    android:background="#80ffffff" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Christianity was the main religion in Egypt between the Fourth and Sixth Centuries"
    android:id="@+id/factTextView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textStyle="bold"
    android:textSize="24sp"
    android:textColor="@android:color/white" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show fact"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:background="@android:color/white"
    android:textColor="@android:color/holo_purple" />

</RelativeLayout>

3 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

There's a problem in the xml with the Button id. It should be showFactButton.

android:id="@+id/showFactButton"
prateek parasher
prateek parasher
Courses Plus Student 445 Points

<?xml version="1.0" encoding="utf-8"?> <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="com.lion.prateek.superfunfacts.FunFactsLion" android:background="@android:color/holo_purple">

<TextView
    android:text="Did you know ?"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:textStyle="bold|italic"
    android:textSize="24sp"
    android:id="@+id/textView"
    android:background="#80ffffff" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Christianity was the main religion in Egypt between the Fourth and Sixth Centuries"
    android:id="@+id/factTextView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textStyle="bold"
    android:textSize="24sp"
    android:textColor="@android:color/white" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show Fact Button"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:background="@android:color/white"
    android:textColor="@android:color/holo_purple" />

</RelativeLayout>

I changed that but still it's not working now

Kourosh Raeen
Kourosh Raeen
23,733 Points

The xml you've posted still shows the id as:

android:id="@+id/button"

Are you sure you changed it?

You have a "Null Pointer Exception" (NPE) at

com.lion.prateek.superfunfacts.FunFactsLion.onCreate(FunFactsLion.java:31)

Whatever your code is in the onCreate method of the FunFactsLion class, on line 31, it's trying to do something with that hasn't been defined. Are your variables and fields initialized? We'd really need to see your code to make a better guess.

For your reference, I looked at the error stack trace and found the only line that wasn't java.lang or android.app or android.os...the line quoted above is obviously your code, and not native system code. Most likely, the problem lies with your code, not the system code. Start there!

Edit: Was writing this while you were posting your code; thanks for that. Which line is line 31 in the java class?

The only thing I see that's somewhat suspicious is this line:

setContentView(R.layout.activity_fun_facts_lion);

Did you give your layout an id of activity_fun_facts_lion? Should it be activity_fun_facts?

prateek parasher
prateek parasher
Courses Plus Student 445 Points

i posted my .java code and .xml can you check and tell me how solve this problem

prateek parasher
PLUS
prateek parasher
Courses Plus Student 445 Points

ohh i got it thank you so much sir

<?xml version="1.0" encoding="utf-8"?> <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="com.lion.prateek.superfunfacts.FunFactsLion" android:background="@android:color/holo_purple">

<TextView
    android:text="Did you know ?"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:textStyle="bold|italic"
    android:textSize="24sp"
    android:id="@+id/textView"
    android:background="#80ffffff" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Christianity was the main religion in Egypt between the Fourth and Sixth Centuries"
    android:id="@+id/factTextView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textStyle="bold"
    android:textSize="24sp"
    android:textColor="@android:color/white" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show Fact Button"
    android:id="@+id/showFactButton"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:background="@android:color/white"
    android:textColor="@android:color/holo_purple" />

</RelativeLayout>