メインアクティビティとは異なるクラスのタイマーを作成しました。コードを実行すると、次のエラーが発生します。'nullのオブジェクト参照でbooleanのandroid.os.Handler.postDelayed(Runnable、long)'を実行しました
java.lang.NullPointerException:ヌルオブジェクトリファレンスで仮想メソッド 'boolean android.os.Handler.postDelayed(java.lang.Runnable、long)'を呼び出そうとしました
それをされて研究することは、それはあるハンドラが私のコードではしかし、初期化されていないことを示唆している(私はmainactivityでそのコードを使用する場合、それは正常に動作スレッド)
誰でもImが間違ってどこになどの任意のポインタを提供することができますか?
主な活動:
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にタイマークラス:
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
感謝としてAppCompatActivity
あなたMainActivity.javaを拡張する必要がいけません。私はあなたのコードを試してみましたが、それはうまくコンパイルされました(文脈の誤字がありましたが、コンテキストに変更してしまいました。 、PID:2561 java.lang.StackOverflowError:スタックサイズ8MB " – Matmajot
http://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror ここに記載されています –