Java 为什么某些函数出现运行错误?

Java 为什么某些函数出现运行错误?,java,android,android-layout,android-studio,android-sqlite,Java,Android,Android Layout,Android Studio,Android Sqlite,我过去调用函数countRecords()的方式,它在onClickListenerCreateStuden类中,但我在按下按钮时发现了消息,但用这种方式没有问题 ((MainActivity)context.countRecords();现在它们之间有什么区别? 永远不要使用构造函数创建活动,它们迟早会被正确初始化 通过意图创建活动,如下所示: 尽管通过执行newmainActivity(),您显然没有做正确的事情,但LogCat肯定会有所帮助。上面说了什么?什么是正确的?请参阅错误日志和日

我过去调用函数countRecords()的方式,它在onClickListenerCreateStuden类中,但我在按下按钮时发现了消息,但用这种方式没有问题 ((MainActivity)context.countRecords();现在它们之间有什么区别?


永远不要使用构造函数创建活动,它们迟早会被正确初始化

通过
意图创建
活动
,如下所示:


尽管通过执行
newmainActivity()
,您显然没有做正确的事情,但LogCat肯定会有所帮助。上面说了什么?什么是正确的?请参阅错误日志和日志猫-----使用日志猫更新您的问题,并请将其作为文本发布,因为。要回答您的实际问题,请参见下面的答案。顺便说一句,这不是LogCat。它显示了一些Eclipse错误。但是在LogCat中只有两行::all messages(无过滤器)com.example.train1(会话过滤器)
MainActivity mActivity = new MainActivity();
mActivity.countRecords();
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

    }
Intent intent = new Intent(this, DisplayMessageActivity.class);