我在與主活動不同的類中創建了一個計時器。當我運行代碼時,出現以下錯誤:'空對象引用'布爾android.os.Handler.postDelayed(java.lang.Runnable,long)'
java.lang.NullPointerException:試圖對null對象引用調用虛擬方法'boolean android.os.Handler.postDelayed(java.lang.Runnable,long)'
研究它是表明處理器沒有在我的代碼但是初始化它(如果我使用在mainActivity代碼線程,它工作正常)
誰能提供任何指針在何處,我要去錯了嗎?
主要活動:
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();
}
}
}
繼承人的Timer類:
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
感謝您的答覆。我嘗試了你的代碼,它編譯得很好(雖然有一個上下文的錯字,我將它改爲上下文,但它在模擬器中運行時崩潰,出現以下錯誤「Process:com.example.mat.shaolinwarriordbimport10 ,PID:2561 java.lang.StackOverflowError:stack size 8MB「 – Matmajot
http://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror 閱讀此處 –