1

I need to go to a new activity i.e. feedback1 when I enter my credentials and press the button. However, my app crashes everytime I press the button

Here is the code for the MainActivity:

package com.example.bohra.feedback;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class MainActivity extends AppCompatActivity {

EditText username1;
EditText password1;
Button login;


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

    login = (Button) findViewById(R.id.login);
    username1 = (EditText) findViewById(R.id.username1);
    password1 = (EditText) findViewById(R.id.password1);



    login.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            loginfunc();

        }

    });
}


private void loginfunc() {

    fixedcon.username = username1.getText().toString();
    fixedcon.password = password1.getText().toString();
    String url = fixedcon.LOGIN_URL + username1.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            });

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

private void showJSON(String response) {

    String pass = "";

    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(fixedcon.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        pass = collegeData.getString(fixedcon.KEY_PASSWORD);

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


    if (pass.equals(fixedcon.password)) {
        Intent androidsolved_intent = new Intent(getApplicationContext(), feedback1.class);
        startActivity(androidsolved_intent);

    }

    else
    {   Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
    }
}

}

This is my AndroidManifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bohra.feedback">
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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=".feedback1" />
    <activity android:name=".feedback2" />
    <activity android:name=".feedback3" />
    <activity android:name=".feedback4" />
    <activity android:name=".feedback5" />
    <activity android:name=".feedback6" />
    <activity android:name=".feedback7" />
    <activity android:name=".feedback8" />
    <activity android:name=".feedback9" />
    <activity android:name=".feedback10" />
    <activity android:name=".finalconfirm" ></activity>
</application>

2 Answers2

1

Remove getApplicationContext() with MainActivity.this in your new Intent statement

0

Hard to say without seeing the stack trace but you never seem to declare or instantiate your object fixedcon that you use in your method loginfunc(...).

*FixedconObjectType* fixedcon = new *FixedconObjectType()*;

Distwo
  • 11,569
  • 8
  • 42
  • 65