javaandroidweb-servicesrestandroid-webservice

Application was crashing after successful Login through android emulator


Logcat imageI am using my web application endpoint to create a native login android application. But while attempting to login I can see the login successful result on console log.But I want that activity to redirected to another activity. Below I am posting my

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
  <manifest 
   xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.reception.farbinder_test">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission 
 android:name="android.permission.READ_EXTERNAL_STORAGE" />

            <application
           android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
              android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
            <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category 
             android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
             </activity>
             <activity android:name=".ResponseActivity"></activity>
              <activity android:name=".SettingsActivity"></activity>
              </application>
              </manifest>

mainactivity.java

             public class MainActivity extends AppCompatActivity 
              implements 
              View.OnClickListener {

              private List<String> stringList;
             private ListView list;
             private LoginPOJO jsonResponse;
             EditText etUserName;
             EditText etPassword;
             ImageView btnLogin;


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

            jsonResponse = new LoginPOJO();

           // UserLogin Field
           etUserName = (EditText) findViewById(R.id.etUserName);
          // UserLogin Password
           etPassword = (EditText) findViewById(R.id.etPassword);
          // Login Button Image
           btnLogin = (ImageView) findViewById(R.id.btnLogin);
           btnLogin.setOnClickListener(this);
           // User SignUp Button Image
          ImageView btnSignUp = (ImageView) 
          findViewById(R.id.btnSignUp);
          btnSignUp.setOnClickListener(this);
          // Forget Password Textbutton
          TextView frgtPassword = (TextView) 
          findViewById(R.id.forgetpassword);
          frgtPassword.setOnClickListener(this);
          // Skip for now button
            final TextView skipfornow = (TextView) 
              findViewById(R.id.skipnow);
           skipfornow.setOnClickListener(this);

            }

              private void 
            logIn(final String username, final String password) 
              {
             final ProgressDialog progressDialog = new 
             ProgressDialog(MainActivity.this);
            progressDialog.
            setProgressStyle(ProgressDialog.STYLE_SPINNER);
             progressDialog.setMessage("Logging you in...");
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.show();
            String UPLOAD_URL = "http://xxxxx-
             dev.elasticbeanstalk.com/api/v1/login";
            final StringRequest stringRequest = new 
            StringRequest(Request.Method.POST, UPLOAD_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    //Dismissing the progress dialog
                    progressDialog.dismiss();
                    // Getting the final Json Object
                    JSONObject parentObject;
                    try {
                        parentObject = new JSONObject(s);

                        // Getting the data from Data Json Object
                        JSONObject dataObject = 
                parentObject.getJSONObject("data");

                        // Getting data from Geo object
                        JSONObject geoObject = 
               dataObject.getJSONObject("geo");

                        // Getting data from businesses Array
                        JSONArray businessesArray = 
              dataObject.getJSONArray("businesses");

                        // Getting data from Meta Object
                        JSONObject metaObject = 
               parentObject.getJSONObject("meta");

                        startActivity(new Intent(MainActivity.this, 
                ResponseActivity.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //Showing toast message of the response
                    Log.i("TAG", "onResponse: " + s);

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) 
              {
                    //Dismissing the progress dialog
                    progressDialog.dismiss();
                    //Showing snackbar
                    Toast.makeText(MainActivity.this, "Connection 
              Problem", Toast.LENGTH_SHORT).show();
                }
            }) {
            @Override
            protected Map<String, String> getParams() throws 
            AuthFailureError {
            //Converting Bitmap to String
            //Creating parameters
            Map<String, String> params = new Hashtable<>();
            params.put("apikey", Utilities.API_KEY);
            params.put("secret", Utilities.SECRET_KEY);
            params.put("email", username);
            params.put("password",password);
            //Adding parameters
            //returning parameters
            return params;
              }
              };
             //Creating a Request Queue
             RequestQueue requestQueue =Volley.newRequestQueue(this);
             //Adding request to the queue
              requestQueue.add(stringRequest);
              }
              private void forgetPassword(final String userName) {
              final ProgressDialog progressDialog = new 
              ProgressDialog(MainActivity.this);
               progressDialog.setProgressStyle
              (ProgressDialog.STYLE_SPINNER);
              progressDialog.setMessage("Logging you in...");
               progressDialog.setIndeterminate(true);
              progressDialog.setCancelable(false);
              progressDialog.show();
              String UPLOAD_URL = "http://xxxxxx-
             dev.elasticbeanstalk.com/api/v1/request_password_reset";
             final StringRequest stringRequest = new 
               StringRequest(Request.Method.POST, UPLOAD_URL,
                new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    //Dismissing the progress dialog
                    progressDialog.dismiss();
                    // Getting the final Json Object
                    JSONObject parentObject;
                    try {
                        parentObject = new JSONObject(s);

                        // Getting the data from Data Json Object
                        JSONObject dataObject = 
              parentObject.getJSONObject("data");

                        // Getting data from Geo object
                        JSONObject geoObject = 
              dataObject.getJSONObject("geo");

                        // Getting data from businesses Array
                        JSONArray businessesArray = 
              dataObject.getJSONArray("businesses");

                        // Getting data from Meta Object
                        JSONObject metaObject = 
              parentObject.getJSONObject("meta");

                        startActivity(new Intent(MainActivity.this, 
              ResponseActivity.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //Showing toast message of the response
                    Log.i("TAG", "onResponse: " + s);

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) 
           {
                    //Dismissing the progress dialog
                    progressDialog.dismiss();
                    //Showing snackbar
                    Toast.makeText(MainActivity.this, "Connection 
            Problem", Toast.LENGTH_SHORT).show();
                }
            }) {
            @Override
            protected Map<String, String> getParams() throws 
            AuthFailureError {
            //Converting Bitmap to String
            //Creating parameters
            Map<String, String> params = new Hashtable<>();
            params.put("apikey", Utilities.API_KEY);
            params.put("secret", Utilities.SECRET_KEY);
            params.put("email", userName);
            //Adding parameters
            //returning parameters
             return params;
             }
               };
            //Creating a Request Queue
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            //Adding request to the queue
             requestQueue.add(stringRequest);
             }

             private void signUp() {

             Intent singUppage = new 
            Intent(MainActivity.this,ResponseActivity.class);
            startActivity(singUppage);

           }
           private void skipForNow(){
           Intent skipForNowPage = new 
           Intent(MainActivity.this,ResponseActivity.class);
           startActivity(skipForNowPage);
           }



            @Override
            public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btnLogin: {
            String username = etUserName.getText().toString().trim();
            String password = etPassword.getText().toString().trim();
            logIn(username, password);
            break;
            }
            case R.id.forgetpassword: {
            String username = etUserName.getText().toString().trim();
            forgetPassword(username);
            break;
            }
           case R.id.btnSignUp:{
            signUp();
            break;
            }
            case R.id.skipnow:{
            skipForNow();
            break;
            }
           }
           }

activitymain.xml

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          tools:context="com.example.reception.xxx.MainActivity">

           <LinearLayout
          android:layout_width="match_parent"
           android:layout_height="0dp"
          android:layout_weight="1"
           android:background="@drawable/signin_bg"
          android:orientation="vertical"
          android:paddingEnd="20dp"
          android:paddingLeft="20dp"
          android:paddingRight="20dp"
          android:paddingStart="20dp"
          android:weightSum="1">

           <ImageView
           android:id="@+id/imageView"
           android:layout_width="wrap_content"
           android:layout_height="40dp"
            android:contentDescription="@string/farbinder_text"
           android:src="@drawable/vdsss"/>

           <ImageView
           android:id="@+id/facebookSigninButton"
           android:layout_width="343dp"
           android:layout_height="76dp"
           android:contentDescription="@string/connect_with_facebook"
           android:src="@drawable/signin_fb"/>

            <ImageView
            android:layout_width="match_parent"
            android:layout_height="92dp"
            android:contentDescription="@string/connect_with_twitter"
            android:src="@drawable/signin_twitter"/>

           <ImageView
            android:id="@+id/btnSignUp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/signup_with_email"
            android:src="@drawable/signup_button"/>

            <TextView
            android:id="@+id/skipnow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/skip_login_for_now"
            android:textColor="#FFF"
            android:textSize="16sp"/>

           </LinearLayout>

           <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="187dp"
            android:orientation="vertical"
             android:paddingLeft="20dp"
             android:paddingStart="20dp"
             android:paddingEnd="20dp"
             android:paddingRight="20dp"
             android:paddingBottom="5dp">

             <EditText
              android:id="@+id/etUserName"
               android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:autoLink="email"
              android:hint="@string/enter_email"
              android:inputType="text"/>

             <EditText
              android:id="@+id/etPassword"
              android:layout_width="match_parent"
               android:layout_height="wrap_content"
              android:inputType="textPassword"
              android:hint="@string/enter_password"/>

              <ImageView
               android:id="@+id/btnLogin"
               android:layout_width="match_parent"
              android:layout_height="63dp"
             android:contentDescription="@string/sign_in"
              android:src="@drawable/signin_button"/>

              <TextView
              android:id="@+id/forgetpassword"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/forgot_password"
              android:textSize="16sp"
              android:layout_gravity="center_horizontal"/>

              </LinearLayout>

             </LinearLayout>

activityresponse.xml

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout 
           xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="16dp"     
          tools:context=
          "com.example.reception.xxxx.MainActivity">
          <TextView
          android:id="@+id/tvStatus"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="20sp"
          android:layout_gravity="center_horizontal"/>
          <ImageView
          android:id="@+id/backArrow"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:scaleType="center"
          android:src="@drawable/backarrow"
          android:contentDescription="@string/backarrow"
         />
         <ImageView
         android:id="@+id/image"
         android:layout_width="match_parent"
         android:layout_height="240dp"
         android:scaleType="centerCrop"/>
         <Button
          android:id="@+id/btnSelectImage"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/select_image"
          android:textAllCaps="false"
          android:layout_gravity="center_horizontal"/>
          </LinearLayout>

responseactivity.java

          public class ResponseActivity extends AppCompatActivity 
           implements 
           View.OnClickListener {
           private final int IMAGE_REQUEST_CODE = 5;
           private ImageView ivImage;
           private Bitmap selectedImage;
           @Override
            protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_response);
           TextView tvResponse = (TextView) 
            findViewById(R.id.tvStatus);
           ivImage = (ImageView) findViewById(R.id.image);
           ImageView etSignUp = (ImageView) 
            findViewById(R.id.btnSignUp);
           etSignUp.setOnClickListener(this);
           ImageView backArrow = 
           (ImageView)findViewById(R.id.backArrow);
           backArrow.setOnClickListener(this);
           }
           private void backArrow(){
            Intent bckArrow = new 
           Intent(ResponseActivity.this,MainActivity.class);
           startActivity(bckArrow);
           }
           private void signUp(){
           Intent signUp = new 
           Intent(ResponseActivity.this,SettingsActivity.class);
           startActivity(signUp);
           }

           @Override
            public void onClick(View v) {
            switch (v.getId()) {
            case R.id.backArrow:{
            backArrow();
            break;
            }
            case R.id.btnSignUp:{
            signUp();
            break;
            }

            }
           }

Solution

  • Problem is here:

    ImageView etSignUp = (ImageView)findViewById(R.id.btnSignUp);
    etSignUp.setOnClickListener(this);
    

    you don't have an imageview with id btnSignUp in your ResponseActivity layout.

    That's why it gives a nullpointerException of the imageview