Aadhi Aadhi - 1 month ago 8
Android Question

Insert data to fragments from db. Is there any difference from common insertion method?

This is my code for insertion, When i put this code to activity it is working well, but in fragment it is not working.

View.OnClickListener addOnClickListener=new OnClickListener()
{
@Override
public void onClick(View v) {

String HouseName = "home";//txtName.getText().toString();
Integer HouseNumber =12; //Integer.parseInt(txtNumber.getText().toString());
String TableName="home_details";

dbhelper=new DatabaseHelper(context);
sqlitedatabase=dbhelper.getWritableDatabase();
dbhelper.insertHome(HouseName, HouseNumber, sqlitedatabase);
// Toast.makeText(getActivity().getBaseContext() ,"Logout", Toast.LENGTH_SHORT).show();
dbhelper.close();

}
};

Answer

try following code:

dbhelper = new DataBaseHelper(getActivity());

I think the NullPointException is because of context is null.

Comments