我想向sqlite dy fragmnet添加一些数据。但是当我运行代码时,它给出了此错误。

  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。但是当我将类转换为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)


最佳答案

btnAddnull,这会在btnAdd.setOnClickListener(this)中导致NullPointerException。这是因为fragment_add_student_details布局不包含带有View id的btnAdd

关于java - java.lang.RuntimeException:无法启动 Activity ComponentInfo {..} java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25571428/

10-10 09:25