Guest User

Untitled

a guest
Apr 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. Process: com.example.dhruv.bill, PID: 2879
  2. java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.dhruv.bill.AppController.addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference
  3. at com.example.dhruv.bill.LoginActivity.checkLogin(LoginActivity.java:189)
  4. at com.example.dhruv.bill.LoginActivity.access$200(LoginActivity.java:33)
  5. at com.example.dhruv.bill.LoginActivity$1.onClick(LoginActivity.java:83)
  6. at android.view.View.performClick(View.java:5052)
  7. at android.view.View$PerformClick.run(View.java:20162)
  8. at android.os.Handler.handleCallback(Handler.java:739)
  9. at android.os.Handler.dispatchMessage(Handler.java:95)
  10. at android.os.Looper.loop(Looper.java:135)
  11. at android.app.ActivityThread.main(ActivityThread.java:5753)
  12. at java.lang.reflect.Method.invoke(Native Method)
  13. at java.lang.reflect.Method.invoke(Method.java:372)
  14. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
  15. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
  16.  
  17. package com.example.dhruv.bill;
  18.  
  19. import android.app.Activity;
  20. import android.app.ProgressDialog;
  21. import android.content.Intent;
  22. import android.os.Bundle;
  23. import android.util.Log;
  24. import android.view.View;
  25. import android.widget.Button;
  26. import android.widget.EditText;
  27. import android.widget.Toast;
  28. import com.example.dhruv.bill.R;
  29. import com.example.dhruv.bill.AppConfig;
  30. import com.example.dhruv.bill.AppController;
  31. import com.example.dhruv.bill.SQLiteHandler;
  32. import com.example.dhruv.bill.SessionManager;
  33. import com.android.volley.Request.Method;
  34. import com.android.volley.Response;
  35. import com.android.volley.VolleyError;
  36. import com.android.volley.toolbox.StringRequest;
  37.  
  38. import org.json.JSONException;
  39. import org.json.JSONObject;
  40.  
  41. import java.util.HashMap;
  42. import java.util.Map;
  43.  
  44. public class LoginActivity extends Activity {
  45. private static final String TAG = RegisterActivity.class.getSimpleName();
  46. private Button btnLogin;
  47. private Button btnLinkToRegister;
  48. private EditText inputEmail;
  49. private EditText inputPassword;
  50. private ProgressDialog pDialog;
  51. private SessionManager session;
  52. private SQLiteHandler db;
  53.  
  54. @Override
  55. public void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_login);
  58.  
  59. inputEmail = (EditText) findViewById(R.id.email);
  60. inputPassword = (EditText) findViewById(R.id.password);
  61. btnLogin = (Button) findViewById(R.id.btnLogin);
  62. btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
  63.  
  64. // Progress dialog
  65. pDialog = new ProgressDialog(this);
  66. pDialog.setCancelable(false);
  67.  
  68. // SQLite database handler
  69. db = new SQLiteHandler(getApplicationContext());
  70.  
  71. // Session manager
  72. session = new SessionManager(getApplicationContext());
  73.  
  74. // Check if user is already logged in or not
  75. if (session.isLoggedIn()) {
  76. // User is already logged in. Take him to main activity
  77. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  78. startActivity(intent);
  79. finish();
  80. }
  81.  
  82. // Login button Click Event
  83. btnLogin.setOnClickListener(new View.OnClickListener() {
  84.  
  85. public void onClick(View view) {
  86. String email = inputEmail.getText().toString().trim();
  87. String password = inputPassword.getText().toString().trim();
  88.  
  89. // Check for empty data in the form
  90. if (!email.isEmpty() && !password.isEmpty()) {
  91. // login user
  92. checkLogin(email, password);
  93. } else {
  94. // Prompt user to enter credentials
  95. Toast.makeText(getApplicationContext(),
  96. "Please enter the credentials!", Toast.LENGTH_LONG)
  97. .show();
  98. }
  99. }
  100.  
  101. });
  102.  
  103. // Link to Register Screen
  104. btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
  105.  
  106. public void onClick(View view) {
  107. Intent i = new Intent(getApplicationContext(),
  108. RegisterActivity.class);
  109. startActivity(i);
  110. finish();
  111. }
  112. });
  113.  
  114. }
  115.  
  116. /**
  117. * function to verify login details in mysql db
  118. * */
  119. private void checkLogin(final String email, final String password) {
  120. // Tag used to cancel the request
  121. String tag_string_req = "req_login";
  122.  
  123. pDialog.setMessage("Logging in ...");
  124. showDialog();
  125.  
  126. StringRequest strReq = new StringRequest(Method.POST,
  127. AppConfig.URL_LOGIN, new Response.Listener<String>() {
  128.  
  129. @Override
  130. public void onResponse(String response) {
  131. Log.d(TAG, "Login Response: " + response.toString());
  132. hideDialog();
  133.  
  134. try {
  135. JSONObject jObj = new JSONObject(response);
  136. boolean error = jObj.getBoolean("error");
  137.  
  138. // Check for error node in json
  139. if (!error) {
  140. // user successfully logged in
  141. // Create login session
  142. session.setLogin(true);
  143.  
  144. // Now store the user in SQLite
  145. String uid = jObj.getString("uid");
  146.  
  147. JSONObject user = jObj.getJSONObject("user");
  148. String name = user.getString("name");
  149. String email = user.getString("email");
  150. String created_at = user
  151. .getString("created_at");
  152.  
  153. // Inserting row in users table
  154. db.addUser(name, email, uid, created_at);
  155.  
  156. // Launch main activity
  157. Intent intent = new Intent(LoginActivity.this,
  158. MainActivity.class);
  159. startActivity(intent);
  160. finish();
  161. } else {
  162. // Error in login. Get the error message
  163. String errorMsg = jObj.getString("error_msg");
  164. Toast.makeText(getApplicationContext(),
  165. errorMsg, Toast.LENGTH_LONG).show();
  166. }
  167. } catch (JSONException e) {
  168. // JSON error
  169. e.printStackTrace();
  170. Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  171. }
  172.  
  173. }
  174. }, new Response.ErrorListener() {
  175.  
  176. @Override
  177. public void onErrorResponse(VolleyError error) {
  178. Log.e(TAG, "Login Error: " + error.getMessage());
  179. Toast.makeText(getApplicationContext(),
  180. error.getMessage(), Toast.LENGTH_LONG).show();
  181. hideDialog();
  182. }
  183. }) {
  184.  
  185. @Override
  186. protected Map<String, String> getParams() {
  187. // Posting parameters to login url
  188. Map<String, String> params = new HashMap<String, String>();
  189. params.put("email", email);
  190. params.put("password", password);
  191.  
  192. return params;
  193. }
  194.  
  195. };
  196.  
  197. // Adding request to request queue
  198. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  199. }
  200.  
  201. private void showDialog() {
  202. if (!pDialog.isShowing())
  203. pDialog.show();
  204. }
  205.  
  206. private void hideDialog() {
  207. if (pDialog.isShowing())
  208. pDialog.dismiss();
  209. }
  210.  
  211. package com.example.dhruv.bill;
  212.  
  213. import android.app.Application;
  214. import android.text.TextUtils;
  215. import com.android.volley.Request;
  216. import com.android.volley.RequestQueue;
  217. import com.android.volley.toolbox.Volley;
  218.  
  219. public class AppController extends Application {
  220.  
  221. public static final String TAG = AppController.class.getSimpleName();
  222.  
  223. private RequestQueue mRequestQueue;
  224.  
  225. private static AppController mInstance;
  226.  
  227. @Override
  228. public void onCreate() {
  229. super.onCreate();
  230. mInstance = this;
  231. }
  232.  
  233. public static synchronized AppController getInstance() {
  234. return mInstance;
  235. }
  236.  
  237. public RequestQueue getRequestQueue() {
  238. if (mRequestQueue == null) {
  239. mRequestQueue = Volley.newRequestQueue(getApplicationContext());
  240. }
  241.  
  242. return mRequestQueue;
  243. }
  244.  
  245. public <T> void addToRequestQueue(Request<T> req, String tag) {
  246. req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
  247. getRequestQueue().add(req);
  248. }
  249.  
  250. public <T> void addToRequestQueue(Request<T> req) {
  251. req.setTag(TAG);
  252. getRequestQueue().add(req);
  253. }
  254.  
  255. public void cancelPendingRequests(Object tag) {
  256. if (mRequestQueue != null) {
  257. mRequestQueue.cancelAll(tag);
  258. }
  259. }
Add Comment
Please, Sign In to add comment