[英]Android String.format() Crash with no reason
I am having a strange crash/error that happened without changing anything. 我遇到了一个奇怪的崩溃/错误,没有更改任何内容。 I am using Volley for network requests and i have a separate class with my URL end points. 我正在使用Volley进行网络请求,并且我的URL端点有一个单独的类。
I am using MY_REQUEST_URL + myparam=%s
as the link and in the volley request i am using String.format(EndPoints.MY_REQUEST_URL, myparameter)
and when I run this request in my app, it crashes (see the log below). 我使用MY_REQUEST_URL + myparam=%s
作为链接,在齐射请求中,我使用String.format(EndPoints.MY_REQUEST_URL, myparameter)
,当我在应用程序中运行此请求时,它崩溃了(请参阅下面的日志)。
What is even stranger is that i am using the same type of requests in the same app and they all work fine except this and another one. 甚至更奇怪的是,我在同一应用程序中使用相同类型的请求,除此请求和另一个请求之外,它们都工作正常。
MyClass code: MyClass代码:
public class MyClass extends Fragment{
// other declarations
ArrayList<JSONObject> data;
View view
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_myclass, null);
// initializing lots of views
data = new ArrayList<JSONObject>();
String userid = Utils.getStringFromPreferences(getActivity(), Utils.VAR_USERID); // this is a custom method of getting from shared preferences, it is static in my custom Utils class
getData(userid);
return view;
}
//different methods
//getData Method
private void getData(String userid) {
String url = String.format(EndPoints.URL_MY_REQUEST_URL, userid);
Log.d("request url debug", url);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Result handling
Log.e("data", response);
if (!response.equals("") || response != null) {
try {
// response handling
} catch (JSONException e) {
// e.printStackTrace();
}
} else {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Something went wrong!");
error.printStackTrace();
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Add the request to the queue - i have a custom class for this, used in every network request that works fine
VolleyRequestQue.getInstance(InsuitBusiness.getContext()).addToRequestQueue(stringRequest);
}
}
This is the Endpoints class: 这是Endpoints类:
public class Endpoints {
public static String MY_MAIN_URL = "https://www.myweb.com/"
public static String MY_REQUEST_URL = MY_MAIN_URL + "scripts/getData.php?userid=%s"
// the rest of the class is all the same like stringname = MY_MAIN_URL + "phpscript.php?parameter=%s"
}
This is the error log: 这是错误日志:
Caused by: java.util.MissingFormatArgumentException: Format specifier '%s'
at java.util.Formatter.format(Formatter.java:2490)
at java.util.Formatter.format(Formatter.java:2426)
at java.lang.String.format(String.java:2626)
at com.myapp.myapp.MyClass.getData(MyClass.java:1220)
at com.myapp.myapp.MyClass$getDataAsync.doInBackground(MyClass.java:1208)
at com.myapp.myapp.MyClass$getDataAsync.doInBackground(MyClass.java:1204)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
MY_MAIN_URL which was supposed to have as value: " https://www.myweb.com/ " i was mistakenly giving another value which contained "%s" and this is how i got the exception and the crash. MY_MAIN_URL应该具有以下值:“ https://www.myweb.com/ ”我错误地给出了另一个包含“%s”的值,这就是我得到异常和崩溃的原因。
For future people having the same problem just make sure there aren't more "%s" in the link, Log the link in the debugger and see how it shows up. 对于以后遇到相同问题的人们,只需确保链接中没有更多的“%s”,将链接记录在调试器中,然后查看其显示方式。
Thanks to everyone for their time. 感谢大家的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.