首页 > 解决方案 > makeToast 无法在另一个类中解决

问题描述

我正在尝试通过方法调用从 MainActivity 获取另一个类中的微调器值。为了检查这一点,我试图 Toast 应该在第二个类中传递的输入值。但我在makeToast()方法上有一个不寻常的问题。

我尝试在函数调用和函数定义中更改setSelectedMain()with 和 without的参数,但仍然出现相同的错误消息。Context context下面是我的MainActivity.java代码:

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);
    }

}

下面是我的Converter.java代码:

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;

    }
}

标签: javaandroidandroid-toast

解决方案


改变

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

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

推荐阅读