2017-08-01 63 views
-4

我正在構建一個帶有兩個選項卡的選項卡式活動(「加入美國」)的應用程序,一個用於登錄,另一個用於註冊。選項卡式活動,登錄並註冊片段

JoinUs.java

public class JoinUs extends AppCompatActivity { 

/** 
* 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}. 
*/ 
private SectionsPagerAdapter mSectionsPagerAdapter; 

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

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.join_us); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // 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.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager)); 


} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_join_us, 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(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

/** 
* 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"; 

    public PlaceholderFragment() { 
    } 

    /** 
    * 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; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) { 
      View rootView = inflater.inflate(R.layout.tab_signin, container, false); 
      return rootView; 
     } 
     else { 
      View rootView = inflater.inflate(R.layout.tab_signup, container, false); 
      return rootView; 
     } 
       //View rootView = inflater.inflate(R.layout.fragment_join_us, container, false); 
       //TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
       //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 
       //return rootView; 
    } 
} 

/** 
* 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). 
     return PlaceholderFragment.newInstance(position + 1); 
    } 

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

tabSignUp.java

public class tabSignUp extends Fragment { 

DatabaseHelper helper = new DatabaseHelper(getContext()); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.tab_signup, container, false); 
    return rootView; 
} 

    public void onSignUpClick(View v) 
    { 
     if (v.getId() == R.id.signmeup) { 
      EditText name = getActivity().findViewById(R.id.fname); 
      EditText email = getActivity().findViewById(R.id.email); 
      EditText username = getActivity().findViewById(R.id.username); 
      EditText pass = getActivity().findViewById(R.id.password); 
      EditText pass2 = getActivity().findViewById(R.id.confpassword); 


      String nameStr = name.getText().toString(); 
      String emailStr = email.getText().toString(); 
      String usernameStr = username.getText().toString(); 
      String passStr = pass.getText().toString(); 
      String pass2Str = pass2.getText().toString(); 

      if (!passStr.equals(pass2Str)) { 
       Toast.makeText(getActivity(), "Passwords don't match", Toast.LENGTH_SHORT).show(); 

      } else { 
       //insert details in database 
       Contact c = new Contact(); 
       c.setName(nameStr); 
       c.setEmail(emailStr); 
       c.setUsername(usernameStr); 
       c.setPass(passStr); 

       helper.insertContact(c); 

      } 

     } 
    } 


} 

tabSignIn.java

public class tabSignIn extends Fragment { 

DatabaseHelper helper = new DatabaseHelper(getContext()); 

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

    return rootView; 
} 

public void onButtonClick(View v) 
{ 
    if (v.getId() == R.id.signmein){ 
     EditText username = (EditText) getActivity().findViewById(R.id.usernamein); 
     String str = username.getText().toString(); 

     EditText passwrd = getActivity().findViewById(R.id.passwordin); 
     String pass = passwrd.getText().toString(); 

     String password = helper.searchPass(str); 
     if (pass.equals(password)) 
     { 
      //take me to homepage 
      Intent i = new Intent (getActivity(),MainActivity.class); 
      i.putExtra("Username",str); 
      startActivity(i); 
     } 
     else 
      { 
       Toast.makeText(getActivity(),"Username and password don't match",Toast.LENGTH_SHORT).show(); 


      } 



    } 
} 
} 

眼下米y代碼不會標記任何錯誤,但是一旦我運行它,請填寫我的詳細信息並按下注冊或登錄按鈕,沒有任何反應。請我需要某人解釋原因。先謝謝你。

+0

'什麼都沒有發生'而且會發生什麼? – sudo

回答

0

onButtonClick方法永遠不會被調用。你忘了添加setOnClickListener

Button signInButton = (Button) rootView.findViewById(R.id.signmein); 
signInButton.setOnClickListener(new View.OnClickListener(){ 
    @Override 
    public void onClick(View v){ 
     onButtonClick(v); 
    } 
}); 

您也可以嘗試調試代碼(https://developer.android.com/studio/debug/index.html),並檢查發生了什麼。

+0

我已經嘗試過,但它仍然無法正常工作。還有什麼我可以嘗試? – Yaro

+0

所以'onButtonClick'方法仍然沒有被調用?你還可以發佈你的XML文件?日誌中是否有錯誤? –