Volley字符串请求POST方法无法使用dialogfragment

时间:2016-01-02 11:57:59

标签: android dialog android-volley

我正在尝试使用排球库的字符串请求向服务器发送一些数据。它根据我在活动中的需要而工作。但我需要在对话框片段中使用相同的请求并失败。我错过了什么?下面是在活动中工作的代码

private void ringtoneDownloads() {
    String android_id = Settings.Secure.getString(AppController.getContext().getContentResolver(),Settings.Secure.ANDROID_ID);
    final String device_identifier = android_id;
    final String song_id = Integer.toString(mSongId);
    final String song_locale_id = Integer.toString(mSongLocaleId);
    Log.d(TAG,android_id);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(SongPlayerActivity.this, response, Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(SongPlayerActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put(KEY_DEVICE_IDENTIFIER,device_identifier);
            params.put(KEY_SONG_ID,song_id);
            params.put(KEY_SONG_LOCALE_ID, song_locale_id);
            return params;
        }

    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

对话框片段我改变了

RequestQueue requestQueue = Volley.newRequestQueue(this);

以下

RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

当我从对话框片段

运行代码时,我收到以下错误日志
ple.tsf.myfirstapp E/AndroidRuntime: Process: com.example.tsf.myfirstapp, PID: 21641
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at android.widget.Toast.<init>(Toast.java:132)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at android.widget.Toast.makeText(Toast.java:434)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at com.example.tsf.myfirstapp.fragments.CrbtSubscribeDialogFragment$3.onResponse(CrbtSubscribeDialogFragment.java:229)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at com.example.tsf.myfirstapp.fragments.CrbtSubscribeDialogFragment$3.onResponse(CrbtSubscribeDialogFragment.java:226)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
01-02 14:40:48.581 21641-21641/com.example.tsf.myfirstapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
01-02 14:40:51.521 21641-21641/com.example.tsf.myfirstapp I/Process: Sending signal. PID: 21641 SIG: 9

0 个答案:

没有答案