0
我想添加一些數據sqlite的DY fragmnet.but當我運行它給這個錯誤代碼..了java.lang.RuntimeException:無法啓動活動ComponentInfo {..}顯示java.lang.NullPointerException
java.lang.RuntimeException: Unable to start activity ComponentInfo{codes.project.sameera.o_project/codes.project.sameera.o_project.Indi_Add_Details_menu}: java.lang.NullPointerException
我可以通過此代碼將數據添加到sqlite而不使用Fragment.but但是當我將我的類轉換爲Fragmnet時,會發生這種情況。
我該如何解決這個問題..?
這是我Fragment_addStudentDetails類
public class Fragment_addStudentDetails extends Fragment implements View.OnClickListener {
EditText fName,mName, Address, sName, hNo, mNo, presentSpo;
DatabaseAdapter databaseHelper;
Button btnAdd;
Button btnView;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_add_student_details,container,false);
fName=(EditText) view.findViewById(R.id.editTextfname);
mName=(EditText) view.findViewById(R.id.editTextmName);
Address = (EditText) view.findViewById(R.id.editTextAdd);
sName = (EditText) view.findViewById(R.id.editTextsure);
hNo = (EditText) view.findViewById(R.id.editTexttpH);
mNo = (EditText) view.findViewById(R.id.editTextTPM);
presentSpo = (EditText) view.findViewById(R.id.editTextPS);
btnAdd = (Button) view.findViewById(R.id.btnAdd);
btnView = (Button) view.findViewById(R.id.btnView);
btnAdd.setOnClickListener(this);
btnView.setOnClickListener(this);
databaseHelper = new DatabaseAdapter(getActivity());
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnAdd:
addStudent(view);
break;
case R.id.btnView:
viewDetails(view);
break;
default:
break;
}
}
public void addStudent(View view){
String fname = fName.getText().toString();
String mname = mName.getText().toString();
String sname = sName.getText().toString();
String address = Address.getText().toString();
String TPhome = hNo.getText().toString();
String TPmobile = mNo.getText().toString();
String presentsport = presentSpo.getText().toString();
long id= databaseHelper.insertData(fname, mname, sname, address, TPhome, TPmobile, presentsport);
if(id<0){
Message.message(getActivity(),"Error inInsertiong Data");
}else
{
Message.message(getActivity(),"Succecsfuly inserted to data");
}
}
public void viewDetails(View view){
String data = databaseHelper.getAllData();
Message.message(getActivity(),data);
}
}
日誌貓
java.lang.RuntimeException: Unable to start activity ComponentInfo{codes.project.sameera.o_project/codes.project.sameera.o_project.Indi_Add_Details_menu}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at codes.project.sameera.o_project.Fragment_addStudentDetails.onCreateView(Fragment_addStudentDetails.java:38)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5240)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
Fragment_addStudentDetails.java的第38行是什麼? – cygery 2014-08-29 15:29:13
@cygery btnAdd.setOnClickListener(this);此方法 – kosala 2014-08-29 15:32:44
然後'btnAdd'爲空。請檢查您的'fragment_add_student_details'佈局是否包含帶有'btnAdd' ID的Button。 – cygery 2014-08-29 15:35:00