java - 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;
}
}
解决方案
改变
Toast.makeText(this, selectedMainChoice, Toast.LENGTH_SHORT).show();
至
Toast.makeText(context, selectedMainChoice, Toast.LENGTH_SHORT).show();
推荐阅读
- java - 双向单机不更新外键
- ruby - 与哈希值一样,哈希键是否有指定的枚举顺序?
- python - 证书验证失败:无法获取本地颁发者证书
- amazon-web-services - 有没有办法在 Amazon SES 上指定默认配置集?
- python - 列表对象没有属性副本
- python - Python:基于 MediaInfo 的视频重命名
- asp.net - Bootstrap Modal Popup Not Clearing old value ASP.NET
- c - 用指针打印字符的问题
- linux-kernel - 通过流式 DMA 映射对主存储器进行非相干访问
- python - 不知道如何为 Naoqi python-sdk 设置 PYTHONPATH 变量