使用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无法转换为Context 通过方法调用转换)方法 Toast.makeText(Context,CharSequence,int)不适用(实际 参数RateME无法通过方法调用转换为Context 转化率)
答案 0 :(得分:3)
你需要在片段中使用 getActivity()而不是 RateME.this
Toast.makeText(getActivity(), "Value of:" + String.valueOf(rating), Toast.LENGTH_LONG).show();
在片段中尝试这种方式。
答案 1 :(得分:3)
在使用片段时尝试此操作:
Toast.makeText(getActivity(), "Value of:" + String.valueOf(rating), Toast.LENGTH_LONG).show();