Ich habe einen Timer in einer anderen Klasse Haupttätigkeit erstellt. Wenn ich den Code ausführen, erhalte ich den folgenden Fehler:‚boolean android.os.Handler.postDelayed (java.lang.Runnable, long)‘ auf einer Null-Objekt Referenz
java.lang.NullPointerException: Versuch, virtuelle Methode 'boolean android.os.Handler.postDelayed (java.lang.Runnable, lang)' auf einem Null-Objekt-Verweis aufzurufen
Researching es legt nahe, dass der Handler jedoch nicht in meinem Code initialisiert wird, ist es (wenn ich diesen Code in der mainactivity verwenden fädeln es funktioniert gut)
Kann jemand bieten alle Hinweise, wo im gehend falsch?
Hauptaktivität:
public class MainActivity extends AppCompatActivity {
private ListView listView;
TextView timertxt; //this
public static TextView hourtext;//this2
int time; //this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timertxt = (TextView) findViewById(R.id.timertxt);//this
hourtext = (TextView) findViewById(R.id.SessionTimerTxt);//this 2
final Timer timer;
final Button generate = (Button) findViewById(R.id.Generate);
generate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseAccess myDatabaseHelper = new DatabaseAccess(MainActivity.this);
myDatabaseHelper.open();
Cursor d = myDatabaseHelper.getRProgram();
if (d.moveToFirst()) ;
DisplayProgram(d);
myDatabaseHelper.close();
Timer timer = new Timer();
timer.count();
//the abpove two lines r causing it to crash prob cos timer class is trying to change text on UI
}
public void DisplayProgram(Cursor d) {
TextView result2 = (TextView) findViewById(R.id.result2);
result2.setText("\n" + d.getString(1)+ " " + (d.getLong(2)*1000));
time = (d.getInt(2)*1000);
TextView dbtime = (TextView) findViewById(R.id.dbtimeproof);
dbtime.setText (String.valueOf (time));
final MyCount counter = new MyCount(time,1000);//this
counter.start();
Button TButton = (Button) findViewById(R.id.tbutton);
TButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//counter.start();
}
});
}
});
}
// count down timer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer { //all below here
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
// it's done
timertxt.setText("Done!");
}
@Override
public void onTick(long millisUntilFinished) {
timertxt.setText("Left: " + millisUntilFinished/1000);
final String ifvaluest = timertxt.getText().toString();
//if hour timer is greater than 1 sec then process below else skip below
if (ifvaluest.equals("Left: 1")){
DatabaseAccess myDatabaseHelper = new DatabaseAccess(MainActivity.this);
myDatabaseHelper.open();
Cursor d = myDatabaseHelper.getRProgram();
if (d.moveToFirst()) ;
DisplayProgram(d);
myDatabaseHelper.close();}}
public void DisplayProgram(Cursor d) {
TextView result2 = (TextView) findViewById(R.id.result2);
result2.setText("\n" + d.getString(1)+ " " + (d.getLong(2)*1000));
time = (d.getInt(2)*1000);
TextView dbtime = (TextView) findViewById(R.id.dbtimeproof);
dbtime.setText (String.valueOf (time));
final MyCount counter = new MyCount(time,1000);//this
counter.start();
}
}
}
Heres die Timer-Klasse:
public class Timer extends AppCompatActivity {
//TextView hourtext;
private int elapsedTime;// used to hold elapsed time
public Handler handler;
private int interval = 60000;//millisecs count
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hourtext = (TextView) findViewById(R.id.SessionTimerTxt);
elapsedTime = 0;
handler = new Handler();//initialise the handler
count();
}//starts the count
public void count() {
runOnUiThread(new Runnable() {
@Override
public void run() {
elapsedTime++; //increase the counter
MainActivity.hourtext.setText(String.valueOf(elapsedTime));
handler.postDelayed(runnable, interval); // call the runnable
}
});
}//ends of the count section
// Runnable - calls the count() function to continue the sequence
private Runnable runnable = new Runnable() {
@Override
public void run() {
count();
}
}; //end runnable section
} //end of class
danke für die Antwort verlängern müssen. Ich habe Ihren Code ausprobiert und es kompiliert (obwohl es einen Tippfehler mit Kontext gab, änderte ich es in einen Kontext und es dauerte. Wenn es im Emulator ausgeführt wurde, stürzte es mit dem folgenden Fehler ab "Process: com.example.mat.shaolinwarriordbimport10 , PID: 2561 java.lang.StackOverflowError: Stapelgröße 8MB " – Matmajot
http://stackoverflow.com/questions/214741/what-is-a-stackoverfloorror Lesen Sie dort –