-1
MainActivity mActivity = new MainActivity();
mActivity.countRecords();

The Way I Used to call function countRecords() Which it was inside OnClickListenerCreateStudent class But I found the message when the button is pressed but with this a way no problems ((MainActivity)context).countRecords(); What is the difference between them now ?
screenshot

package com.example.train1;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.TextView;


    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button buttonCreateLocation = (Button) findViewById(R.id.buttonCreateStudent);
            buttonCreateLocation.setOnClickListener(new OnClickListenerCreateStudent());
            countRecords();
        }


        public void countRecords() {
            int recordCount = new TableControllerStudent(this).count(); 
            TextView textViewRecordCount = (TextView) findViewById(R.id.textViewRecordCount);
            textViewRecordCount.setText(recordCount + " records found.");
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
}





package com.example.train1;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


    import android.app.Activity;


    public class OnClickListenerCreateStudent implements View.OnClickListener {


             //MainActivity M_activity;
        //MainActivity mActivity// = new MainActivity();

        @Override
        public void onClick(View view) {

        //M_activity = new MainActivity();

          final Context context = view.getContext();

          LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          final View formElementsView = inflater.inflate(R.layout.student_input_form, null, false);

          final EditText editTextStudentFirstname = (EditText) formElementsView.findViewById(R.id.editTextStudentFirstname);
          final EditText editTextStudentEmail = (EditText) formElementsView.findViewById(R.id.editTextStudentEmail);

          new AlertDialog.Builder(context).setView(formElementsView).setTitle("Create Student")
          .setPositiveButton("Add",
              new DialogInterface.OnClickListener() {
             // countRecords();

                  public void onClick(DialogInterface dialog, int id) {

                      String studentFirstname = editTextStudentFirstname.getText().toString();
                      String studentEmail = editTextStudentEmail.getText().toString();

                      ObjectStudent objectStudent = new ObjectStudent();
                      objectStudent.firstname= studentFirstname;
                      objectStudent.email= studentEmail;
                      // call to table controller stud and put objectData
                      boolean createSuccessful = new TableControllerStudent(context).create(objectStudent);
                      if(createSuccessful){
                            Toast.makeText(context, "Student information was saved.", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(context, "Unable to save student information.", Toast.LENGTH_SHORT).show();
                        }




                    ((MainActivity)context).countRecords();

                      MainActivity mActivity = new MainActivity();
                      mActivity.countRecords();


                      //((MainActivity)).
                      dialog.cancel();

                  }
              }).show();

          // Toast.makeText(context," "+context , Toast.LENGTH_LONG).show();

        }





      public class ObjectStudent {
            int id;
            String firstname;
            String email;

            public ObjectStudent(){

           }

       }

      // class End

    }
ahmed
  • 39
  • 10
  • Although you're clearly not doing the right thing by doing `new MainActivity()`, but surely the LogCat would have helped. What did it say? – Sufian Jul 20 '16 at 11:02
  • what is the right thing? see error log & log cat -------- http://www8.0zz0.com/2016/07/20/16/967915010.png – ahmed Jul 20 '16 at 13:01
  • Update your question with the LogCat and please post it as text because of [these reasons](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557). And to answer your actual question, see the answer below. – Sufian Jul 20 '16 at 13:10
  • Btw, that ain't LogCat. It is showing some Eclipse errors. – Sufian Jul 20 '16 at 13:29
  • but in the LogCat there is nothing but two lines : : all messages(no filters) com.example.train1 (Session Filter) – ahmed Jul 20 '16 at 13:36
  • The LogCat can sometimes be a mess to work with. There should be few question on SO to help you out. – Sufian Jul 20 '16 at 14:20
  • Does your app crash on button press or when? – Sufian Jul 20 '16 at 15:33
  • Yes it dose when i press on – ahmed Jul 20 '16 at 15:46
  • Then you should follow the answer below. It should fix it. If it crashes again, update your question with the LogCat. Amd please mark the answer as correct if it crashes no longer. – Sufian Jul 20 '16 at 18:43
  • Possible duplicate of [Why is the Android emulator so slow? How can we speed up the Android emulator?](http://stackoverflow.com/questions/1554099/why-is-the-android-emulator-so-slow-how-can-we-speed-up-the-android-emulator) – ahmed Apr 26 '17 at 06:37

1 Answers1

1

Never create your activities with the constructor, they're bound to not be initialized correctly sooner or later.

Create the Activity via the Intent, as in the docs:

Intent intent = new Intent(this, DisplayMessageActivity.class);
Sufian
  • 6,405
  • 16
  • 66
  • 120
Vucko
  • 7,371
  • 2
  • 27
  • 45