. lang。RuntimeException:无法启动activity ComponentInfo{.. .} .lang. nullpointerexception

时间:2023-01-12 20:50:12

I want to add some data to sqlite dy fragmnet.but when i Run the code it gives this error..

我想给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

i can add data to sqlite by this code without using Fragment.but when i convert my class into Fragmnet this happen.

我可以通过这段代码向sqlite添加数据,而不用使用片段。但是当我将类转换成Fragmnet时,就会发生这种情况。

How Can i fix this..?

我怎样才能解决这个问题?

this is my Fragment_addStudentDetails class

这是我的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);
}

}

}

log cat

日志的猫

        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)

           

           

1 个解决方案

#1


1  

btnAdd is null which results in the NullPointerException in btnAdd.setOnClickListener(this). This is because the fragment_add_student_details layout contains no View with the btnAdd id.

btnAdd为null,这会导致btnAdd. setonclicklistener (this)中的NullPointerException。这是因为fragment_add_student_details布局不包含带有btnAdd id的视图。

#1


1  

btnAdd is null which results in the NullPointerException in btnAdd.setOnClickListener(this). This is because the fragment_add_student_details layout contains no View with the btnAdd id.

btnAdd为null,这会导致btnAdd. setonclicklistener (this)中的NullPointerException。这是因为fragment_add_student_details布局不包含带有btnAdd id的视图。