无法在片段类中创建类的对象

时间:2014-09-12 04:26:44

标签: android android-fragments

这是我的代码..我有一个数据库助手类&我无法在Fragment类中创建它的对象

public class signup extends Fragment {

EditText e1,e2,e3,e4;
Button b1;
String s1,s2,s3;
Database d;
int i1;
@Override
public View onCreateView(LayoutInflater inflater,
    ViewGroup container,  Bundle savedInstanceState) {
        View sign= inflater.inflate(R.layout.signup, container, false);


        e1=(EditText)sign.findViewById(R.id.e1);
        e2=(EditText)sign.findViewById(R.id.e2);
        e3=(EditText)sign.findViewById(R.id.e3);
        e4=(EditText)sign.findViewById(R.id.e4);
        b1=(Button)sign.findViewById(R.id.b1);
        d=new Database(getActivity().getApplicationContext());      




        s1=e1.getText().toString();
        s2=e2.getText().toString();
        s3=e3.getText().toString();
        i1=Integer.parseInt(e4.getText().toString());

        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

            d.insert(s1, s2, s3, i1);   


                // TODO Auto-generated method stub

            }
        });


        return sign;

} }

错误的一部分是:

09-12 03:54:24.436: E/AndroidRuntime(836): FATAL EXCEPTION: main
09-12 03:54:24.436: E/AndroidRuntime(836): java.lang.NullPointerException
09-12 03:54:24.436: E/AndroidRuntime(836):  at com.example.demo.signup.<init>
(signup.java:22)
 09-12 03:54:24.436: E/AndroidRuntime(836): at   
com.example.demo.Tabs.getItem(Tabs.java:21)
09-12 03:54:24.436: E/AndroidRuntime(836):  at 
android.support.v4.app.FragmentPagerAdapter.instantiateItem
(FragmentPagerAdapter.java:97)
09-12 03:54:24.436: E/AndroidRuntime(836):  at 
android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
09-12 03:54:24.436: E/AndroidRuntime(836):  at 
android.support.v4.view.ViewPager.populate(ViewPager.java:1053)
09-12 03:54:24.436: E/AndroidRuntime(836):  at
android.support.v4.view.ViewPager.populate(ViewPager.java:919)
09-12 03:54:24.436: E/AndroidRuntime(836):  at  
android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
09-12 03:54:24.436: E/AndroidRuntime(836):  at 
android.view.View.measure(View.java:15518)
09-12 03:54:24.436: E/AndroidRuntime(836):  at 
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)

1 个答案:

答案 0 :(得分:0)

试试这个:

public class signup extends Fragment {

EditText e1,e2,e3,e4;
Button b1;
String s1,s2,s3;
Database d;
int i1;

@Override
public View onCreateView(LayoutInflater inflater,
    ViewGroup container,  Bundle savedInstanceState) {
       View sign= inflater.inflate(R.layout.signup, container, false);


    e1=(EditText)sign.findViewById(R.id.e1);
    e2=(EditText)sign.findViewById(R.id.e2);
    e3=(EditText)sign.findViewById(R.id.e3);
    e4=(EditText)sign.findViewById(R.id.e4);
    b1=(Button)sign.findViewById(R.id.b1);
    //Edit
    d=new Database(getActivity());      




    s1=e1.getText().toString();
    s2=e2.getText().toString();
    s3=e3.getText().toString();
    i1=Integer.parseInt(e4.getText().toString());

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

        d.insert(s1, s2, s3, i1);   


            // TODO Auto-generated method stub

        }
    });


    return sign;

}

}