gpt4 book ai didi

java - Android fragment 未显示在应用程序中

转载 作者:行者123 更新时间:2023-11-30 00:33:54 25 4
gpt4 key购买 nike

所以我正在创建一个应用程序,但我遇到了我的 fragment 没有显示在屏幕上的问题。它在 Android Studio 预览中看起来很好,但在真实设备上运行时我只看到操作栏,没有其他东西。

这是我的 fragment 代码:

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

登录 fragment 代码:

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

基础 fragment 类:

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

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

我做错了什么?

最佳答案

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

问题是 .add

尝试将其替换为replace

关于java - Android fragment 未显示在应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43747774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com