Android Build a Simple Android App Basic Android Programming Generating a Random Number

Wejdan Al Mubarky
PRO
Wejdan Al Mubarky
Pro Student 1,451 Points

Emulator says" the app has stopped"

@BenDeitch and when I hit "try again," it says "the app keeps stopping"! this is the error from Android monitor:

------------------------------------
07-05 22:15:14.424 371-371/? I/art: Not late-enabling -Xcheck:jni (already on)

07-05 22:15:14.426 371-371/? W/art: Unexpected CPU variant for X86 using defaults: x86

07-05 22:15:14.808 371-371/? W/System: ClassLoader referenced unknown path: /data/app/com.example.asus.funfacts-2/lib/x86

07-05 22:15:14.815 371-371/? I/InstantRun: starting instant run server: is main process

07-05 22:15:14.875 371-371/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter 

android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, 

android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-
private method in android.graphics.drawable.Drawable
07-05 22:15:14.957 371-371/? D/AndroidRuntime: Shutting down VM

07-05 22:15:14.957 371-371/? E/AndroidRuntime: FATAL EXCEPTION: main

                                               Process: com.example.asus.funfacts, PID: 371

                                               java.lang.RuntimeException: Unable to start activity 

ComponentInfo{com.example.asus.funfacts/com.example.asus.funfacts.FunFactActivity}: 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:2665)

                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)

                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)

                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)

                                                   at android.os.Handler.dispatchMessage(Handler.java:102)

                                                   at android.os.Looper.loop(Looper.java:154)

                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)

                                                   at java.lang.reflect.Method.invoke(Native Method)

                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)

                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

                                                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.example.asus.funfacts.FunFactActivity.onCreate(FunFactActivity.java:25)

                                                   at android.app.Activity.performCreate(Activity.java:6679)

                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)

                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)

                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 

                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java) 

                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 

                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 

                                                   at android.os.Looper.loop(Looper.java:154) 

                                                   at android.app.ActivityThread.main(ActivityThread.java:6119) 

                                                   at java.lang.reflect.Method.invoke(Native Method) 

                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 

                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 
07-05 22:15:15.366 371-378/? I/art: Debugger is no longer active

07-05 22:15:15.366 371-378/? I/art: Starting a blocking GC Instrumentation

4 Answers

Tim Lee
Tim Lee
968 Points

That happens when there is a run time error in your code. I cannot pin point to your error but what I can do is tell you what to do to fix your problem. At the bottom of the screen, pull up the android monitor screen which logs all activities in the app. Through that you can trace back to exactly when your code breaks and why that is happening.

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Wejden!

It looks like you're calling 'setOnClickListener' on a null object reference. The only time we're calling 'setOnClickListener' is on 'mShowFactButton'. So it looks like 'mShowFactButton' is null for some reason.

In your code, you're setting 'mShowFactButton' with this:

mShowFactButton= (Button) findViewById(R.id.ShowFactButton);

Is that the id you've got in the layout file? I've got mine as 'R.id.showFactButton' (lower case s :)

Wejdan Al Mubarky
Wejdan Al Mubarky
Pro Student 1,451 Points

yes I wrote this one :

mShowFactButton= (Button) findViewById(R.id.ShowFactButton);

with capital S by mistake, but I fixed it but it still showing me the same error! this is my code

package com.example.asus.funfacts;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class FunFactActivity extends AppCompatActivity {

    private TextView mFactTextView;
    private Button mShowFactButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_fact);
        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 = "Orchies can run faster than horses";
                mFactTextView.setText(fact);
            }
        };
        mShowFactButton.setOnClickListener(listener);

    }
}
Ben Deitch
Ben Deitch
Treehouse Teacher

Can you post the layout file as well?

Ben Deitch
Ben Deitch
Treehouse Teacher

In your layout file, the Button has an ID of 'button'. So instead of R.id.showFactButton, it should be R.id.button.

Wejdan Al Mubarky
PRO
Wejdan Al Mubarky
Pro Student 1,451 Points

@BenDeitch

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 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:paddingBottom="16dp"

android:paddingTop="16dp"

android:paddingLeft="16dp"

android:paddingRight="16dp"

android:background="#51b46d"

tools:context="com.example.asus.funfacts.FunFactActivity">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Did you know?"
    android:textColor="#80ffffff"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.052"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.032"/>

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:layout_marginBottom="16dp"
    android:background="@android:color/white"
    android:text="show another fact"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintLeft_creator="1"/>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ants stretch when they wake up in the morning "
    android:textColor="@android:color/white"
    android:textSize="24sp"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintBottom_creator="1"
    android:layout_marginStart="9dp"
    app:layout_constraintBottom_toBottomOf="parent"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="9dp"/>

</android.support.constraint.ConstraintLayout>

Wejdan Al Mubarky
PRO
Wejdan Al Mubarky
Pro Student 1,451 Points

@BenDeitch ok I changed the button name and now it's working better! now when I run the app it opens but when I click the button, the app stops. but thank you for your help at least it is opening now.

Ben Deitch
Ben Deitch
Treehouse Teacher

No problem! Also, the new version of Android defaults to using a CoordinatorLayout instead of a RelativeLayout like I use in the video. If you want to just use my layout it's right here:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/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="#51b46d"
    android:padding="50dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Did you know?"
        android:textColor="#80ffffff"
        android:textSize="24sp"/>

    <TextView
        android:id="@+id/factTextView"
        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/white"
        android:textSize="24sp"/>

    <Button
        android:id="@+id/showFactButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:text="Show Another Fun Fact"
        android:textColor="#51b46d"/>

</RelativeLayout>