Reputation: 683
So I'm creating an app and I'm stuck with this problem of my fragment not showing on screen. It looks just fine on Android Studio preview, but when run on real device I only see action bar and nothing else.
Here's my fragment code:
<?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 code:
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 class:
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);
}
}
And 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();
}
}
}
What am I doing wrong?
Upvotes: 1
Views: 965
Reputation: 172
if (fragment ==null) {
fragment = createFragment();
fragmentManager.beginTransaction()
.add(R.id.activity_fragment_base_fragmentContainer, fragment)
.commit();
}
Problems is .add
try to replace it to replace
Upvotes: 0