单击按钮时应用程序崩溃

时间:2015-03-18 17:09:40

标签: java android

我尝试在按钮上执行操作,但点击后出现错误,就像这样。 在哪部分我的代码错误?

enter image description here

public class SyncDataFragment extends Fragment {
    View view;
    Activity act;
    public static final String TAG = SyncDataFragment.class.getSimpleName();

    private Button btnUpload;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

            view = inflater.inflate(R.layout.layout_sync_server, null);

            btnUpload = (Button) view.findViewById(R.id.btnSyncToServer);

            btnUpload.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            submitData();
                    }
            });

            return view;

    }

    public void submitData() {

            final String urlPath = "http://www.mylink.co.id/insert";
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("nama", "Bertho");
            params.put("alamat", "Palmerah");
            params.put("tanggal", "2015-03-18 11:34:00");

            JsonObjectRequest req = new JsonObjectRequest(urlPath, new JSONObject(params),
                   new Response.Listener<JSONObject>() {
                       @Override
                       public void onResponse(JSONObject response) {
                           try {
                               VolleyLog.v("Response:%n %s", response.toString(4));
                               Toast.makeText(act, response.toString(4), Toast.LENGTH_LONG).show();
                           } catch (JSONException e) {
                               e.printStackTrace();
                               Toast.makeText(act, "Error...Masuk catch", Toast.LENGTH_LONG).show();
                           }
                       }
                   }, new Response.ErrorListener() {
                       @Override
                       public void onErrorResponse(VolleyError error) {
                           VolleyLog.e("Error: ", error.getMessage());
                           Toast.makeText(act, error.getMessage(), Toast.LENGTH_LONG).show();
                       }
                   });

            // add the request object to the queue to be executed
            AppController.getInstance().addToRequestQueue(req);
            }
    }

0 个答案:

没有答案