0

I want to write a Fibonacci application using AsyncTask in Android Studio. Although there is no error in my app, when I run it it says: "MyApplication has stopped!". Can anyone point out my mistakes? I have included the java code here. Thanks in advance.

package com.example.asus.myapplication;

public class fibonacci
        extends AppCompatActivity
        implements View.OnClickListener {

    private Button SolveButton;
    private EditText Number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fibonacci);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Number = (EditText) findViewById(R.id.editText);
        Number.setOnClickListener(this);
        SolveButton = (Button) findViewById(R.id.button);
        SolveButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.button: {
                double i =
                        Double.parseDouble(Number.getText().toString());
                new fibonacci_thread().execute(i);
                break;
            }
        }
    }


    public class fibonacci_thread
            extends AsyncTask<Double, Void,
            Double> {
        double s1 = 0;
        double s2 = 1;
        double s3 = 0;

        @Override
        protected Double doInBackground(Double... params) {

            for(double k = 3; k <= params[0]; k++) {
                s3 = s1 + s2;
                s1 = s2;
                s2 = s3;
            }

            return s2;
        }

        @Override
        public void onPostExecute(Double result) {
            EditText textView = (EditText) findViewById(R.id.editText2);
            String yourDoubleString = String.valueOf("Result: " +
                                                             result);
            textView.setText(yourDoubleString);
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }
}

and here is my Event Log during installation of the app on a "Custom Phone - 7.1.0 - API 25 - 768x1280", using Genymotion: 12:14 AM Genymotion: Device [Custom Phone - 7.1.0 - API 25 - 768x1280]: started

12:15 AM Executing tasks: [:app:assembleDebug]

12:15 AM Gradle build finished in 6s 61ms

12:15 AM Instant Run performed a full build and install since the installation on the device does not match the local build on disk. (Don't show again)

12:15 AM Can't bind to local 8623 for debugger

ZARA
  • 1
  • 3

2 Answers2

0

You are probably getting IllegalStateException that leads to a RuntimeException in this case because you are programmatically adding toolbar in your view, while your activity might have decor provided action bar. From here on wards you can apply two approaches:

  1. Use the default action bar and remove the following two lines from your code:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);

  2. First remove the default action bar and then use your own toolbar in its place. You can look here for details.

Iffat Fatima
  • 1,610
  • 2
  • 16
  • 27
  • I checked my styles.xml and I have got this two lines: false true So I suppose my problem in installation is not related to the toolbar. – ZARA May 15 '18 at 06:43
0

After disabling Instant Run from settings, my problem was solved! This way I could install the App on my devices. Thank you buddies for your helps!

ZARA
  • 1
  • 3