从“活动”移动到“片段”的代码中的引用不再解析



我刚开始使用Android/Java,所以请耐心等待。

我将代码从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片段类的onCreateView后不再解析的变量/方法:

findViewByIdsetContentView

基本上,这是一个登录表单,默认片段应该是login,该页面上的按钮(Button forgottenPassword)将打开另一个片段(FragmentForgottenPassword)。

有人能告诉我我做错了什么吗?

在活动布局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>

在您的登录活动中:删除碎片管理器的内容。你还不需要它。

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

您应该在布局膨胀时从布局中获取子视图。在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;

我猜您希望片段显示在活动中。试试这个:

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();

    }}

然后在Login片段中,它应该是这样的:

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

我不完全确定我是否抓住了你所有的错误,但我认为你应该阅读以下内容:一和二;它们是关于碎片的在线教程,你也可以浏览更多。我认为这是一个很好的起点。所有最好的

最新更新