片段中的 SQLite 数据库 - Android Studio

SQLite Database in Fragments - Android Studio

我是第一次做 android 开发。我需要把我的数据库分成碎片,我得到这个异常 - 谁能帮我找出我的错误?

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

数据库助手:

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "hunderassen.db";
public static final String TABLE_NAME = "hunderassen_table";
public static final String COLUMN1 = "ID";
public static final String COLUMN2 = "name_Hunderasse";
public static final String COLUMN3 = "durchschnittlicheDauer";
public static final String COLUMN4 = "groesseHund";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1); //creating db and table
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, name_Hunderasse TEXT, durchschnittlicheDauer TEXT, groesseHund TEXT) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists " + TABLE_NAME);
    this.onCreate(db);
}

public boolean insertData(String name_hr, String durchschnitt, String groesse) {

    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN2, name_hr);
    contentValues.put(COLUMN3, durchschnitt);
    contentValues.put(COLUMN4, groesse);
    long result = database.insert(TABLE_NAME, null, contentValues);

    //check if value is -1
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

和片段:

public class Fragment3 extends Fragment {

DatabaseHelper myDB;
EditText editHunderasse, editDurchschnitt, editGroesse, editID;
Button btnADD;
Button btnView;
Button btnUpdate;
Button btnDelete;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
    View rootView = inflater.inflate(R.layout.fragment3_layout, container, false);
    return rootView;
}

/**
 * if (container == null) {
 * return null;
 * }
 * return inflater.inflate(R.layout.fragment3_layout, container, false);
 * }
 **/

@Override
public void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    myDB = new DatabaseHelper(getActivity());

    editHunderasse = (EditText) getView().findViewById(R.id.editText_Hunderasse);
    editDurchschnitt = (EditText) getView().findViewById(R.id.editText_Dauer);
    editGroesse = (EditText) getView().findViewById(R.id.editText_Groesse);
    btnADD = (Button) getView().findViewById(R.id.button_add);
    btnView = (Button) getView().findViewById(R.id.button_view);
    btnUpdate = (Button) getView().findViewById(R.id.button_update);
    editID = (EditText) getView().findViewById(R.id.editText_id);
    btnDelete = (Button) getView().findViewById(R.id.button_delete);

    addData(); //aufrufen der Methode addData
}

public void addData() {
    btnADD.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myDB.insertData(
                    editHunderasse.getText().toString(),
                    editDurchschnitt.getText().toString(),
                    editGroesse.getText().toString()
            );
        }
    });
}

XML - 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hunderasse"
    android:id="@+id/textView3" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_Hunderasse" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dauer"
    android:id="@+id/textView4" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_Dauer"
    android:layout_gravity="center_horizontal" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Größe"
    android:id="@+id/textView17" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_Groesse" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_id" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ADD"
    android:id="@+id/button_add"
    android:clickable="false"
    android:enabled="true"
    android:focusable="false" />

也许有人可以提供帮助?非常感谢!

editHunderasse = (EditText) getView().findViewById(R.id.editText_Hunderasse);
editDurchschnitt = (EditText) getView().findViewById(R.id.editText_Dauer);
editGroesse = (EditText) getView().findViewById(R.id.editText_Groesse);
btnADD = (Button) getView().findViewById(R.id.button_add);
btnView = (Button) getView().findViewById(R.id.button_view);
btnUpdate = (Button) getView().findViewById(R.id.button_update);
editID = (EditText) getView().findViewById(R.id.editText_id);
btnDelete = (Button) getView().findViewById(R.id.button_delete);

将这些行移至 onCreateView() 方法并将 getView 替换为 rootView

editHunderasse = (EditText) rootView .findViewById(R.id.editText_Hunderasse);

把你在onCreate()里写的都写在onViewCreated()里或者在onCreateView()里写成如下

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
    View rootView = inflater.inflate(R.layout.fragment3_layout, container, false);

    myDB = new DatabaseHelper(getActivity());

    editHunderasse = (EditText) rootView.findViewById(R.id.editText_Hunderasse);
    editDurchschnitt = (EditText) rootView.findViewById(R.id.editText_Dauer);
    editGroesse = (EditText) rootView.findViewById(R.id.editText_Groesse);
    btnADD = (Button) rootView.findViewById(R.id.button_add);
    btnView = (Button) rootView.findViewById(R.id.button_view);
    btnUpdate = (Button) rootView.findViewById(R.id.button_update);
    editID = (EditText) rootView.findViewById(R.id.editText_id);
    btnDelete = (Button) rootView.findViewById(R.id.button_delete);

    addData(); //aufrufen der Methode addData


    return rootView;
}