将bundle传递给片段时出错

时间:2015-10-05 09:52:10

标签: java android android-fragments bundle

有人可以帮我查一下我的代码吗?我想将InformationWorkForce数据传递给WorkDetailsTable并通过一次点击按钮保存。我尝试了许多它不起作用的方法。请帮帮我...我从昨天开始就一直在这里。这完全令人沮丧!

TabPagerAdapter.java

    public class TabPagerAdapter extends FragmentStatePagerAdapter {
        public TabPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

       @Override
        public Fragment getItem(int i) {

            switch (i) {
                case 0:

                    return  Information.newInstance("name");
                case 1:

                    return WorkForce.newInstance("SubCon");
                case 2:


                    return WorkDetailsTable.newInstance();
            }
            return null ;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 3; //No of Tabs you can give your number of tabs
        }

Information.java

public class Information extends Fragment implements View.OnClickListener {
        private Spinner spinner, spinner2, spinner3;

        private static String a;
         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View info = inflater.inflate(R.layout.information, container, false);
            dialog = new DateDialog();
            spinner = (Spinner)info.findViewById(R.id.spinner);
            addItemsOnSpinner();
            a= spinner.getSelectedItem().toString();
            return info;
        }

     public static Information newInstance(String a)
        {
           Information fragment=new Information();
            Bundle bundle=new Bundle();
            bundle.putString("a",a);
            fragment.setArguments(bundle);
            return fragment;
        }

     public void addItemsOnSpinner() {
            List<String> list = new ArrayList<String>();
            list.add("1 ");
            list.add("2");
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);



        }

WorkForce.java

   public class WorkForce extends Fragment {
        private static EditText txt1;
        private static String subCon;
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View work = inflater.inflate(R.layout.workforce, container, false);
            txt1 = (EditText) work.findViewById(R.id.editText);
            subCon = txt1.getText().toString();
            return work;
        }

        public static WorkForce newInstance(String subCon) {

            WorkForce f = new WorkForce();
            Bundle bundle = new Bundle();
            bundle.putString("subCon", subCon);
            f.setArguments(bundle);
            return f;
        }
    }

WorkDetailsTable.java

  private com.example.project.project.API.InfoAPI ts;
        private com.example.project.project.API.WorkDetailsAPI WD;
        private com.example.project.project.API.WorkForceAPI WF;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View workDetails = inflater.inflate(R.layout.tableworkdetails, container, false);
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            spinnerTra = (Spinner) workDetails.findViewById(R.id.spinner6);
     addItemsOnSpinner();
     WD = new com.example.project.project.API.WorkDetailsAPI(getActivity());
            ts = new com.example.project.project.API.InfoAPI(getActivity());
            WF = new com.example.project.project.API.WorkForceAPI(getActivity());
      return workDetails;
        }

      public static WorkDetailsTable newInstance() {

        final WorkDetailsTable f = new WorkDetailsTable();
        final Bundle bundle = new Bundle();
        final String name = bundle.getString("a");
        final String subContractors = bundle.getString("subCon");
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                ts.insertTimeSheet(name);
                WF.insertWorkForce(subContractors);

            }
        });
        f.setArguments(bundle);
        return f;
    }

InfoAPI.java

public class InfoAPI {
    private SQLiteDatabase database;
    private MyDatabaseHelper dbHelper;
            ......

    public long insertTimeSheet(String name,String weather,String date,String status)
    {
        database=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(MyDatabaseHelper.Name,name);
        database.insert(MyDatabaseHelper.TABLE_INFO,null,values);
        database.close();
        return 0 ;

    }

}

WorkForceAPI.java

public class WorkForceAPI {
    private SQLiteDatabase database;
    private MyDatabaseHelper dbHelper;

              .......
    public long insertWorkForce(String subcontractors,String numberOfPerson,String numberOfHours)
    {

        database=dbHelper.getWritableDatabase();
        ContentValues values1=new ContentValues();
        values1.put(MyDatabaseHelper.Subcontractors,subcontractors);
        database.close();
        return 0 ;

    }

}

错误:

Error:(145, 17) error: non-static variable ts cannot be referenced from a static context
Error:(146, 17) error: non-static variable WF cannot be referenced from a static context

1 个答案:

答案 0 :(得分:0)

static newInstance函数中,您指的是tsWF,它们是实例属性。

您应该在其中一个实例方法中设置OnClickListener,例如onCreateViewonViewCreated等。