J'essaie de créer un minuteur simple en utilisant les classes Timer
et TimerTask
. Je reçois toujours l'erreur suivante:
Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'Android.view.View Android.view.Window.findViewById(int)' on a null object reference
Voici mon code:
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);
}
}
}
Voici le fichier xml (layout):
<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>
Chaque solution que j'ai vue à cette erreur a à voir avec la mise en page, mais je ne modifie pas cela de toute façon. J'ai également déplacé vers le TimerTask
dans la classe startB et j'obtiens la même erreur. Comment puis-je corriger cette erreur?
Voici l'erreur complète que j'obtiens d'une exécution après avoir effacé le logcat (je mets ** là où l'erreur se produit):
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)
Vous essayez de findViewById
avant que le contexte de l'activité ne soit prêt.
Si vous souhaitez également accéder à timerf dans d'autres méthodes, créez un membre de classe mais effectuez l'initialisation dans onCreate
après setContentView
:
TextView timerf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerf = (TextView) findViewById(R.id.timer);
}
Mise à jour:
Je n'ai pas remarqué les autres variables de membre de classe que vous instanciez. Ils font référence à timerf qui est nul.
Vous devez également les initialiser dans le onCreate
. Voici le code complet qui devrait fonctionner:
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);
}
}