Timer
クラスとTimerTask
クラスを使用して簡単なタイマーを作成しようとしています。次のエラーが表示されます。
Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'Android.view.View Android.view.Window.findViewById(int)' on a null object reference
ここに私のコードがあります:
package com.austinheitmann.stopwatch;
import Android.os.CountDownTimer;
import Android.os.SystemClock;
import Android.support.v7.app.ActionBarActivity;
import Android.os.Bundle;
import Android.util.Log;
import Android.view.Menu;
import Android.view.MenuItem;
import Android.view.View;
import Android.widget.TextView;
import Java.util.Timer;
import Java.util.TimerTask;
public class MainActivity extends ActionBarActivity {
TextView timerf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerf = (TextView) findViewById(R.id.timer);
}
int i=0;
TimerTask time = new TimerTask() {
public void run() {
i++;
int k = i/60;
int j = i%60;
if (j>9) {
timerf.setText("seconds remaining: " + k + ":" + j);
}else {
timerf.setText("seconds remaining: " + k + ":0" + j);
}
Log.d("Uhh", "Sec:" + i);
}
};
Timer timers = new Timer();
public void startB(View view) {
timers.schedule(time, 100000000, 1000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
XMLファイル(レイアウト)は次のとおりです。
<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=".MainActivity">
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:textAppearance="?android:attr/textAppearanceLarge"
Android:text="0:00"
Android:id="@+id/timer"
Android:layout_alignParentTop="true"
Android:layout_centerHorizontal="true"
Android:layout_marginTop="54dp" />
<Button
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Start/Pause"
Android:id="@+id/start"
Android:layout_below="@+id/timer"
Android:layout_alignParentLeft="true"
Android:layout_alignParentStart="true"
Android:layout_marginTop="119dp"
Android:onClick="startB"/>
<Button
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Stop/Reset"
Android:id="@+id/stop"
Android:layout_alignBottom="@+id/start"
Android:layout_alignParentRight="true"
Android:layout_alignParentEnd="true" />
</RelativeLayout>
このエラーに対して私が見たすべての解決策はレイアウトに関係していますが、とにかくそれを変更していません。 startBクラスのTimerTask
にも移動しましたが、同じエラーが発生します。このエラーを修正するにはどうすればよいですか?
Logcatをクリアした後の1回の実行で得られるエラー全体を以下に示します(エラーが発生した場所に**を付けます)。
01-16 12:27:21.008 2455-2455/com.austinheitmann.stopwatch I/art﹕ Not late-enabling -Xcheck:jni (already on)
01-16 12:27:21.119 2455-2455/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
01-16 12:27:21.123 2455-2455/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.austinheitmann.stopwatch, PID: 2455
Java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.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:2209)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
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:2071)
at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.Java:25)
at Java.lang.reflect.Constructor.newInstance(Native Method)
at Java.lang.Class.newInstance(Class.Java:1572)
at Android.app.Instrumentation.newActivity(Instrumentation.Java:1065)
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2199)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
01-16 12:44:57.778 2582-2582/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM
01-16 12:44:57.778 2582-2582/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.austinheitmann.stopwatch, PID: 2582
Java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.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:2209)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
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:2071)
at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.Java:25)
at Java.lang.reflect.Constructor.newInstance(Native Method)
at Java.lang.Class.newInstance(Class.Java:1572)
at Android.app.Instrumentation.newActivity(Instrumentation.Java:1065)
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2199)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
01-16 12:46:25.059 2641-2641/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM
01-16 12:46:25.059 2641-2641/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.austinheitmann.stopwatch, PID: 2641
Java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.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:2209)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
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:2071)
at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.Java:25)
at Java.lang.reflect.Constructor.newInstance(Native Method)
at Java.lang.Class.newInstance(Class.Java:1572)
at Android.app.Instrumentation.newActivity(Instrumentation.Java:1065)
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2199)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
01-16 12:47:50.630 2702-2702/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM
01-16 12:47:50.630 2702-2702/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.austinheitmann.stopwatch, PID: 2702
Java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.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:2209)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
**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:2071)
at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.Java:25)
at Java.lang.reflect.Constructor.newInstance(Native Method)
at Java.lang.Class.newInstance(Class.Java:1572)
at Android.app.Instrumentation.newActivity(Instrumentation.Java:1065)
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2199)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
アクティビティのコンテキストが準備される前にfindViewById
を試みています。
他のメソッドでもtimerfにアクセスする場合は、クラスメンバーを作成しますが、onCreate
の後にsetContentView
で初期化を行います。
TextView timerf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerf = (TextView) findViewById(R.id.timer);
}
更新:
インスタンス化している他のクラスメンバー変数に気付きませんでした。それらはnullのtimerfを参照しています。
それらもonCreate
で初期化する必要があります。動作するはずの完全なコードは次のとおりです。
package com.austinheitmann.stopwatch;
import Android.os.CountDownTimer;
import Android.os.SystemClock;
import Android.support.v7.app.ActionBarActivity;
import Android.os.Bundle;
import Android.util.Log;
import Android.view.Menu;
import Android.view.MenuItem;
import Android.view.View;
import Android.widget.TextView;
import Java.util.Timer;
import Java.util.TimerTask;
public class MainActivity extends ActionBarActivity {
Timer timers = new Timer();
TextView timerf;
TimerTask time;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerf = (TextView) findViewById(R.id.timer);
time = new TimerTask() {
public void run() {
i++;
int k = i/60;
int j = i%60;
if (j>9) {
timerf.setText("seconds remaining: " + k + ":" + j);
}else {
timerf.setText("seconds remaining: " + k + ":0" + j);
}
Log.d("Uhh", "Sec:" + i);
}
};
}
public void startB(View view) {
timers.schedule(time, 100000000, 1000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}