Rick
Rick

Reputation: 73

Introducing onClick function leads to compiler errors

I wanted to make a function that I can link up to a button in Android Studio. But the Android Studio IDE gives me missing) and ; errors and the function can be found when I want to link it to a onclick event

OefenActivity.java:

package com.example.rick2.rekentuin_native;

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

public class OefenenActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    int index = 0;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_oefenen);

    String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
    TextView tv1 = findViewById(R.id.textView6);
    tv1.setText(sessionId);

    int intArray[] = new int[]{1,2,3,4,5,6,7,8,9,10};

    TextView tv2 = findViewById(R.id.textView5);
    tv2.setText(Integer.toString(intArray[index]));

    public void CheckAns(View view) {
        EditText edit = findViewById(R.id.editText4);
        Integer result = Integer.parseInt(edit.getText().toString());
        int number1 = Integer.parseInt(sessionId);

        if(result == intArray[index] + number1) {
            if(index < 10) {
                index++;
                tv2.setText(Integer.toString(intArray[index]));
            }
        }
    }
}
}

The function giving me problems is:

 public void CheckAns(View view) {
    EditText edit = findViewById(R.id.editText4);
    Integer result = Integer.parseInt(edit.getText().toString());
    int number1 = Integer.parseInt(sessionId);

    if(result == intArray[index] + number1) {
        if(index < 10) {
            index++;
            tv2.setText(Integer.toString(intArray[index]));
        }
    }
}

Android Studio gives me the following errors:

')' expected,
';' expected,
cannot resolve symbol 'view',
unexpected token,
variable 'CheckAns' is never used.

android studio warnings

Upvotes: 0

Views: 39

Answers (1)

Salman
Salman

Reputation: 2471

Your function should be inside the class not inside the onCreate method. onCreate itself is a method. So do it like below

package com.example.rick2.rekentuin_native;

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

public class OefenenActivity extends AppCompatActivity {

int index = 0;
int intArray[] = new int[]{1,2,3,4,5,6,7,8,9,10};
TextView tv2;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_oefenen);

    String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
    TextView tv1 = findViewById(R.id.textView6);
    tv1.setText(sessionId);



    tv2 = findViewById(R.id.textView5);
    tv2.setText(Integer.toString(intArray[index]));
}

public void CheckAns(View view) {
        EditText edit = findViewById(R.id.editText4);
        Integer result = Integer.parseInt(edit.getText().toString());
        int number1 = Integer.parseInt(sessionId);

        if (result == intArray[index] + number1) {
            if (index < 10) {
                index++;
                tv2.setText(Integer.toString(intArray[index]));
            }
        }
    }

}

Upvotes: 2

Related Questions