Android 片段未显示在应用程序中

Posted

技术标签:

【中文标题】Android 片段未显示在应用程序中【英文标题】:Android Fragment not showing in app 【发布时间】:2017-09-30 13:27:35 【问题描述】:

所以我正在创建一个应用程序,但我遇到了我的片段未在屏幕上显示的问题。它在 android Studio 预览版上看起来还不错,但在真实设备上运行时,我只看到操作栏,没有其他内容。

这是我的片段代码:

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


    <EditText
        android:id="@+id/fragment_login_userEmail"
        android:layout_
        android:layout_
        android:hint="Email"/>

    <EditText
        android:id="@+id/fragment_login_userPassword"
        android:layout_
        android:layout_
        android:hint="Password"/>

    <Button
        android:layout_
        android:layout_
        android:id="@+id/fragment_login_loginButton"
        android:text="Sign In"/>

    <Button
        android:layout_
        android:layout_
        android:id="@+id/fragment_login_registerButton"
        android:text="Sign Up"/>
</LinearLayout>

登录片段代码:

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

我做错了什么?

【问题讨论】:

你的片段容器的id是多少?是container 还是activity_fragment_base_fragmentContainer activity_fragment_base_fragmentContainer 您应该在调用inflater.inflate时将其设置为父级 能否在扩展 BaseFragmentActivity 的活动中发布实现的 createFragment 方法? @Override Fragment createFragment() return RegisterFragment.newInstance(); @ahasbini 【参考方案1】:
if (fragment ==null) 
        fragment = createFragment();
        fragmentManager.beginTransaction()
                .add(R.id.activity_fragment_base_fragmentContainer, fragment)
                .commit();
    

问题是.add

尝试将其替换为replace

【讨论】:

以上是关于Android 片段未显示在应用程序中的主要内容,如果未能解决你的问题,请参考以下文章

Android Layout 未显示或显示在前一个布局下方

ViewPager2 中未显示图像(片段重新打开时)

Listview 项目未在 Fragment Android 中显示

onbackpressed 数据未显示在片段中

带有片段的 Android Up 按钮未显示完整片段

片段内的Android TabHost