Android Fragment未在应用中显示

时间:2017-05-02 21:48:01

标签: java android xml android-fragments fragment

所以我正在创建一个应用程序,而我仍然坚持我的片段没有显示在屏幕上的问题。它在Android Studio预览中看起来很好,但是当在真实设备上运行时,我只看到操作栏而没有别的。

这是我的片段代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">


    <EditText
        android:id="@+id/fragment_login_userEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"/>

    <EditText
        android:id="@+id/fragment_login_userPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_login_loginButton"
        android:text="Sign In"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_login_registerButton"
        android:text="Sign Up"/>
</LinearLayout>

LoginFragment代码:

package com.dario.beastchat.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dario.beastchat.R;
import com.dario.beastchat.R2;
import com.dario.beastchat.activities.BaseFragmentActivity;
import com.dario.beastchat.activities.RegisterActivity;
import com.dario.beastchat.utils.Constants;

import java.net.URISyntaxException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.socket.client.IO;
import io.socket.client.Socket;

public class LoginFragment extends BaseFragment {

    @BindView(R2.id.fragment_login_userEmail)
    EditText mUserEmailEt;

    @BindView(R2.id.fragment_login_userPassword)
    EditText mUserPasswordEt;

    @BindView(R2.id.fragment_login_loginButton)
    Button mLoginButton;

    @BindView(R2.id.fragment_login_registerButton)
    Button mRegisterButton;

    private Unbinder mUnbinder;
    private Socket mSocket;


    public static LoginFragment newInstance(){
        return new LoginFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            mSocket = IO.socket(Constants.IP_LOCAL_HOST);
        } catch (URISyntaxException e) {
            Log.i(LoginFragment.class.getSimpleName(), e.getMessage());
            Toast.makeText(getActivity(), "Cant connect to the server", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

        mSocket.connect();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_login, container, false);
        mUnbinder = ButterKnife.bind(this, rootView);

        return rootView;
    }

    @OnClick(R2.id.fragment_login_registerButton)
    public void setmRegisterButton(){
        startActivity(new Intent(getActivity(), RegisterActivity.class));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSocket.disconnect();
    }
}

BaseFragment类:

package com.dario.beastchat.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

public class BaseFragment extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

和BaseFragment活动:

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import com.dario.beastchat.R;

public abstract class BaseFragmentActivity extends AppCompatActivity {
    abstract Fragment createFragment();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_fragment_base);

        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.activity_fragment_base_fragmentContainer);

        if (fragment ==null) {
            fragment = createFragment();
            fragmentManager.beginTransaction()
                    .add(R.id.activity_fragment_base_fragmentContainer, fragment)
                    .commit();
        }
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

if (fragment ==null) {
        fragment = createFragment();
        fragmentManager.beginTransaction()
                .add(R.id.activity_fragment_base_fragmentContainer, fragment)
                .commit();
    }

问题是.add

尝试将其替换为replace