不确定导致错误的原因。输出说它是因为我的Fragment包含数据库逻辑。显然,错误是由addScoutData();
和sqLiteDatabase = databaseHelper.getWritableDatabase();
这是片段类
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class AddScoutDataFragment extends Fragment {
Button cancelButton;
Button addDataButton;
DatabaseHelper myDB;
EditText tNumber, tPoticullis, tChevalFrise, tMoat, tRamparts, tDrawbridge, tSallyPort, tRockWall, tRockTerrain, tLowBar;
Context context = this.getActivity();
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
public AddScoutDataFragment() {
// Required empty public constructor
} //End of AddScoutDataFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_scout_data, container, false);
view.setBackgroundColor(Color.WHITE);
//Create database
myDB = new DatabaseHelper(getActivity());
//Instantiate all editText objects
tNumber = (EditText) view.findViewById(R.id.editNumber);
tPoticullis = (EditText) view.findViewById(R.id.editPoticullis);
tChevalFrise = (EditText) view.findViewById(R.id.editChevalFrise);
tMoat = (EditText) view.findViewById(R.id.editMoat);
tRamparts = (EditText) view.findViewById(R.id.editRamparts);
tDrawbridge = (EditText) view.findViewById(R.id.editDrawbridge);
tSallyPort = (EditText) view.findViewById(R.id.editSallyPort);
tRockWall = (EditText) view.findViewById(R.id.editRockWall);
tRockTerrain = (EditText) view.findViewById(R.id.editRockTerrain);
tLowBar = (EditText) view.findViewById(R.id.editLowBar);
//Adds data to ScoutFragment
addDataButton = (Button) view.findViewById(R.id.buttonDataAdd);
addDataButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//Saves data to database
addScoutInfo();
//Returns to ScoutFragment
ScoutFragment fragment = new ScoutFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} //End of onClick
}); //End of setOnClickListener
//Returns to ScoutFragment without adding any data
cancelButton = (Button) view.findViewById(R.id.buttonCancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Returns to ScoutFragment
ScoutFragment fragment = new ScoutFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} //End of onClick
}); //End of setOnClickListener
// Inflates the layout for this fragment
return view;
} //End of onCreateView
public void addScoutInfo(){
//Converts all editText values into strings
String sNumber = tNumber.getText().toString();
String sPoticullis = tPoticullis.getText().toString();
String sChevalFrise = tChevalFrise.getText().toString();
String sMoat = tMoat.getText().toString();
String sRamparts = tRamparts.getText().toString();
String sDrawbridge = tDrawbridge.getText().toString();
String sSallyPort = tSallyPort.getText().toString();
String sRockWall = tRockWall.getText().toString();
String sRockTerrain = tRockTerrain.getText().toString();
String sLowBar = tLowBar.getText().toString();
//Saves data
databaseHelper = new DatabaseHelper(context);
sqLiteDatabase = databaseHelper.getWritableDatabase();
databaseHelper.addInformation(sNumber, sPoticullis, sChevalFrise, sMoat, sRamparts, sDrawbridge, sSallyPort, sRockWall,
sRockTerrain, sLowBar, sqLiteDatabase);
Toast.makeText(getContext(), "Data Saved", Toast.LENGTH_LONG).show();
databaseHelper.close();
} //End of addScoutInfo
} //End of class
错误记录
3-16 00:36:47.355 5827-5827/com.compscitutorials.basigarcia.ramfernoscout E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.compscitutorials.basigarcia.ramfernoscout, PID: 5827
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.compscitutorials.basigarcia.ramfernoscout.AddScoutDataFragment.addScoutInfo(AddScoutDataFragment.java:100)
at com.compscitutorials.basigarcia.ramfernoscout.AddScoutDataFragment$1.onClick(AddScoutDataFragment.java:58)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
更新以下行:
databaseHelper = new DatabaseHelper(context);
到
databaseHelper = new DatabaseHelper(getActivity());
原因:您的上下文为空。
修改1:
您需要在保存数据之前打开数据库:
databaseHelper = new DatabaseHelper(context);
databaseHelper.open();
sqLiteDatabase = databaseHelper.getWritableDatabase();
databaseHelper.addInformation(sNumber, sPoticullis, sChevalFrise, sMoat, sRamparts, sDrawbridge, sSallyPort, sRockWall,
sRockTerrain, sLowBar, sqLiteDatabase);
Toast.makeText(getContext(), "Data Saved", Toast.LENGTH_LONG).show();
databaseHelper.close();
希望这是有道理的。