0

I have a little problem with my project. every time I use

btnLinkToCreateSession.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                   CreateSessionActivity.class);
            startActivity(i);
            finish();
        }
    });

the error appears:

"ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent)"

when i take it out, then everything works, again. is there any fault in the code, or must be the error somewhere else?

 btnLinkToCreateSession.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                   CreateSessionActivity.class);
            startActivity(i);
            finish();
        }
    });

    // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent j = new Intent(getApplicationContext(),
                RegisterActivity.class);
            startActivity(j);
            finish();
        }
    });

Edit: + CreateSessionActivity.java

package com.game;

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


 import com.game.library.DatabaseHandler;
import com.game.library.UserFunctions;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
      import android.widget.TextView;

 public class CreateSessionActivity extends Activity {
Button btnCreateSession;
Button btnLinkToLogin;
EditText inputNameOfSession;
EditText inputUserMax;
EditText inputBetAmount;
TextView createSessionErrorMsg;

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_SID = "sid";
private static String KEY_SNAME = "sname";
private static String KEY_SMAX = "smax";
private static String KEY_SBET = "sbet";
private static String KEY_CREATED_AT = "created_at";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.session);

    // Importing all assets like buttons, text fields
    inputNameOfSession = (EditText) findViewById(R.id.sessionName);
    inputUserMax = (EditText) findViewById(R.id.sessionMax);
    inputBetAmount = (EditText) findViewById(R.id.sessionBet);
    btnCreateSession = (Button) findViewById(R.id.btnCreateSession);
    btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
    createSessionErrorMsg = (TextView) findViewById(R.id.session_error);

    // Register Button Click event
    btnCreateSession.setOnClickListener(new View.OnClickListener() {            
        public void onClick(View view) {
            String sname = inputNameOfSession.getText().toString();
            String smax = inputUserMax.getText().toString();
            String sbet = inputBetAmount.getText().toString();

            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.openSession(sname, smax,      sbet);

            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    createSessionErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS); 
                    if(Integer.parseInt(res) == 1){
                        // user successfully registred
                        // Store user details in SQLite      Database
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_session_u = json.getJSONObject("session");

                        // Clear all previous data in database
                        userFunction.logoutSession(getApplicationContext());
                        db.addSession(json_session_u.getString(KEY_SNAME), json_session_u.getString(KEY_SMAX), json.getString(KEY_SBET), json_session_u.getString(KEY_CREATED_AT));                     
                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
                        // Close all views before launching      Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);
                        // Close Registration Screen
                        finish();
                    }else{
                        // Error in registration
                        createSessionErrorMsg.setText("Error occured in registration");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    // Link to Login Screen
    btnLinkToLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(i);
            // Close Registration View
            finish();
        }
    });
}

}

I had some trouble with inserting my logcat into here thatswhy:

http://androidprojekt.esy.es/android_login_api/loggo.txt

#

i think i solved it - it works

    inputEmail = (EditText) findViewById(R.id.loginEmail);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
    /* I had forgotten to add this: */
    btnLinkToCreateSession = (Button) findViewById(R.id.btnLinkToCreateSession);
    loginErrorMsg = (TextView) findViewById(R.id.login_error);
user3650191
  • 351
  • 2
  • 5
  • 17

1 Answers1

0

You should use the Activity's context instead of getApplicationContext() to start activities. Change all the intents which you use to start Activity like this:

Intent dashboard = new Intent(YourActivity.this, DashboardActivity.class);

You should also read this post for deeper insight.

Community
  • 1
  • 1
kupsef
  • 3,357
  • 1
  • 21
  • 31