Note that there are some explanatory texts on larger screens.

plurals
  1. POresolve 'called from wrong thread' exception
    primarykey
    data
    text
    <p>The purpose of my app is: User enters a number and clicks a button. The button uses the input to calculate the Fibonacci sequence with a timer - with each number in the sequence displaying each second to a textView. But when I try to run the timer I get the CalledFromWrongThreadException. I've posted my code below. As you can tell by my log statements I believe I know which line is causing the problem. I think it's because I'm calling a method which is outside my onclicklistener but when I move that other method around I just cause more problems.</p> <p>I've read a couple other posts and I'm not really sure what the proper way is to print to a text area using my method. Does anyone know how I can make this work? </p> <pre><code>public class MainActivity extends Activity { // primary widgets private EditText editText; private TextView textView; private Button button1; static int seconds = 0; static Timer timer; static ArrayList&lt;Integer&gt; fibList = new ArrayList&lt;Integer&gt;(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText1); textView = (TextView) findViewById(R.id.textView2); button1 = (Button) findViewById(R.id.button1); final int delay = 1000; final int period = 1000; timer = new Timer(); //Attempt to clear TextView textView.setText(""); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //Clear Textview String array = " "; fibList.clear(); textView.setText(array); //Log.i("ARRAY", "ATTEMPT to CLEAR"+fibList); String input = editText.getText().toString(); int number = Integer.parseInt(input); int tmp = 0; // confirm input if (number &lt; 20) { Toast.makeText(getApplicationContext(), "You entered: " + number, Toast.LENGTH_LONG).show(); for (int i = 0; i &lt;= number; i++) { fibList.add(fib(i)); // sum even numbers if (fib(i) % 2 == 0) { tmp += fib(i); } } } else { Toast.makeText(getApplicationContext(), "Number is too Large: " + number, Toast.LENGTH_LONG) .show(); } //I believe error occurs in this method Log.i("TEST", "START TIMER"); timer.scheduleAtFixedRate(new TimerTask() { public void run() { Log.i("TEST", "RUN TIMER"); int nextIndex = setInterval(); Log.i("TEST", "SET INTERVAL"); if (nextIndex &lt; fibList.size()) { Log.i("TEST", "TRY TO PRINT"); //It looks like error occurs here when I try to print to textView textView.setText(fibList.get(nextIndex)+ " "); Log.i("TEST", "NEXT INDEX"+fibList.get(nextIndex)); Log.i("TEST", "DID PRINT"); } } }, delay, period); Log.i("TEST", "END TIMER"); } }); } // run fibonacci sequence public static int fib(int n) { if (n &lt; 2) { return n; } else { return fib(n - 1) + fib(n - 2); } } //counts up for every element through the array public static final int setInterval() { if (seconds &gt;= fibList.size()) timer.cancel(); return seconds++; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload