Tadas Tadas - 5 months ago 11
Android Question

Code doesn't work, button isn't responding

I have created register activity which sends data to database, and the button has to open another activity, which is MainActivity. The code works fine, but theres one problem. When the button is being clicked, it responds and sends the information to database, but the activity isn't changing.

How could I change the code to open another activity?

RegisterRequest.java

public class RegisterRequest extends StringRequest {

private static final String REGISTER_REQUEST_URL = "http://loginacti.comli.com/Register.php";
private Map<String, String> params;

public RegisterRequest(String Expect, String Prize, String Name, String Email, Response.Listener<String> listener) {
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("Expect", Expect);
params.put("Prize", Prize);
params.put("Name", Name);
params.put("Email", Email);

}

@Override
public Map<String, String> getParams() {
return params;
}


RegisterActivity.java

public class RegisterActivity extends AppCompatActivity {

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

final EditText etUknown = (EditText) findViewById(R.id.etUknown);
final EditText etPrize = (EditText) findViewById(R.id.etPrize);
final EditText etName = (EditText) findViewById(R.id.etName);
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
final Button bRegister = (Button) findViewById(R.id.bRegister);

bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String Expect = etUknown.getText().toString();
final String Prize = etPrize.getText().toString();
final String Name = etName.getText().toString();
final String Email = etEmail.getText().toString();


Response.Listener<String> responseListener = new Response.Listener<String>(){

@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success){
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
RegisterActivity.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();

}

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


RegisterRequest registerRequest = new RegisterRequest(Expect, Prize, Name, Email, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}


MainActivity.java

public class MainActivity extends AppCompatActivity {

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


final TextView registerLink = (TextView) findViewById(R.id.eRegister);

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


Manifest

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<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=".RegisterActivity"></activity>
</application>


activity_register.xml

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etUknown"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Numbers" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etPrize"
android:hint="Secret CD"
android:layout_below="@+id/etUknown"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/etUknown"
android:layout_alignEnd="@+id/etUknown" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etName"
android:hint="Full Name"
android:layout_below="@+id/etPrize"
android:layout_alignRight="@+id/etPrize"
android:layout_alignEnd="@+id/etPrize"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/etEmail"
android:hint="New Email"
android:layout_below="@+id/etName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/etName"
android:layout_alignEnd="@+id/etName" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/bRegister"
android:layout_marginTop="46dp"
android:layout_below="@+id/etEmail"
android:layout_centerHorizontal="true" />

Answer

Add one more Permission ACCESS_NETWORK_STATE

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<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=".RegisterActivity"></activity>
</application>