使用Toast和makeText()方法获取错误
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.Toast;
public class RateME extends Fragment {
public RateME() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_rate_me, container, false);
final RatingBar ratingBar_default = (RatingBar) v.findViewById(R.id.ratingbar_default);
ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
if (rating > 2 )我试过在这里出错
Toast toast = Toast.makeText(RateME.this, "Value of:" + String.valueOf(rating), Toast.LENGTH_LONG).show();也尝试过
Toast toast = new Toast.makeText(RateME.this, "Value of:" + String.valueOf(rating), Toast.LENGTH_LONG).show();
{
Toast.makeText(RateME.this, "Value of:" + String.valueOf(rating), Toast.LENGTH_LONG).show();
}
else
{
//Toast.makeText(RateME.this, "here"+String.valueOf(rating),Toast.LENGTH_SHORT).show();
}
}
});
return v;
}
}这是logcat
错误:(35,23)错误:不适用于makeText( RateME,String,int)方法Toast.makeText( Context,int,int)不适用(实际参数RateME不能通过方法调用转换转换为上下文)方法Toast.makeText( Context,CharSequence,int)不适用(实际参数RateME不能通过方法调用转换转换为上下文)
发布于 2016-03-16 09:41:08
您需要在片段中使用getActivity()而不是RateME.this
Toast.makeText(getActivity(), "Value of:" + String.valueOf(rating), Toast.LENGTH_LONG).show();试着用这条路破片。
发布于 2016-03-16 09:42:10
在使用片段时,请尝试这样做:
Toast.makeText(getActivity(), "Value of:" + String.valueOf(rating), Toast.LENGTH_LONG).show();https://stackoverflow.com/questions/36031759
复制相似问题