当我按下后退按钮(向上按钮)时,在使用截击后显示“应用已停止”

时间:2017-03-27 11:43:01

标签: android android-volley

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);

        but1=(Button)findViewById(R.id.btnlogin);

      //
        final String Token=getIntent().getExtras().getString("token");
//        Toast.makeText(getApplicationContext(),Token, Toast.LENGTH_SHORT).show();
        final String firstName=getIntent().getExtras().getString("firstname");

        //getting cardview data's
        String url = 
        com.android.volley.toolbox.JsonObjectRequest jsonRequest = new com.android.volley.toolbox.JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        // the response is already constructed as a JSONObject!
                        try {

                            JSONArray obj = response.getJSONArray("result");

                            int o = obj.length();
                            Log.v("Length", String.valueOf(o));

                            for (int i = 0; i < obj.length(); i++) {

                                JSONObject jsonObject = obj.getJSONObject(i);
                                //listname.add(jsonObject.getString("templateId"));
                                listehr.add(jsonObject.getString("templateId"));
                                listdate.add(jsonObject.getString("startTime"));
                                listtime.add(jsonObject.getString("category"));
//                                ehrUid.add(jsonObject.getString("ehrUid"));
                                List<String> compositionUid = new ArrayList<>();
//                                String startTime = jsonObject.getString("startTime");
//                                Log.v("startTime",startTime);


                            }

                            Log.v("listtime", String.valueOf(listtime.size()));
                            Log.v("Response", response.toString());
                            String total  = response.getString("total");
                            Log.v("Total",total);
                            String result = response.getString("result");
                            Log.v("Result",result);
                        } catch (JSONException e) {
                            e.printStackTrace();

                        }
                    }

                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        System.out.println(error);
                    }
                }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                headers.put("Authorization",Token);
                return headers;
            }
        };
        com.android.volley.toolbox.Volley.newRequestQueue(NavigationActivity.this).add(jsonRequest);

        //

2 个答案:

答案 0 :(得分:0)

您必须为这两个密钥“token”,“firstname”发送字符串值。

final String Token = getIntent()。getExtras()。getString(“token”);
final String firstName = getIntent()。getExtras()。getString(“firstname”);

如果你发现它无法在另一个类中发送意图的代码片段。

答案 1 :(得分:0)

Punithapriya 这是主要活动的代码

公共类MainActivity扩展了AppCompatActivity {

TextView t;
private EditText username;
private EditText password;
EditText showPsd;
CheckBox mCbShowPwd;
public Button but1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //login page using json
    but1=(Button)findViewById(R.id.btnlogin);
    username=(EditText)findViewById(R.id.editText2);
    password=(EditText)findViewById(R.id.btnpass);
        but1.setOnClickListener(new View.OnClickListener() {
          @Override
            public void onClick(View v) {
              //validation for login
              if(username.getText().toString().length()==0){
                  username.setError("Username not entered");
                  username.requestFocus();
              }
              else if(password.getText().toString().length()==0){
                  password.setError("Password  not entered");
                  password.requestFocus();
              }
              //
              String url = ;

              JsonObjectRequest jsonRequest = new JsonObjectRequest
                      (Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
                          @Override
                          public void onResponse(JSONObject response) {
                              // the response is already constructed as a JSONObject!
                              try {
                                  Log.v("Hai", response.toString());
                                  String firstName  = response.getString("firstName");
                                  Log.v("firstName", firstName);
                                  String organizationList = response.getString("organizationList");
                                  Log.v("organizationList",organizationList);
                                  String token = response.getString("token");
                                  Log.v("token",token);
                                  Toast.makeText(getApplicationContext(),"Logged in Suceessfully", Toast.LENGTH_SHORT).show();
                                  Intent i = new Intent(MainActivity.this,NavigationActivity.class);
                                  i.putExtra("token",token);
                                  i.putExtra("firstname",firstName);
                                  startActivity(i);
                                  finish();


                              } catch (JSONException e) {
                                  e.printStackTrace();

                              }
                          }

                      }, new Response.ErrorListener() {

                          @Override
                          public void onErrorResponse(VolleyError error) {
                              error.printStackTrace();
                              System.out.println(error);
                              Toast.makeText(getApplicationContext(),"Login Error", Toast.LENGTH_SHORT).show();
                          }
                      });
              Volley.newRequestQueue(MainActivity.this).add(jsonRequest);

            }
        });



    //password
    showPsd = (EditText) findViewById(R.id.btnpass);
    // get the show/hide password Checkbox
    mCbShowPwd = (CheckBox) findViewById(R.id.btnshow);

    // add onCheckedListener on checkbox
    // when user clicks on this checkbox, this is the handler.
    mCbShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                                              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                                                  // checkbox status is changed from uncheck to checked.
                                                  if (!isChecked) {
                                                      // show password
                                                      showPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                                                      EditText et = (EditText)findViewById(R.id.btnpass);
                                                      et.setSelection(et.getText().length());
                                                  } else {
                                                      // hide password
                                                      showPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                                                      EditText et = (EditText)findViewById(R.id.btnpass);
                                                      et.setSelection(et.getText().length());
                                                  }
                                              }
                                          });
    //

    //Textview calling



    final TextView regLink=(TextView)findViewById(R.id.register1);
    final TextView forLink=(TextView)findViewById(R.id.forgot);

    //register

    regLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent regIntent=new Intent(MainActivity.this,RegisterActivity.class);
            MainActivity.this.startActivity(regIntent);
        }
    });

    //forgot


    forLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent forIntent=new Intent(MainActivity.this,ForgotActivity.class);
            MainActivity.this.startActivity(forIntent);
        }
    });

}
//exit dialog box
@Override
public void onBackPressed() {

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("EHR Store");
    builder.setIcon(R.drawable.sd);
    builder.setMessage("Do you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();

}     }