makeToast 无法在另一个 class 中解析
makeToast can't be resolved in another class
我正在尝试通过方法调用从 MainActivity 获取另一个 class 中的微调器值。为了检查这一点,我正在尝试 Toast 应该在第二个 class 中传递的输入值。但是我遇到了 makeToast()
方法的异常问题。
我尝试在函数调用和函数定义中使用和不使用 Context context
来更改 setSelectedMain()
的参数,但仍然出现相同的错误消息。下面是我的 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;
}
}
试试这个
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;
}
}
改变
Toast.makeText(this, selectedMainChoice, Toast.LENGTH_SHORT).show();
至
Toast.makeText(context, selectedMainChoice, Toast.LENGTH_SHORT).show();
我正在尝试通过方法调用从 MainActivity 获取另一个 class 中的微调器值。为了检查这一点,我正在尝试 Toast 应该在第二个 class 中传递的输入值。但是我遇到了 makeToast()
方法的异常问题。
我尝试在函数调用和函数定义中使用和不使用 Context context
来更改 setSelectedMain()
的参数,但仍然出现相同的错误消息。下面是我的 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;
}
}
试试这个
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;
}
}
改变
Toast.makeText(this, selectedMainChoice, Toast.LENGTH_SHORT).show();
至
Toast.makeText(context, selectedMainChoice, Toast.LENGTH_SHORT).show();