为什么我不能从活动的数据发送到片段

问题描述:

  1. ACTIVITY 1:发送数据(MA_NV)TO SWIPMENU_CONTROL公共类
public class MainActivity extends ActionBarActivity { 

    public Button DangKy_Btn; 
    public Button DangNhap_Btn;  
    public EditText MaNV_Edit; 
    public EditText Password_Edit; 
    public CheckBox TrangThai_CB; 
    public TextView MaNV_Info; 

    DBAdapter db; 
    String prefname="my_status"; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     db= new DBAdapter(MainActivity.this); 

     DangKy_Btn=(Button) findViewById(R.id.DangKy_Btn); 
     DangKy_Btn.setOnClickListener(new OnClickListener() { 
      @Override  
      public void onClick(View v) {   
       Intent myIntent= new Intent(MainActivity.this,dang_ky.class);   
       startActivity(myIntent);   
      } 
     }); 

     MaNV_Edit=(EditText) findViewById(R.id.MaNV_Edit); 
     MaNV_Info=(TextView) findViewById(R.id.MaNV_Info); 
     Password_Edit=(EditText) findViewById(R.id.Password_Edit); 
     TrangThai_CB=(CheckBox) findViewById(R.id.TrangThai_CB); 
     DangNhap_Btn=(Button) findViewById(R.id.DangNhap_Btn); 
     DangNhap_Btn.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 

       String Ma_NV=MaNV_Edit.getText().toString(); 
       String Mat_Khau=Password_Edit.getText().toString(); 
       db.open(); 
       Cursor c=db.Check_Login(Ma_NV,Mat_Khau); 

       if(c.moveToFirst()){ 
        do_Login(Ma_NV); 
       } 
       else Toast.makeText(MainActivity.this,"Thất bại",Toast.LENGTH_SHORT).show(); 
       db.close(); 
      } 
     }); 
    } 

    public void do_Login(String Ma_NV){ 
     Toast.makeText(MainActivity.this, Ma_NV, Toast.LENGTH_SHORT).show(); 
     finish(); 
     Intent myIntent=new Intent(MainActivity.this,SwipMenu_Control.class); 
     Bundle myBundle=new Bundle(); 
     myBundle.putString("manv",Ma_NV); 
     myIntent.putExtra("Ma_NV",myBundle); 
     startActivity(myIntent); 

    } 

    protected void onPause(){ 
     super.onPause(); //GOI HAM LUU TRANG THAI 
     savingPreferences(); 
    } 

    protected void onResume(){ 
     super.onResume(); //GOI HAM DOC TRANG THAI 
     restoringPreferences(); 
    } 

    public void savingPreferences(){ 
     SharedPreferences pre=getSharedPreferences(prefname,MODE_PRIVATE); 
     SharedPreferences.Editor editor=pre.edit(); 
     String Ma_NV= MaNV_Edit.getText().toString(); 
     String Pass=Password_Edit.getText().toString(); 
     boolean trangthai=TrangThai_CB.isChecked(); 

     if(!trangthai){ 
      editor.clear(); 
     }else{ 
      editor.putString("Ma_NV", Ma_NV); 
      editor.putString("Password", Pass); 
      editor.putBoolean("Checked",trangthai); 
     } 
     editor.commit(); 
    } 

    public void restoringPreferences(){ 
     SharedPreferences pre=getSharedPreferences(prefname,MODE_PRIVATE); 
     boolean trangthai=pre.getBoolean("Checked",false); 
     if(trangthai) 
     { 
      String Ma_NV=pre.getString("Ma_NV",""); 
      String Pass =pre.getString("Password", ""); 
      MaNV_Edit.setText(Ma_NV); 
      Password_Edit.setText(Pass); 
      //Dang nhap vao luon 
      /* db.open(); 
      Cursor c=db.Check_Login(Ma_NV,Pass); 
      if(c.moveToFirst()){ 
       do_Login(Ma_NV); 
      } 
      else Toast.makeText(MainActivity.this,"Thất bại",Toast.LENGTH_SHORT).show(); 
      db.close();*/ 
     } 
     TrangThai_CB.setChecked(trangthai); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

2.ACTIVITY 2:发送数据(MA_NV ),以FRAGMENT_PRODUCT为什么我不能从活动的数据发送到片段

public class SwipMenu_Control extends ActionBarActivity implements ActionBar.TabListener { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link FragmentPagerAdapter} derivative, which will keep every 
    * loaded fragment in memory. If this becomes too memory intensive, it 
    * may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.swipview_main); 

     Intent callerIntent= getIntent(); 
     Bundle packageFromCaller=callerIntent.getBundleExtra("Ma_NV"); 
     String MaNV= packageFromCaller.getString("manv"); 

     Fragment fragInfo = new Fragment(); 
     Bundle bundle = new Bundle(); 
     bundle.putString("manv", MaNV); 
     fragInfo.setArguments(bundle); 

     // Set up the action bar. 
     final ActionBar actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 

     mViewPager.setAdapter(mSectionsPagerAdapter); 

     // When swiping between different sections, select the corresponding 
     // tab. We can also use ActionBar.Tab#select() to do this if we have 
     // a reference to the Tab. 

     mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       actionBar.setSelectedNavigationItem(position); 
      } 
     }); 

     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
      // Create a tab with text corresponding to the page title defined by 
      // the adapter. Also specify this Activity object, which implements 
      // the TabListener interface, as the callback (listener) for when 
      // this tab is selected. 
      actionBar.addTab(
        actionBar.newTab() 

          .setText(mSectionsPagerAdapter.getPageTitle(i)) 
          .setTabListener(this)); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
     // When the given tab is selected, switch to the corresponding page in 
     // the ViewPager. 
     mViewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a PlaceholderFragment (defined as a static inner class below). 
      switch(position){ 
       case 0: return PlaceholderFragment.newInstance(position); 
       case 1: return Fragment_Product.newInstance(position); 
       case 2: return Fragment_Accout.newInstance(position); 
       default:return PlaceholderFragment.newInstance(position); 
      } 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
       case 0: 
        return getString(R.string.title_section1).toUpperCase(l); 
       case 1: 
        return getString(R.string.title_section2).toUpperCase(l); 
       case 2: 
        return getString(R.string.title_section3).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 

    public static class PlaceholderFragment extends Fragment{ 

     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 

     private static final String ARG_SECTION_NUMBER = "section_number"; 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public PlaceholderFragment() { 


     } 

     private Button Add_Guest; 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 


      View rootView = inflater.inflate(R.layout.fragment_guest, container, false); 
      final String myValue = getArguments().getString("manv"); 
      if(myValue!=null){ 
      Add_Guest =(Button) rootView.findViewById(R.id.Btn_Add_Guest); 
      Add_Guest.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Toast.makeText(getActivity(), myValue, Toast.LENGTH_SHORT).show(); 
        Intent intent=new Intent(getActivity(),Add_Guest.class); 
        Bundle myBundle=new Bundle(); 
        myBundle.putString("Ma_NV", myValue); 
        intent.putExtra("manhanvien", myBundle); 
        getActivity().startActivity(intent); 
       } 
      }); 
      } 
      else Toast.makeText(getActivity(), "haiz", Toast.LENGTH_SHORT).show(); 
      return rootView; 
     } 

    } 

    public static class Fragment_Product extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     private static final String ARG_SECTION_NUMBER = "section_number"; 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static Fragment_Product newInstance(int sectionNumber) { 
      Fragment_Product fragment = new Fragment_Product(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public Fragment_Product() { 
     } 

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

      return rootView; 
     } 
     public static class Fragment_Accout extends Fragment { 
      /** 
      * The fragment argument representing the section number for this 
      * fragment. 
      */ 
      private static final String ARG_SECTION_NUMBER = "section_number"; 

      /** 
      * Returns a new instance of this fragment for the given section 
      * number. 
      */ 
      public static Fragment_Accout newInstance(int sectionNumber) { 
       Fragment_Accout fragment = new Fragment_Accout(); 
       Bundle args = new Bundle(); 
       args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
       fragment.setArguments(args); 
       return fragment; 
      } 

      public Fragment_Accout() { 
      } 

      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
       View rootView = inflater.inflate(R.layout.fragment_accout, container, false); 
       return rootView; 
      } 
    } 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     //Handle the back button 
     if(keyCode == KeyEvent.KEYCODE_BACK) { 
      //Ask the user if they want to quit 
      new AlertDialog.Builder(this) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .setTitle("QUIT") 
      .setMessage("BAN CO CHAC CHAN MUON THOAT") 
      .setPositiveButton("YES", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        //Stop the activity 
        SwipMenu_Control.this.finish();  
       } 

      }) 
      .setNegativeButton("NO", null) 
      .show(); 

      return true; 
     } 
     else { 
      return super.onKeyDown(keyCode, event); 
     } 

    } 
    @Override 
    public void onBackPressed() { 
     new AlertDialog.Builder(this) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .setTitle("Closing Activity") 
      .setMessage("Are you sure you want to close this activity?") 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
     { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       SwipMenu_Control.this.finish();  
      } 

     }) 
     .setNegativeButton("No", null) 
     .show(); 
    } 
} 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

3.

> 10-16 12:03:47.793: E/AndroidRuntime(852): FATAL EXCEPTION: main 10-16 
> 12:03:47.793: E/AndroidRuntime(852): java.lang.IllegalStateException: 
> Could not find a method Add_Guest(View) in the activity class 
> theanh.quanly.SwipMenu_Control for onClick handler on view class 
> android.widget.Button with id 'Btn_Add_Guest' 10-16 12:03:47.793: 
> E/AndroidRuntime(852): at android.view.View$1.onClick(View.java:3620) 
> 10-16 12:03:47.793: E/AndroidRuntime(852): at 
> android.view.View.performClick(View.java:4240) 10-16 12:03:47.793: 
> E/AndroidRuntime(852): at 
> android.view.View$PerformClick.run(View.java:17721) 10-16 
> 12:03:47.793: E/AndroidRuntime(852): at 
> android.os.Handler.handleCallback(Handler.java:730) 10-16 
> 12:03:47.793: E/AndroidRuntime(852): at 
> android.os.Handler.dispatchMessage(Handler.java:92) 10-16 
> 12:03:47.793: E/AndroidRuntime(852): at 
> android.os.Looper.loop(Looper.java:137) 10-16 12:03:47.793: 
> E/AndroidRuntime(852): at 
> android.app.ActivityThread.main(ActivityThread.java:5103) 10-16 
> 12:03:47.793: E/AndroidRuntime(852): at 
> java.lang.reflect.Method.invokeNative(Native Method) 10-16 
> 12:03:47.793: E/AndroidRuntime(852): at 
> java.lang.reflect.Method.invoke(Method.java:525) 10-16 12:03:47.793: 
> E/AndroidRuntime(852): at 
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
> 10-16 12:03:47.793: E/AndroidRuntime(852): at 
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-16 
> 12:03:47.793: E/AndroidRuntime(852): at 
> dalvik.system.NativeStart.main(Native Method) 10-16 12:03:47.793: 
> E/AndroidRuntime(852): Caused by: java.lang.NoSuchMethodException: 
> Add_Guest [class android.view.View] 10-16 12:03:47.793: 
> E/AndroidRuntime(852): at 
> java.lang.Class.getConstructorOrMethod(Class.java:423) 10-16 
> 12:03:47.793: E/AndroidRuntime(852): at 
> java.lang.Class.getMethod(Class.java:787) 10-16 12:03:47.793: 
> E/AndroidRuntime(852): at android.view.View$1.onClick(View.java:3613) 
> 10-16 12:03:47.793: E/AndroidRuntime(852): ... 11 more 
+0

究竟什么不行?任何日志或错误消息? – tristan2468 2014-10-16 15:44:14

+0

感谢您的评论@ tristan2468。请帮助我! – theanh 2014-10-16 16:07:47

只能从活动发送的数据通过使用监听器,抽象类或构造器片段。希望你能理解。