2016-05-11 13 views
0

私はAndroid/Javaで非常に新しい作業をしていますので、私にご負担ください。コードの参照がアクティビティからフラグメントに移動しました

私は多くのクラスは、もは​​やそのようfindViewByIdとして、解決方法onCreateFragmentLoginを作成したフラグメントにLoginActivity > onCreateからコードを移動しました。私は、どこかでコンテナーのコンテクストのコンテキストを適切に渡していないと仮定しています。

またはいくつかの他の初心者の間違い...

ここLoginActivity.java [関連部分をコピー]

public class LoginActivity extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Initialize Fragments // 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

     setContentView(R.layout.fragment_login); 
    } 
} 

とFragmentLogin.javaです:

public class FragmentLogin extends Fragment { 

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

     final ValidationManager Check = new ValidationManager(); 
     final SessionManager Session = new SessionManager(this); 

     final EditText textEmail = (EditText) findViewById(R.id.login_email); 
     final EditText textPassword = (EditText) findViewById(R.id.login_pass); 
     final Button buttonLogIn = (Button) findViewById(R.id.button_login); 
     final Button buttonSignup = (Button) findViewById(R.id.button_signup); 

     // Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment // 
     final Button forgottenPassword = (Button) findViewById(R.id.button_lost_pass); 
     forgottenPassword.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       setContentView(R.layout.fragment_forgotten_password); 
      } 
     }); 

    ... more code ... 

    } 
} 

変数/メソッド2番目のコードブロックがアクティビティのonCreateメソッドに存在していても、コードをに移動した後に解決されなくなったときに機能していましたFragmentLoginフラグメントクラスの210:

findViewByIdsetContentView

基本的に、これはデフォルトのフラグメントがloginする必要がありますログインフォームで、そのページ(Button forgottenPassword)上のボタンは、別のフラグメント(FragmentForgottenPassword)を開きます。

誰でも私が間違っていることを教えてもらえますか?

+0

フラグメントトランザクションはコミットされていません。beginTransaction()。add/replace/.commit() '' 'を実行し、' '' setContentView''を '' ' super.onCreate'''。 – danypata

答えて

2

、これに似たものを追加:あなたのLoginActivityで

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <fragment android:name="your.package.FragmentLogin" 
       android:id="@+id/fragment_login" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" /> 

</LinearLayout> 

を:フラグメントマネージャのものを削除します。あなたはまだそれを必要としません。

あなたFragmentLoginで
public class LoginActivity extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.fragment_login); 
    } 
} 

、最後にreturn文を移動し、IDによって、あなたの意見を見つけるために、膨張したビューを使用します。

public class FragmentLogin extends Fragment { 

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

     final ValidationManager Check = new ValidationManager(); 
     final SessionManager Session = new SessionManager(this); 

     final EditText textEmail = (EditText) view.findViewById(R.id.login_email); 
     final EditText textPassword = (EditText) view.findViewById(R.id.login_pass); 
     final Button buttonLogIn = (Button) view.findViewById(R.id.button_login); 
     final Button buttonSignup = (Button) view.findViewById(R.id.button_signup); 

     // Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment // 
     final Button forgottenPassword = (Button) view.findViewById(R.id.button_lost_pass); 
     forgottenPassword.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       setContentView(R.layout.fragment_forgotten_password); 
      } 
     }); 

    ... more code ... 

     return view; 

    } 
} 
+0

ありがとう - これは非常に助けになりました。 – mwieczorek

0

膨らんでいる間にレイアウトからサブビューを取得する必要があります。レイアウトは通常OnCreateViewの方法で膨らんだ。あなたの活動のレイアウトXMLファイルに

View layout = inflater.inflate(R.layout.fragment_login, null); 

final EditText textEmail = (EditText) layout.findViewById(R.id.login_email); 
final EditText textPassword = (EditText) layout.findViewById(R.id.login_pass); 

... 

return layout; 
0

私は内に表示されるようにフラグメントにしたい推測していますアクティビティ。これを試してみてください:

public class LoginActivity extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    //put the setContentView() before the fragment initialization also, I noticed your activity layout is the same as your fragment layout,this should not be so, hence I changed that 
     setContentView(R.layout.activity_login); 


     // Initialize Fragments // 
     //you did not initialize the fragments completely, it should be like 
     //this 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction().replace(R.id.fragment_container, new FragmentLogin()).commit(); 


    }} 

[ログインフラグメントで、それは次のようにする必要があります:

public class FragmentLogin extends Fragment {@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.fragment_login, container, false); 

    final ValidationManager Check = new ValidationManager(); 
    final SessionManager Session = new SessionManager(this); 

    final EditText textEmail = (EditText) view.findViewById(R.id.login_email); 
    final EditText textPassword = (EditText) view.findViewById(R.id.login_pass); 
    final Button buttonLogIn = (Button) view.findViewById(R.id.button_login); 
    final Button buttonSignup = (Button) view.findViewById(R.id.button_signup); 

    // Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment // 
    final Button forgottenPassword = (Button) view.findViewById(R.id.button_lost_pass); 
    forgottenPassword.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //you cant call setContentView in a fragment 

     } 
    }); 

... more code ... 

}} 

私はすべてのあなたの過ちをキャッチ全くわかりませんが、私はあなたがこれらを読むべきだと思う:onetwo ;彼らは断片のオンラインチュートリアルです、あなたはまた、より多くを参照することができます。私はそれが良い出発点だと思う。すべてのベスト

関連する問題