我在gettingToast
上遇到了问题
public class LiveMatch extends Fragment {
private List<Items_list> matches;
private static final String URL="https://www.cricbuzz.com/match-api/livematches.json";
private String recent_match;
View v;
public LiveMatch() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.live, container, false);
return v;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
matches = new ArrayList<>();
final StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray live_match = new JSONArray(response);
Toast.makeText(v.getContext(),live_match.length(),Toast.LENGTH_LONG).show();
for (int i = 0; i < live_match.length(); i++) {
JSONObject object = live_match.getJSONObject(i);
recent_match = object.getString("mathes");
RecyclerView recyclerView = v.findViewById(R.id.recycl);
RecylerAddapter recylerAddapter = new RecylerAddapter(getContext(), matches);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(recylerAddapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(v.getContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(Objects.requireNonNull(getContext())).add(stringRequest);
}
}
问题在于解析JSON响应。
Web服务(API)响应来自JSONObject,您在JSONArray中处理它。 看看Livestream URL的回应
纠正处理响应的代码,它会起作用。
请注意,您必须通过使用Iterator迭代循环来获取值,因为matches对象包含JSONObjects。
更新您的onResponse方法如下:
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
Log.e("Response: ", jsonObject.toString());
JSONObject objMatch = jsonObject.optJSONObject("matches");
Iterator<String> iterator = objMatch.keys();
int counter = 0;
while (iterator.hasNext()) {
String key = iterator.next();
try {
JSONObject value = objMatch.getJSONObject(key);
Log.e("Value: ", value.toString());
// Here you have to add values in Items_list and then after add it in matches list
counter++;
} catch (JSONException e) {
// Something went wrong!
}
}
Toast.makeText(getActivity(), "Total Records: " + counter, Toast.LENGTH_LONG).show();
// After competition of iterator loop, you have to set that List in RecyclerView Adapter here
} catch (JSONException e) {
e.printStackTrace();
}
}
Toast只能在looper线程上显示,例如主线程。我的猜测是Volley在工作线程上运行的回调。
从主线程中显示你的祝酒词:
getActivity().runOnUiThread({
Toast.makeText(...).show()
})
我正在使用Java 8 lambda表示法,但你可以将它改编为Runnable
。
话虽如此,也阅读所有其他答案。
你必须在Fragments中使用getActivity()
或getContext()
来获取默认上下文并使用它。所以干净的v.getContext()
并称之为上述方法之一
Toast makeText
方法重载,可以通过资源ID加载
makeText(Context context,int resId,int duration)
或传递文本显示
makeText(上下文上下文,CharSequence文本,int duration)
如果您使用整数加载它live_match.length()
Android尝试加载所需的资源但无法找到它,因此不会显示Toast。要达到目标,您必须将该整数转换为字符串(实际上为CharSequence),如评论中所述。
Toast.makeText(getActivity(), String.valueOf(live_match.length()), Toast.LENGTH_LONG).show();
如果这不起作用,请确保您可以从那里触摸主线程,我认为您正在后台线程中加载Toast,其中不允许触摸用户界面
Toast只接受CharSequence“String”。将您的值转换为字符串,您将能够看到吐司。 String.Valueof(你的价值)。 live_match.length()可能是一个Integer。在Fragment中尝试getActivity()
public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
return makeText(context, null, text, duration);
}