-1

완전히 여기 안돼서, 나는 간단한 안드로이드 애플 리케이션을 만들려고합니다. "Sleep", "Woke", "Reset"의 세 버튼으로 수면 시간을 카운트 다운합니다. 휴대 전화를 사용하면 "불행히도 멈췄다"고 표시됩니다.카운트 다운 타이머 런타임 오류

이미 디버그 모드로 실행했지만 작동하지 않습니다. 여기

내 자바 코드 :

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(); 

      }}); 


    } 

} 

이 내 XML 파일입니다 :

<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> 

그리고 이것이 내가 로그에있어 그 어떤 오류입니다 :

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)  

을 할 수 내 문제에 대해 구체적으로 이야기하십시오.

답변

0

setContentView 메서드 뒤에 findViewById을 사용해야합니다. 그렇지 않으면보기가 아직 확장되지 않았으므로 NPE가 표시됩니다.

@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(); 

     }}); 


} 
0

시도해보십시오. OnCreate 활동에서 버튼을 정의하십시오. text1.setText((int)millis * 1000);에 null 포인터 예외가 생기므로 text1을 얻지 못했습니다. 따라서 findViewById 이후에 setContentView을 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(); 

     }}); 


    } 

} 
+0

작동합니다! 감사! –

+0

환영합니다 .. 코딩을 계속하십시오. 그리고 답을 얻는다면 받아 들인 응답으로 표시하는 것을 잊지 마세요 :) –