-1

我正在做Android開發的第一次。我需要將我的數據庫分解成碎片,我得到這個異常 - 任何人都可以幫助我並找到我的錯誤嗎?碎片中的SQLite數據庫 - Android Studio

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

DatabaseHelper:

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" /> 

也許任何人都可以幫忙嗎?非常感謝!

+0

發生異常的線是什麼? –

+0

btnADD.setOnClickListener(new View.OnClickListener()它說有一個空引用,但我不知道爲什麼 – christina

回答

1

它寫在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; 
} 
+0

@christina我的答案對你有幫助嗎? – Nikhil

+0

是的,謝謝 - 它現在正在工作 – christina

0
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()方法,並與rootView取代getView

不管你寫在 onCreate()
editHunderasse = (EditText) rootView .findViewById(R.id.editText_Hunderasse);