-1

Totally newbie here, I try to make simple android app. To countdown time of sleep, with three button "Sleep", "Woke", "Reset". When I run in my phone, it says "Unfortunately, has stopped".

I already run it in debug mode, but it not working too.

here my java code:

package com.example.android.sleepreminder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {

    Button buttonSleep = (Button) findViewById(R.id.sleep);
    Button buttonWoke = (Button) findViewById(R.id.woke);
    Button buttonReset = (Button) findViewById(R.id.reset);
    EditText text1 = (EditText) findViewById(R.id.count_down_timer);
   // private static final String FORMAT = "%02d:%02d:%02d";

    int second =60;

    public CountDownTimer countDownTimer = new CountDownTimer(second * 10000, 1000) {
        @Override
        public void onTick(long millis) {
            text1.setText((int)millis * 1000);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onFinish() {
            text1.setText("reset");
        }

    };

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

        buttonSleep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              countDownTimer.start();

            }});

        buttonWoke.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }});


        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                countDownTimer.onFinish();

            }});


    }

}

and this is my XML files :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText
        android:id="@+id/count_down_timer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="time" />

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <Button
        android:id="@+id/sleep"
        style="@style/Button"
        android:text="Sleep" />

    <Button
        android:id="@+id/woke"
        style="@style/Button"
        android:text="Woke" />

    <Button
        android:id="@+id/reset"
        style="@style/Button"
        android:text="Reset" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

And this is what error that I got in log :

11-27 01:12:16.699 16135-16135/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.android.sleepreminder, PID: 16135
                                                   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.sleepreminder/com.example.android.sleepreminder.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                                                       at android.os.Handler.dispatchMessage(Handler.java:111)
                                                       at android.os.Looper.loop(Looper.java:194)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5624)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
                                                       at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:107)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                                                       at android.app.Activity.findViewById(Activity.java:2102)
                                                       at com.example.android.sleepreminder.MainActivity.<init>(MainActivity.java:14)
                                                       at java.lang.reflect.Constructor.newInstance(Native Method)
                                                       at java.lang.Class.newInstance(Class.java:1606)
                                                       at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
                                                       at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
                                                       at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:360)
                                                       at android.app.Instrumentation.newActivity(<Xposed>)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                       at android.os.Looper.loop(Looper.java:194) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5624) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
                                                       at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:107) 

Can you tell what specific about my problem.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Arie Wijaya
  • 23
  • 2
  • 7

2 Answers2

0

You need to use findViewById after setContentView method or else you will get NPE as the view has not been inflated yet.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonSleep = (Button) findViewById(R.id.sleep);
    Button buttonWoke = (Button) findViewById(R.id.woke);
    Button buttonReset = (Button) findViewById(R.id.reset);
    EditText text1 = (EditText) findViewById(R.id.count_down_timer);

    buttonSleep.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          countDownTimer.start();

        }});

    buttonWoke.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


        }});


    buttonReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countDownTimer.onFinish();

        }});


}
android_Muncher
  • 1,057
  • 7
  • 14
0

Try this. define button in OnCreate activity. You get null pointer exception on text1.setText((int)millis * 1000); so it didn't get text1. So findViewById after setContentView in onCreate.

public class MainActivity extends Activity {

Button buttonSleep;
Button buttonWoke;
Button buttonReset;
EditText text1;
// private static final String FORMAT = "%02d:%02d:%02d";

int second =60;

public CountDownTimer countDownTimer = new CountDownTimer(second * 10000, 1000) {
    @Override
    public void onTick(long millis) {
        text1.setText((int)millis * 1000);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onFinish() {
        text1.setText("reset");
    }

};

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

    buttonSleep = (Button) findViewById(R.id.sleep);
    buttonWoke = (Button) findViewById(R.id.woke);
    buttonReset = (Button) findViewById(R.id.reset);
    text1 = (EditText) findViewById(R.id.count_down_timer);

    buttonSleep.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          countDownTimer.start();

        }});

    buttonWoke.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


        }});


    buttonReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countDownTimer.onFinish();

        }});


  }

 }
Upendra Shah
  • 2,218
  • 17
  • 27