【问题标题】:Android String.format() Crash with no reasonAndroid String.format() 无故崩溃
【发布时间】:2018-01-13 15:27:12
【问题描述】:

我遇到了一个奇怪的崩溃/错误,没有改变任何东西。 我正在使用 Volley 进行网络请求,并且我有一个带有我的 URL 端点的单独类。

我使用MY_REQUEST_URL + myparam=%s 作为链接,在截击请求中我使用String.format(EndPoints.MY_REQUEST_URL, myparameter),当我在我的应用程序中运行此请求时,它会崩溃(请参阅下面的日志)。

更奇怪的是,我在同一个应用程序中使用相同类型的请求,除了这个和另一个之外,它们都可以正常工作。

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);
} 

}

这是 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"

}

这是错误日志:

 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)

【问题讨论】:

  • 请帮帮我,我不明白这是怎么回事
  • 你能粘贴你的代码sn-p吗?因为看起来你的MY_REQUEST_URL 没有%s
  • @Napster 请再次检查编辑,我拥有并且拥有与他们工作的完全相同的方法,现在除了这两个之外,他们中的许多人现在都可以工作。我没有改变任何东西,我只是运行应用程序,它突然出现,我尝试使缓存无效并重新启动,但仍然是同样的问题
  • MY_MAIN URL 是粘贴代码的错字吗?应该是MY_MAIN_URL
  • 你可以从MyClass.java 发布实际代码吗,因为你刚刚发布的内容很好

标签: java android string format android-volley


【解决方案1】:

MY_MAIN_URL 应该具有值:“https://www.myweb.com/”我错误地给出了另一个包含“%s”的值,这就是我得到异常和崩溃的原因。

对于以后遇到相同问题的人,只需确保链接中没有更多的“%s”,在调试器中记录链接并查看它是如何显示的。

感谢大家的宝贵时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-08
    • 2011-08-02
    • 2014-03-12
    • 2019-07-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多