2016-05-11 23 views
0

我很新的Android/Java工作,請耐心等待。代碼中的參考從Activity移動到Fragment不再解決

我將代碼從LoginActivity > onCreate移到我創建FragmentLogin的方法onCreate中,其中許多類不再解析,如findViewById。我假設我沒有正確傳遞容器活動的上下文。

或某些其他新手的錯誤...

這裏是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 ... 

    } 
} 

的變量/方法是當第二個代碼塊位於活動的onCreate方法中但在將代碼移動到之後不再解析所述FragmentLogin片段類的210:

findViewByIdsetContentView

基本上,這是一個登錄表單其中默認片段應login,和該頁面(Button forgottenPassword)上的按鈕將打開另一個片段(FragmentForgottenPassword)。

誰能告訴我我做錯了什麼?

+0

你的分段事務沒有被提交,你必須執行'''beginTransaction().add/replace/.commit()''','''''setContentView''' super.onCreate'''。 – danypata

回答

2

在你的活動佈局的xml文件,添加一些與此類似:

<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> 

在你LoginActivity:刪除片段經理的東西。你不需要它。

public class LoginActivity extends FragmentActivity { 

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

     setContentView(R.layout.fragment_login); 
    } 
} 

在你FragmentLogin,移動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方法中誇大佈局。

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 ;他們是在線教程的片段,你也可以瀏覽更多。我認爲這是一個很好的起點。所有最好的