Arif Hamim
Arif Hamim

Reputation: 59

makeToast can't be resolved in another class

I am trying to get the spinner value in another class from MainActivity via a method call. To check this, I am trying to Toast the input value that should be passed inside the second class. But I'm having an unusual problem for makeToast() method.

I tried to change the parameter of setSelectedMain() with and without Context context both in the function call and function definitions, but still, the same error message appearing. Below is my MainActivity.java code:

package com.gazzali.spinitmeow;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    Spinner spinnerMainChoice;
    Spinner spinnerInputChoice;
    Spinner spinnerOutputChoice;

    EditText getInputValueID;
    double inputValue;

    TextView outputValue;

    Button buttonConvert;

    String selectedMainChoice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* ------------ Main code Starts Here ----------------*/

        /* Main conversion Type choice with Spinner (Drop Down menu)*/
        spinnerMainChoice = findViewById(R.id.spinnerIDMainChoice);
        // [IMPORTANT] Set Spinner Click Listener
        spinnerMainChoice.setOnItemSelectedListener(this);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapterMainChoice = ArrayAdapter.createFromResource(this,
                R.array.MainChoices_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapterMainChoice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinnerMainChoice.setAdapter(adapterMainChoice);

        /* Input Conversion type choice with Spinner */
        spinnerInputChoice = findViewById(R.id.spinnerIDInputChoice);
        spinnerInputChoice.setOnItemSelectedListener(this);

        /* Output Conversion type choice with Spinner */

        spinnerOutputChoice = findViewById(R.id.spinnerIDOutputChoice);
        spinnerOutputChoice.setOnItemSelectedListener(this);

        /* for input and output fields */
        getInputValueID = findViewById(R.id.editTextIDInputValue);
        String inputValueString = getInputValueID.getText().toString();

        /* only if InputValueString is not empty , then proceed */
        if(!TextUtils.isEmpty(inputValueString))
        {
            try
            {
                inputValue = Double.parseDouble(inputValueString);
            }
            catch (Exception e1)
            {
                e1.printStackTrace();
            }
        }
        outputValue = findViewById(R.id.textViewIDOutputValue);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // An item was selected. retrieve the selected item
        selectedMainChoice = parent.getItemAtPosition(pos).toString();
        Log.i("Selected", selectedMainChoice);
        //Toast.makeText(MainActivity.this, "Selected: " + selectedMainChoice, Toast.LENGTH_SHORT).show();

            /* Implement object of spinnerSelects class*/
            spinnerSelects spinnerSelectsInMain = new spinnerSelects(spinnerInputChoice, spinnerOutputChoice);
            /* the main EVIL '(context) this' in  the 2nd parameter, 5 hours wasted, but I learnt many more */
            spinnerSelectsInMain.setInputOutputSpinners(selectedMainChoice, this);

            /* calling test for converter class */
        testOnConverter();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }

    public void testOnConverter(){
        converter converterInMain = new converter();
        converterInMain.setSelectedMain(this, selectedMainChoice);
    }

}

Below is my Converter.java code :

package com.gazzali.spinitmeow;

import android.content.Context;
import android.util.Log;
import android.widget.Spinner;
import android.widget.Toast;

public class converter {
    String selectedMainChoice;
    Spinner spinnerInputChoice, spinnerOutputChoice;


    public void setSelectedMain(Context context, String selectedMainChoiceFromMain) {
        this.selectedMainChoice = selectedMainChoiceFromMain;

        /* Here makeText() method shows error that it can't be Resolved */

        Toast.makeText(this, selectedMainChoice, Toast.LENGTH_SHORT).show();
    }

    public void setInputOutputChoice(Spinner spinnerInputChoiceFromSpinnerSelects, Spinner spinnerOutputChoiceFromSpinnerSelcts){
        this.spinnerInputChoice = spinnerInputChoiceFromSpinnerSelects;
        this.spinnerOutputChoice = spinnerOutputChoiceFromSpinnerSelcts;

    }
}

Upvotes: 1

Views: 94

Answers (2)

majurageerthan
majurageerthan

Reputation: 2319

Change

Toast.makeText(this, selectedMainChoice, Toast.LENGTH_SHORT).show();

to

Toast.makeText(context, selectedMainChoice, Toast.LENGTH_SHORT).show();

Upvotes: 3

A.J
A.J

Reputation: 143

Try This

package com.gazzali.spinitmeow;

import android.content.Context;
import android.util.Log;
import android.widget.Spinner;
import android.widget.Toast;

public class converter {
    String selectedMainChoice;
    Spinner spinnerInputChoice, spinnerOutputChoice;


    public void setSelectedMain(Context context, String selectedMainChoiceFromMain) {
        this.selectedMainChoice = selectedMainChoiceFromMain;

        /* Here makeText() method shows error that it can't be Resolved */

        Toast.makeText(context, selectedMainChoice, Toast.LENGTH_SHORT).show();
    }

    public void setInputOutputChoice(Spinner spinnerInputChoiceFromSpinnerSelects, Spinner spinnerOutputChoiceFromSpinnerSelcts){
        this.spinnerInputChoice = spinnerInputChoiceFromSpinnerSelects;
        this.spinnerOutputChoice = spinnerOutputChoiceFromSpinnerSelcts;

    }
}

Upvotes: 0

Related Questions