0

I tried to make a mortgage calc app on my Android Studio.

I really make a class called "Mortgage" on it.

In fact, when I make it on MainActivity.java it not working.

Here is a code:

package com.example.mortgagecalapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    mortgage = new Mortgage();
    setContentView( R.layout.activity_main );
}

@Override
protected void onStart() {
    super.onStart();
    updateView();
}

private void updateView() {
    TextView amountTV = findViewById(R.id.amountNumber_textView);
    amountTV.setText(mortgage.getFormattedAmount());
    TextView yearsTV = findViewById(R.id.yearsNumber_textView);
    yearsTV.setText("" + mortgage.getYears());

}

public void modifyData(View view) {

    Intent myIntenT = new Intent(this, DataActivity.class);
    this.startActivity(myIntenT);
}
}

It said Cannot resolve symbol 'mortgage' , I do not know why it cannot resolve it. I really make mortgage = new Mortgage(); on it. All the mortgage is red.

How to fix it, or I got a bug?

Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33
  • 3
    You never declare `mortgage`. Should it be a local variable? Should it be a field? You need to declare `Mortgage mortgage` in the appropriate scope! – Thomas Kläger Feb 27 '21 at 17:26

1 Answers1

1

Do this it will work.

public class MainActivity extends AppCompatActivity {
  Mortgage mortgage;  // add this line 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mortgage = new Mortgage();
    setContentView(R.layout.activity_main);
  }
Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33
Danish
  • 676
  • 5
  • 10