Android 显示片段中的Toast-没有适合makeText的方法
我有以下代码:-Android 显示片段中的Toast-没有适合makeText的方法,android,Android,我有以下代码:- import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; import android.widget.ToggleButton; public class Tab3 extends
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;
public class Tab3 extends android.support.v4.app.Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private ToggleButton toggleButton1, toggleButton2;
private Button btnDisplay;
public void addListenerOnButton(View v) {
toggleButton1 = (ToggleButton) v.findViewById(R.id.toggleButton1);
toggleButton2 = (ToggleButton) v.findViewById(R.id.toggleButton2);
btnDisplay = (Button) v.findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("toggleButton1 : ").append(
toggleButton1.getText());
result.append("\ntoggleButton2 : ").append(
toggleButton2.getText());
Toast.makeText(Tab3.this, result.toString(),
Toast.LENGTH_SHORT).show();
}
});
}
public static Tab3 newInstance(int sectionNumber) {
Tab3 fragment = new Tab3();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab3,container,false);
addListenerOnButton(v);
return v;
}
}
这是一个切换按钮,在选项卡页面中显示一些消息
我在这一部分有红线:-
Toast.makeText(Tab3.this, result.toString(),
Toast.LENGTH_SHORT).show();
出现此错误时:-
错误:32,22错误:找不到适用于的方法
makeTextTab3,字符串,int方法
Toast.makeTextContext、CharSequence、int是不适用的参数
错配;无法将Tab3转换为上下文方法
makeTextContext,int,int不适用于参数不匹配;
无法将Tab3转换为上下文
有人能帮我吗?makeText需要一个上下文作为第一个参数
Tab3是一个片段。片段不是上下文。使用Tab3.this.getContext代替Tab3.this.getContext尝试使用this代替Tab3.this,使用this:getActivity遵循以下步骤:
1.将上下文声明为全局上下文
Context context;
2.在onCreateView中编写以下代码:
context = container.getContext();
3.在需要的地方声明上下文
Toast.makeText(context, result.toString(),
Toast.LENGTH_SHORT).show();
或者只是获取上下文