Hellz Yeahh Hellz Yeahh - 5 months ago 17x
Java Question

Why I keep getting "Unfortunately, <App> has stopped" on my app? (java, android studio)

I just wanted to make an app that calculates the percentage of a value, but I never succeeded. I kept trying different methods and different codes but none worked, but so far this is most legit looking one I've made. Anyway, when I compile the app and debug it, it has no error at all, but when I launched the app and try to click "calculate" the app instantly stops working and exits, and there is error always tells me "expected '('" on the second closing bracket and "expected';'" on the third closing bracket. can anybody see the problem with my code? I am newbie please help. Thank you, your help will be greatly appreciated.

Here is the java code:
package com.example.alexm.calc;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{

protected void onCreate(Bundle savedInstanceState) {

final EditText percentedit = (EditText) findViewById(R.id.percentedit);
final EditText valueedit = (EditText) findViewById(R.id.valueedit);
Button button = (Button) findViewById(R.id.button);
final EditText result = (EditText) findViewById(R.id.result);

assert button != null;
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){

int cal1, cal2, cal3, anser;
String edit1 = percentedit.getText().toString();
String edit2 = valueedit.getText().toString();
int val1 = Integer.parseInt(edit1);
int val2 = Integer.parseInt(edit2);
cal1 = val1 * val2;
cal2 = 100 * 1;
cal3 = cal1/cal2;
anser = cal3;




here is the error

Error:(52, 10) error: ')' expected
Error:(54, 2) error: reached end of file while parsing
:app:compileDebugJavaWithJavac FAILED
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.


Try this:

result.setText("" + anser);

setText() method do not accept int and that's probably why you are getting an error.

Btw, why do you have this line: cal2 = 100 * 1;?