java.lang.RuntimeException:无法启动活动ComponentInfo {..} java.lang.NullPointerException

时间:2014-08-29 15:27:19

标签: java android sqlite android-fragments

我想将一些数据添加到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

我可以在不使用Fragment的情况下通过此代码向sqlite添加数据。但是当我将我的类转换为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);
}

}

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 个答案:

答案 0 :(得分:1)

btnAddnull,导致btnAdd.setOnClickListener(this)中出现NullPointerException。这是因为fragment_add_student_details布局中不包含ViewbtnAdd的<{1}}。