Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的数据库应用程序已停止工作,无法打开_Java_Android_Oop_Android Sqlite - Fatal编程技术网

Java 我的数据库应用程序已停止工作,无法打开

Java 我的数据库应用程序已停止工作,无法打开,java,android,oop,android-sqlite,Java,Android,Oop,Android Sqlite,我目前正在开发一个Android Studio应用程序,该应用程序利用SQLite数据库存储购车的客户记录,只要单击ADD按钮,应用程序就会停止。显然,存在一个空对象引用。该应用程序有一个MainActivity,它有一个add按钮,允许用户添加客户数据库条目。单击时,应用程序应转到CustomerRcrdActivity以允许数据库条目,从那里,用户应能够添加、查看、更新或删除条目,以及为什么条目应显示在屏幕上 我正在学习如何为Android创建应用程序,所以请善待我。我不是要求你为我做任何事

我目前正在开发一个Android Studio应用程序,该应用程序利用SQLite数据库存储购车的客户记录,只要单击ADD按钮,应用程序就会停止。显然,存在一个空对象引用。该应用程序有一个MainActivity,它有一个add按钮,允许用户添加客户数据库条目。单击时,应用程序应转到CustomerRcrdActivity以允许数据库条目,从那里,用户应能够添加、查看、更新或删除条目,以及为什么条目应显示在屏幕上

我正在学习如何为Android创建应用程序,所以请善待我。我不是要求你为我做任何事,但请你为我指出正确的方向。我到处都在寻找输入,但担心我忽略了一个逻辑错误,我可能需要第二双眼睛

如果您对此事有任何意见或建议,我们将不胜感激

多谢各位

下面是我怀疑可能是错误的代码(我将包含XML,以防我做了一些不可靠的事情)

content_main.xml(它包括我希望在添加条目时更新的listview)如果有更简单的方法,我不会一成不变

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<TextView
    android:id="@+id/ContentMain_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-smallcaps"
    android:text="@string/content_main_title"
    android:textAlignment="center"
    android:textSize="20sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.027" />

<Button
    android:id="@+id/button_CR_add"
    android:layout_width="112dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="416dp"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="7dp"
    android:onClick="buttonClicked"
    android:text="@string/ContentMain_button_CD_add"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.054"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ContentMain_title"
    app:layout_constraintVertical_bias="0.022" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="486dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button_CR_add">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>
以下是Logcat日志:

05-30 09:02:03.253 30145-30145/edu.phoenix.mbl402.week4apppp1943_rev1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.phoenix.mbl402.week4apppp1943_rev1, PID: 30145
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.phoenix.mbl402.week4apppp1943_rev1/edu.phoenix.mbl402.week4apppp1943_rev1.CustomerRcrdActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at edu.phoenix.mbl402.week4apppp1943_rev1.CustomerRcrdActivity.ShowRecords(CustomerRcrdActivity.java:276)
    at edu.phoenix.mbl402.week4apppp1943_rev1.CustomerRcrdActivity.onCreate(CustomerRcrdActivity.java:79)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
另外,我已经包括了建议的修改,再次感谢您

潜在问题1 很难看出代码中的任何内容会立即停止应用程序,也就是说,除非在没有任何用户交互的情况下启动CustomerRcrdActivity(假设它不是主要/第一个活动)

在这种情况下,可能是活动没有被定义为mainfest中的活动(AndroidManifest.xml

如果不是,则应将其编码为活动,以下内容就足够了:-

    <activity android:name=".CustomerRcrdActivity">
    </activity>
解决方法很简单,您只需在
onCreate
方法中使用一行代码,如下所示:-

    lv = (ListView) findViewById(R.id.lv); // For testing I gave the ListView an id of **lv**.
单击View按钮就可以了(目前的流行方式是,注意目前在测试过程中没有添加任何内容)

问题3-单击任何其他按钮 当调用
getValues
方法时,单击任何其他按钮结果(如果未输入任何内容)会导致NumberFormatException异常,因为CarCost字段中没有任何内容。为了避免这种情况,我简单地补充说:-

    String car_Cost = CarCost.getText().toString(); //<<<<EXISTING CODE
    if (car_Cost.length() < 1) {
        car_Cost = "0.00";
    }
    d_Car_Cost = Double.parseDouble(car_Cost); //<<<< EXISTING CODE
此外,
deleteCustomer
已更改为如果未根据以下内容删除任何内容,则发送不同的消息:-

public boolean deleteCustomer(int Id)
{
    boolean rv = false;
    SQLiteDatabase dh = this.getWritableDatabase();
    rv = (dh.delete(TABLE_CUSTOMERS, KEY_ID + " = ?",
            new String[] { String.valueOf(Id) }) > 0);
    dh.close();
    return rv;
}
private void deleteCustomer()
{
    getValues();
     if (dh.deleteCustomer(Id)) {
         Toast.makeText(getApplicationContext(), "Customer Deleted Successfully", Toast.LENGTH_LONG).show();
     } else {
         Toast.makeText(getApplicationContext(), "Customer Not Deleted", Toast.LENGTH_LONG).show();
     }
}
        case R.id.button_CR_add:
            // CALL addCustomer() TO ADD TO DATABASE
            addCustomer();

            // START NEW ACTIVITY WHEN ADD BUTTON CLICKED
            //Intent intent = new Intent(this, DatabaseHandler.class);
            //startActivity(intent);

            break;
修正添加按钮 尝试将DatabaseHandler作为活动启动的行被注释掉,如下所示:-

public boolean deleteCustomer(int Id)
{
    boolean rv = false;
    SQLiteDatabase dh = this.getWritableDatabase();
    rv = (dh.delete(TABLE_CUSTOMERS, KEY_ID + " = ?",
            new String[] { String.valueOf(Id) }) > 0);
    dh.close();
    return rv;
}
private void deleteCustomer()
{
    getValues();
     if (dh.deleteCustomer(Id)) {
         Toast.makeText(getApplicationContext(), "Customer Deleted Successfully", Toast.LENGTH_LONG).show();
     } else {
         Toast.makeText(getApplicationContext(), "Customer Not Deleted", Toast.LENGTH_LONG).show();
     }
}
        case R.id.button_CR_add:
            // CALL addCustomer() TO ADD TO DATABASE
            addCustomer();

            // START NEW ACTIVITY WHEN ADD BUTTON CLICKED
            //Intent intent = new Intent(this, DatabaseHandler.class);
            //startActivity(intent);

            break;
此外,为了在未输入任何内容时不添加行,我将
addCustomer
方法更改为:-

// INSERT DATA INTO DATABASE
private void addCustomer()
{
    boolean ok_to_add = true;
    getValues();
    if (F_Name == null || F_Name.length() < 1) ok_to_add = false;
    if (L_Name == null || L_Name.length() < 1) ok_to_add = false;
    if (Car_Make == null || Car_Make.length() < 1) ok_to_add = false;
    if (Car_Model == null || Car_Model.length() < 1) ok_to_add = false;
    if (ok_to_add) {
        dh.addCustomers(new Customer(F_Name, L_Name, Car_Make, Car_Model, d_Car_Cost));
        Toast.makeText(getApplicationContext(), "Customer Added Successfully", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(), "Unable To Add - Some Data hasn't been given", Toast.LENGTH_LONG).show();
    }
}
如果未进行更新(始终在当前),CustomerRcrdActivity的updateCustomer方法将更改为Toast另一条消息:-

有一些改进/考虑的示例(可能) 下面的代码可能会给你一些想法/技巧,并基于答案(我将包含所有代码,这些代码将帮助你避免应用程序在启动时崩溃)

第一个改进是在活动启动时加载ListView。这非常简单,只需调用
ShowRecords
方法。这样就不需要查看按钮(见下文,因为它已被hi-jacked)

然而,
ShowRecords
方法已经发生了巨大的变化。将适配器的单个实例与源客户列表合并

按钮中添加了一些逻辑/智能(sic)。不适用的将灰显。 -最初您不能删除或更新任何内容,只能添加(添加而不是添加任何数据)。 -如果单击列表中的某个项目,则该数据将加载到EditText中,并且“删除”按钮是唯一可以单击的按钮。 -如果更改EditText中的值,则只能单击“更新”按钮

所有操作都会起作用,并且还会更新列表(如果根据上述逻辑可用)。因此,可以添加、更新和删除记录

“查看”按钮已被提升为“清除”按钮。这始终可用,并将清除EditText并将Add按钮设置为可用(单击列表项仍将加载该记录的数据)

为了便于上述操作,定义了一些新的类变量,并且将一些从方法变量转移到了类变量,例如Adapater和客户列表(您已经声明了da,因此使用了da)

尽管对DatabaseHelper做了一些小的更改(long用于id,这是正确的做法,因为id可以超过int的最大值),但绝大多数更改仅针对CustomerRcrdActivity

无论如何,代码如下:-

客户活动:-

public class CustomerRcrdActivity extends AppCompatActivity {

    private EditText FName, LName, CarMake, CarModel, CarCost;
    private String F_Name;
    private String L_Name;
    private String Car_Make;
    private String Car_Model;
    private double d_Car_Cost;
    private DatabaseHandler dh;
    private dataAdapter da;
    private Customer dataModel;
    private ListView lv;
    private long mId;

    //<<<<<<<<<< new class variables
    private String original_FName, original_LName, original_CarMake, original_CarModel, original_CarCost;
    private boolean mLoaded = false;
    private Button mViewButton, mUpdateButton, mAddButton, mDeleteButton;
    private ArrayList<Customer> mCustomers = new ArrayList<>();
    private List<EditText> mAlleditTexts = new ArrayList<>();

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

        // GET Intent THAT STARTED ACTIVITY
        Intent intent = getIntent();

        // INSTANTIATE DATABASE HANDLER
        dh = new DatabaseHandler(this);

        FName = (EditText) findViewById(R.id.editText2_FName);
        LName = (EditText) findViewById(R.id.editText2_LName);
        CarMake = (EditText) findViewById(R.id.editText2_CarMake);
        CarModel = (EditText) findViewById(R.id.editText2_CarModel);
        CarCost = (EditText) findViewById(R.id.editText2_CarCost);

        //<<<<<<<<<<< ADDED
        mAlleditTexts.add(FName);
        mAlleditTexts.add(LName);
        mAlleditTexts.add(CarMake);
        mAlleditTexts.add(CarModel);
        mAlleditTexts.add(CarCost);
        lv = (ListView) findViewById(R.id.lv);
        //<<<<<<<<<< END OF ADDED

        mViewButton = (Button) findViewById(R.id.button_CR_view);
        mAddButton = (Button) findViewById(R.id.button_CR_add);
        mUpdateButton = (Button) findViewById(R.id.button_CR_update);
        mDeleteButton = (Button) findViewById(R.id.button_CR_delete);

        mViewButton.setVisibility(View.GONE); // never Show and free screen space
        mViewButton.setText("CLEAR"); // Hijack View Button for clear data
        mViewButton.setVisibility(View.VISIBLE); // Show the View (now CLEAR)
        mAddButton.setEnabled(false); // Can't click Add as nothing to add
        mUpdateButton.setEnabled(false); // Can't click Update nothing to update
        mDeleteButton.setEnabled(false); // Can't click Delete as nothing to delete
        setOriginalValues();
        ShowRecords(); //<<<< Always show the list no need for View button
        setEditTextFocusChangedListener(mAlleditTexts);
    }

    //<<<<<<<<<< NEW METHOD
    private void setEditTextFocusChangedListener(List<EditText> edit_texts) {
        for (EditText e: edit_texts) {

            e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (areOriginalValuesChanged()) {
                        if (mLoaded) {
                            mUpdateButton.setEnabled(true);
                            mDeleteButton.setEnabled(false);
                            mAddButton.setEnabled(false);
                        } else {

                        }
                    } else {
                        if (!mLoaded) {
                            mAddButton.setEnabled(true);
                        } else {
                            mAddButton.setEnabled(false);
                        }
                    }
                }
            });
        }
    }

    //<<<<<<<<<< NEW METHOD
    private void setOriginalValues() {
        original_FName = FName.getText().toString();
        original_LName = LName.getText().toString();
        original_CarMake = CarMake.getText().toString();
        original_CarModel = CarModel.getText().toString();
        original_CarCost = CarCost.getText().toString();
    }

    //<<<<<<<<<< NEW METHOD
    private boolean areOriginalValuesChanged() {
        if (original_FName.equals(FName.getText().toString())
                && original_LName.equals(LName.getText().toString())
                && original_CarMake.equals(CarMake.getText().toString())
                && original_CarModel.equals(CarModel.getText().toString())
                && original_CarCost.equals(CarCost.getText().toString())
                ) {
            return false;
        }
        return true;
    }

    //<<<<<<<<<< NEW METHOD
    private void clearEditTexts(List<EditText> editTexts) {
        for (EditText e: editTexts) {
            e.setText("");
        }
        setOriginalValues();
        mLoaded = false;
    }

    public void buttonClicked(View view)
    {
        int id = view.getId();

        switch (id)
        {
            case R.id.button_CR_update:
                // CALL updateCustomer() TO UPDATE CUSTOMER RECORDS
                updateCustomer();
                mLoaded = false;
                break;

            case R.id.button_CR_add:
                // CALL addCustomer() TO ADD TO DATABASE
                addCustomer();
                clearEditTexts(mAlleditTexts);
                mLoaded = false;

                // START NEW ACTIVITY WHEN ADD BUTTON CLICKED
                //Intent intent = new Intent(this, DatabaseHandler.class);
                //startActivity(intent);

                break;

            case R.id.button_CR_delete:
                // CALL deleteCustomer() TO DELETE CUSTOMER RECORD
                deleteCustomer();
                break;

            case R.id.button_CR_view:
                // CALL viewCustomer() TO VIEW CUSTOMER RECORD
                // START NEW ACTIVITY WHEN ADD BUTTON CLICKED
                //Intent intent2 = new Intent(this, CustomerDBActivity.class);
                //startActivity(intent2);

                // CALL showRecords() TO VIEW CUSTOMER RECORD VIA ListView
                clearEditTexts(mAlleditTexts);
                mLoaded = false;
                mAddButton.setEnabled(true);
                mUpdateButton.setEnabled(false);
                mDeleteButton.setEnabled(false);
                break;
        }
        ShowRecords(); // Update the List irrespective of button clicked
    }


    private void updateCustomer()
    {
        getValues();
         if (dh.updateCustomer(dataModel, mId)>0) {
             Toast.makeText(getApplicationContext(), "Customer Updated Successfully", Toast.LENGTH_LONG).show();
         } else {
             Toast.makeText(getApplicationContext(), "Customer Not Updated", Toast.LENGTH_LONG).show();
         }
    }
    // INSERT DATA INTO DATABASE
    private void addCustomer()
    {
        boolean ok_to_add = true;
        getValues();
        if (F_Name == null || F_Name.length() < 1) ok_to_add = false;
        if (L_Name == null || L_Name.length() < 1) ok_to_add = false;
        if (Car_Make == null || Car_Make.length() < 1) ok_to_add = false;
        if (Car_Model == null || Car_Model.length() < 1) ok_to_add = false;
        if (ok_to_add) {
            dh.addCustomers(new Customer(F_Name, L_Name, Car_Make, Car_Model, d_Car_Cost));
            Toast.makeText(getApplicationContext(), "Customer Added Successfully", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Unable To Add - Some Data hasn't been given", Toast.LENGTH_LONG).show();
        }
    }

    private void deleteCustomer()
    {
        getValues();
         if (dh.deleteCustomer(mId)) {
             Toast.makeText(getApplicationContext(), "Customer Deleted Successfully", Toast.LENGTH_LONG).show();
         } else {
             Toast.makeText(getApplicationContext(), "Customer Not Deleted", Toast.LENGTH_LONG).show();
         }
    }

    // FUNCTION TO GET VALUES FROM EditText
    private void getValues()
    {

        F_Name = FName.getText().toString();
        L_Name = LName.getText().toString();
        Car_Make = CarMake.getText().toString();
        Car_Model = CarModel.getText().toString();
        String car_Cost = CarCost.getText().toString();
        if (car_Cost.length() < 1) {
            car_Cost = "0.00";
        }
        d_Car_Cost = Double.parseDouble(car_Cost);

    }
    // RETRIEVE DATA FROM DATABASE & SET TO LIST VIEW
    //<<<<<<<<<< CHANGED QUITE A BIT
    // Introduced single Customer List with class scope rather than create new list and adapter everytime
    //      i.e. mCustomers
    // Always clear mCustomers and rebuild from database
    // if da (the adapter) is null and therefore hasn't been instantiated, instantiate it just once
    // otherwsie always notify the adapter that the data (mCustomer) has changed
    // Added code to the Listener (added just the once now) to set the edit text's to the values
    // of the clicked item in the list (load the data)
    //  setting flag to say that the data has just been loaded.
    //  also setting the original values to the new data
    private void ShowRecords()
    {
        mCustomers.clear();
        mCustomers.addAll(dh.getAllCustomers());
        //final ArrayList<Customer> customers = new ArrayList<>(dh.getAllCustomers());
        //final Customer customers = new Customer(dh.getAllCustomers());
        if (da == null) {
            da = new dataAdapter(this, mCustomers);
            //final dataAdapter data = new dataAdapter(this, mCustomers); //<<<< Shouldn't create a new adapter everytime

            lv.setAdapter(da);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    dataModel = mCustomers.get(position);
                    //<<<<<<<<<< Added
                    FName.setText(dataModel.getFName());
                    LName.setText(dataModel.getLName());
                    CarMake.setText(dataModel.get_CarMake());
                    CarModel.setText(dataModel.get_CarModel());
                    CarCost.setText(String.valueOf(dataModel.get_CarCost()));
                    mLoaded = true;
                    setOriginalValues();
                    mDeleteButton.setEnabled(true);
                    mUpdateButton.setEnabled(false);
                    mAddButton.setEnabled(false);
                    mId = dataModel.getID();
                    //<<<<<<<<<< End of Added
                    Toast.makeText(getApplicationContext(), String.valueOf(dataModel.getID()), Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            da.notifyDataSetChanged();
        }
    }
}
布局 布局都应该模仿你的,所以不需要包括它们(你的可能会更好,因为用于此的布局是最小的)

主要活动 这只会启动
CustomerRcrdActivity
,因此非常基本,只是:-

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent i = new Intent(this,CustomerRcrdActivity.class);
        startActivity(i);
    }
}
  • 注意:从
    CustomerRcrdActivity
    使用back将使您进入一个空屏幕,您无法执行其他操作
布局是创建项目时生成的基本单一文本视图:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


注意如果您使用上述方法,程序包名称(第3行)可能不正确,因此需要更改。

请发布错误日志更正方向-了解如何读取日志以及如何调试应用程序。错误是什么?stack trace pleseintent intent=新的intent(这是DatabaseHandler.class);DatabaseHandler不是一项活动非常感谢你花时间向我解释事情!现在,应用程序仍在启动前停止,我将发布L
/**
 *UPDATING SINGLE CUSTOMER
 **/
public int updateCustomer(Customer customer, int id)
{
    if (customer == null) {
        return 0;
    }
    SQLiteDatabase dh = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_FNAME, customer.getFName());
    values.put(KEY_LNAME, customer.getLName());
    values.put(KEY_CAR_MAKE, customer.get_CarMake());
    values.put(KEY_CAR_MODEL, customer.get_CarModel());
    values.put(KEY_CAR_COST, customer.get_CarCost());

    // UPDATING ROW
    return dh.update(TABLE_CUSTOMERS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(id) });
}
private void updateCustomer()
{
    getValues();
     if (dh.updateCustomer(dataModel, Id)>0) {
         Toast.makeText(getApplicationContext(), "Customer Updated Successfully", Toast.LENGTH_LONG).show();
     } else {
         Toast.makeText(getApplicationContext(), "Customer Not Updated", Toast.LENGTH_LONG).show();
     }
public class CustomerRcrdActivity extends AppCompatActivity {

    private EditText FName, LName, CarMake, CarModel, CarCost;
    private String F_Name;
    private String L_Name;
    private String Car_Make;
    private String Car_Model;
    private double d_Car_Cost;
    private DatabaseHandler dh;
    private dataAdapter da;
    private Customer dataModel;
    private ListView lv;
    private long mId;

    //<<<<<<<<<< new class variables
    private String original_FName, original_LName, original_CarMake, original_CarModel, original_CarCost;
    private boolean mLoaded = false;
    private Button mViewButton, mUpdateButton, mAddButton, mDeleteButton;
    private ArrayList<Customer> mCustomers = new ArrayList<>();
    private List<EditText> mAlleditTexts = new ArrayList<>();

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

        // GET Intent THAT STARTED ACTIVITY
        Intent intent = getIntent();

        // INSTANTIATE DATABASE HANDLER
        dh = new DatabaseHandler(this);

        FName = (EditText) findViewById(R.id.editText2_FName);
        LName = (EditText) findViewById(R.id.editText2_LName);
        CarMake = (EditText) findViewById(R.id.editText2_CarMake);
        CarModel = (EditText) findViewById(R.id.editText2_CarModel);
        CarCost = (EditText) findViewById(R.id.editText2_CarCost);

        //<<<<<<<<<<< ADDED
        mAlleditTexts.add(FName);
        mAlleditTexts.add(LName);
        mAlleditTexts.add(CarMake);
        mAlleditTexts.add(CarModel);
        mAlleditTexts.add(CarCost);
        lv = (ListView) findViewById(R.id.lv);
        //<<<<<<<<<< END OF ADDED

        mViewButton = (Button) findViewById(R.id.button_CR_view);
        mAddButton = (Button) findViewById(R.id.button_CR_add);
        mUpdateButton = (Button) findViewById(R.id.button_CR_update);
        mDeleteButton = (Button) findViewById(R.id.button_CR_delete);

        mViewButton.setVisibility(View.GONE); // never Show and free screen space
        mViewButton.setText("CLEAR"); // Hijack View Button for clear data
        mViewButton.setVisibility(View.VISIBLE); // Show the View (now CLEAR)
        mAddButton.setEnabled(false); // Can't click Add as nothing to add
        mUpdateButton.setEnabled(false); // Can't click Update nothing to update
        mDeleteButton.setEnabled(false); // Can't click Delete as nothing to delete
        setOriginalValues();
        ShowRecords(); //<<<< Always show the list no need for View button
        setEditTextFocusChangedListener(mAlleditTexts);
    }

    //<<<<<<<<<< NEW METHOD
    private void setEditTextFocusChangedListener(List<EditText> edit_texts) {
        for (EditText e: edit_texts) {

            e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (areOriginalValuesChanged()) {
                        if (mLoaded) {
                            mUpdateButton.setEnabled(true);
                            mDeleteButton.setEnabled(false);
                            mAddButton.setEnabled(false);
                        } else {

                        }
                    } else {
                        if (!mLoaded) {
                            mAddButton.setEnabled(true);
                        } else {
                            mAddButton.setEnabled(false);
                        }
                    }
                }
            });
        }
    }

    //<<<<<<<<<< NEW METHOD
    private void setOriginalValues() {
        original_FName = FName.getText().toString();
        original_LName = LName.getText().toString();
        original_CarMake = CarMake.getText().toString();
        original_CarModel = CarModel.getText().toString();
        original_CarCost = CarCost.getText().toString();
    }

    //<<<<<<<<<< NEW METHOD
    private boolean areOriginalValuesChanged() {
        if (original_FName.equals(FName.getText().toString())
                && original_LName.equals(LName.getText().toString())
                && original_CarMake.equals(CarMake.getText().toString())
                && original_CarModel.equals(CarModel.getText().toString())
                && original_CarCost.equals(CarCost.getText().toString())
                ) {
            return false;
        }
        return true;
    }

    //<<<<<<<<<< NEW METHOD
    private void clearEditTexts(List<EditText> editTexts) {
        for (EditText e: editTexts) {
            e.setText("");
        }
        setOriginalValues();
        mLoaded = false;
    }

    public void buttonClicked(View view)
    {
        int id = view.getId();

        switch (id)
        {
            case R.id.button_CR_update:
                // CALL updateCustomer() TO UPDATE CUSTOMER RECORDS
                updateCustomer();
                mLoaded = false;
                break;

            case R.id.button_CR_add:
                // CALL addCustomer() TO ADD TO DATABASE
                addCustomer();
                clearEditTexts(mAlleditTexts);
                mLoaded = false;

                // START NEW ACTIVITY WHEN ADD BUTTON CLICKED
                //Intent intent = new Intent(this, DatabaseHandler.class);
                //startActivity(intent);

                break;

            case R.id.button_CR_delete:
                // CALL deleteCustomer() TO DELETE CUSTOMER RECORD
                deleteCustomer();
                break;

            case R.id.button_CR_view:
                // CALL viewCustomer() TO VIEW CUSTOMER RECORD
                // START NEW ACTIVITY WHEN ADD BUTTON CLICKED
                //Intent intent2 = new Intent(this, CustomerDBActivity.class);
                //startActivity(intent2);

                // CALL showRecords() TO VIEW CUSTOMER RECORD VIA ListView
                clearEditTexts(mAlleditTexts);
                mLoaded = false;
                mAddButton.setEnabled(true);
                mUpdateButton.setEnabled(false);
                mDeleteButton.setEnabled(false);
                break;
        }
        ShowRecords(); // Update the List irrespective of button clicked
    }


    private void updateCustomer()
    {
        getValues();
         if (dh.updateCustomer(dataModel, mId)>0) {
             Toast.makeText(getApplicationContext(), "Customer Updated Successfully", Toast.LENGTH_LONG).show();
         } else {
             Toast.makeText(getApplicationContext(), "Customer Not Updated", Toast.LENGTH_LONG).show();
         }
    }
    // INSERT DATA INTO DATABASE
    private void addCustomer()
    {
        boolean ok_to_add = true;
        getValues();
        if (F_Name == null || F_Name.length() < 1) ok_to_add = false;
        if (L_Name == null || L_Name.length() < 1) ok_to_add = false;
        if (Car_Make == null || Car_Make.length() < 1) ok_to_add = false;
        if (Car_Model == null || Car_Model.length() < 1) ok_to_add = false;
        if (ok_to_add) {
            dh.addCustomers(new Customer(F_Name, L_Name, Car_Make, Car_Model, d_Car_Cost));
            Toast.makeText(getApplicationContext(), "Customer Added Successfully", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Unable To Add - Some Data hasn't been given", Toast.LENGTH_LONG).show();
        }
    }

    private void deleteCustomer()
    {
        getValues();
         if (dh.deleteCustomer(mId)) {
             Toast.makeText(getApplicationContext(), "Customer Deleted Successfully", Toast.LENGTH_LONG).show();
         } else {
             Toast.makeText(getApplicationContext(), "Customer Not Deleted", Toast.LENGTH_LONG).show();
         }
    }

    // FUNCTION TO GET VALUES FROM EditText
    private void getValues()
    {

        F_Name = FName.getText().toString();
        L_Name = LName.getText().toString();
        Car_Make = CarMake.getText().toString();
        Car_Model = CarModel.getText().toString();
        String car_Cost = CarCost.getText().toString();
        if (car_Cost.length() < 1) {
            car_Cost = "0.00";
        }
        d_Car_Cost = Double.parseDouble(car_Cost);

    }
    // RETRIEVE DATA FROM DATABASE & SET TO LIST VIEW
    //<<<<<<<<<< CHANGED QUITE A BIT
    // Introduced single Customer List with class scope rather than create new list and adapter everytime
    //      i.e. mCustomers
    // Always clear mCustomers and rebuild from database
    // if da (the adapter) is null and therefore hasn't been instantiated, instantiate it just once
    // otherwsie always notify the adapter that the data (mCustomer) has changed
    // Added code to the Listener (added just the once now) to set the edit text's to the values
    // of the clicked item in the list (load the data)
    //  setting flag to say that the data has just been loaded.
    //  also setting the original values to the new data
    private void ShowRecords()
    {
        mCustomers.clear();
        mCustomers.addAll(dh.getAllCustomers());
        //final ArrayList<Customer> customers = new ArrayList<>(dh.getAllCustomers());
        //final Customer customers = new Customer(dh.getAllCustomers());
        if (da == null) {
            da = new dataAdapter(this, mCustomers);
            //final dataAdapter data = new dataAdapter(this, mCustomers); //<<<< Shouldn't create a new adapter everytime

            lv.setAdapter(da);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    dataModel = mCustomers.get(position);
                    //<<<<<<<<<< Added
                    FName.setText(dataModel.getFName());
                    LName.setText(dataModel.getLName());
                    CarMake.setText(dataModel.get_CarMake());
                    CarModel.setText(dataModel.get_CarModel());
                    CarCost.setText(String.valueOf(dataModel.get_CarCost()));
                    mLoaded = true;
                    setOriginalValues();
                    mDeleteButton.setEnabled(true);
                    mUpdateButton.setEnabled(false);
                    mAddButton.setEnabled(false);
                    mId = dataModel.getID();
                    //<<<<<<<<<< End of Added
                    Toast.makeText(getApplicationContext(), String.valueOf(dataModel.getID()), Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            da.notifyDataSetChanged();
        }
    }
}
public class DatabaseHandler extends SQLiteOpenHelper
{
    // DATABASE VERSION
    private static final int DATABASE_VERSION = 1;
    // DATABASE NAME
    private static final String DATABASE_NAME = "Car Dealer";
    // PREFERRED THE NAME "STAR DEALERSHIP CUSTOMER DATABASE"
// CUSTOMER TABLE NAME
    public static final String TABLE_CUSTOMERS = "Customers";


    // CUSTOMER TABLE COLUMN NAMES
    private static final String KEY_ID = "ID";
    private static final String KEY_FNAME = "First";
    private static final String KEY_LNAME = "Last";
    private static final String KEY_CAR_MAKE = "Make";
    private static final String KEY_CAR_MODEL = "Model";
    private static final String KEY_CAR_COST = "Cost";

    DatabaseHandler(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    // CREATE TABLES
    @Override
    public void onCreate(SQLiteDatabase dh)
    {
        String CREATE_TABLE_CUSTOMERS = "CREATE TABLE " + TABLE_CUSTOMERS + "("
                + KEY_ID +" INTEGER PRIMARY KEY,"
                + KEY_FNAME +" TEXT,"
                + KEY_LNAME +" TEXT,"
                + KEY_CAR_MAKE  +" TEXT,"
                + KEY_CAR_MODEL +" TEXT,"
                + KEY_CAR_COST +" NUMERIC" + ")";
        dh.execSQL(CREATE_TABLE_CUSTOMERS);
    }
    // UPGRADING DATABASE
    @Override
    public void onUpgrade(SQLiteDatabase dh, int oldVersion, int newVersion)
    {
        // DROP OLDER TABLE IF EXISTED
        dh.execSQL("DROP TABLE IF EXISTS " + TABLE_CUSTOMERS);

        // CREATE TABLES AGAIN
        onCreate(dh);
    }
    /**
     * All CRUD (CREATE, READ, UPDATE, DELETE) OPERATIONS
     */
// INSERT VALUES TO TABLE CUSTOMERS

    public void addCustomers(Customer customer)
    {
        SQLiteDatabase dh = this.getReadableDatabase();
        ContentValues values = new ContentValues();

        values.put(KEY_FNAME, customer.getFName());
        values.put(KEY_LNAME, customer.getLName() );
        values.put(KEY_CAR_MAKE, customer.get_CarMake());
        values.put(KEY_CAR_MODEL, customer.get_CarModel());
        values.put(KEY_CAR_COST, customer.get_CarCost());

        dh.insert(TABLE_CUSTOMERS, null, values);
        dh.close();
    }
    /**
     *GETTING ALL CUSTOMERS
     **/
    public List<Customer> getAllCustomers()
    {
        List<Customer> customerList = new ArrayList<Customer>();
        // SELECT ALL QUERY
        String selectQuery = "SELECT  * FROM " + TABLE_CUSTOMERS;

        SQLiteDatabase dh = this.getWritableDatabase();
        Cursor cursor = dh.rawQuery(selectQuery, null);

        // LOOP THROUGH ALL ROWS & ADD TO LIST
        if (cursor.moveToFirst())
        {
            do {
                Customer customer = new Customer();
                customer.setID(Integer.parseInt(cursor.getString(0)));
                customer.setFName(cursor.getString(1));
                customer.setLName(cursor.getString(2));
                customer.set_CarMake(cursor.getString(3));
                customer.set_CarModel(cursor.getString(4));
                customer.set_CarCost(cursor.getDouble(5));

                // ADDING CUSTOMER TO LIST
                customerList.add(customer);
            } while (cursor.moveToNext());
        }
        // CLOSE OUT RAW QUERY CALL
        cursor.close();
        // RETURN CUSTOMER LIST
        return customerList;
    }
    /**
     *UPDATING SINGLE CUSTOMER
     **/
    public int updateCustomer(Customer customer, long id)
    {
        if (customer == null) {
            return 0;
        }
        SQLiteDatabase dh = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_FNAME, customer.getFName());
        values.put(KEY_LNAME, customer.getLName());
        values.put(KEY_CAR_MAKE, customer.get_CarMake());
        values.put(KEY_CAR_MODEL, customer.get_CarModel());
        values.put(KEY_CAR_COST, customer.get_CarCost());

        // UPDATING ROW
        return dh.update(TABLE_CUSTOMERS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(id) });
    }
    /**
     *DELETING SINGLE CUSTOMER
     **/
    public boolean deleteCustomer(long Id)
    {
        boolean rv = false;
        SQLiteDatabase dh = this.getWritableDatabase();
        rv = (dh.delete(TABLE_CUSTOMERS, KEY_ID + " = ?",
                new String[] { String.valueOf(Id) }) > 0);
        dh.close();
        return rv;
    }
}
public class Customer {

    private long CustomerId;
    private String FName;
    private String LName;
    private String CarMake;
    private String CarModel;
    private Double CarCost;

    public Customer(String first_name, String last_name, String make_of_car, String model, Double cost) {
        this.FName = first_name;
        this.LName = last_name;
        this.CarMake = make_of_car;
        this.CarModel = model;
        this.CarCost = cost;
    }

    public Customer() {
    }

    public void setFName(String FName) {
        this.FName = FName;
    }

    public String getFName() {
        return FName;
    }

    public void setLName(String LName) {
        this.LName = LName;
    }

    public String getLName() {
        return LName;
    }

    public void set_CarMake(String carMake) {
        CarMake = carMake;
    }

    public String get_CarMake() {
        return CarMake;
    }

    public void set_CarCost(Double carCost) {
        CarCost = carCost;
    }

    public Double get_CarCost() {
        return CarCost;
    }

    public void set_CarModel(String carModel) {
        CarModel = carModel;
    }

    public String get_CarModel() {
        return CarModel;
    }

    public void setCustomerId(long customerId) {
        CustomerId = customerId;
    }

    public long getCustomerId() {
        return CustomerId;
    }

    public long getID() {
        return this.getCustomerId();
    }

    public void setID(long id) {
        this.setCustomerId(id);
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent i = new Intent(this,CustomerRcrdActivity.class);
        startActivity(i);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    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=".CustomerRcrdActivity">
    </activity>
</application>