我的 fragment_profile.xml 不显示。我的程序有什么问题?

huangapple go评论76阅读模式
英文:

My fragment_profile.xml doesnt show. Whats wrong with my program?

问题

I created a new fragment which is ProfileFragment.java and also created fragment_profile.xml.
The program does not give any error but my layout doesn't show. What is wrong?

My ProfileFragment.java:

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;

public class ProfileFragment extends Fragment {

    //firebase auth
    FirebaseAuth firebaseAuth;
    FirebaseUser user;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;

    //layout views
    ImageView avatar;
    TextView nameTxt, emailTxt, phoneTxt;

    public ProfileFragment() {
        //boş public constructor gerekli
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_profile, container, false);

        //init firebase
        firebaseAuth = FirebaseAuth.getInstance();
        user = firebaseAuth.getCurrentUser();
        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference("Users");

        //init layout views
        avatar = view.findViewById(R.id.avatar);
        nameTxt = view.findViewById(R.id.nameTxt);
        emailTxt = view.findViewById(R.id.emailTxt);
        phoneTxt = view.findViewById(R.id.phoneTxt);

        /* giriş yapan kullanıcıların bilgilerini email yada uid kullanarak çekmek zorundayız
        Kullanıcı detaylarını email adreslerini kullanarak çekeceğiz
        orderbyChild query kullanarak giriş yapılan email ile email key ini eşleştirerek kullanıcı detaylarına ulaşılıyor
         */
        Query query = databaseReference.orderByChild("email").equalTo(user.getEmail());
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                //gerekli veriler gelene kadar kontrol et
                for (DataSnapshot ds: snapshot.getChildren()){
                    //verileri almak için
                    String name = ""+ ds.child("name").getValue();
                    String email = ""+ ds.child("email").getValue();
                    String phone = ""+ ds.child("phone").getValue();
                    String image = ""+ ds.child("image").getValue();

                    //set data
                    nameTxt.setText(name);
                    emailTxt.setText(email);
                    phoneTxt.setText(phone);

                    try {
                        // resim alınırsa ayarla
                        Picasso.get().load(image).into(avatar);
                    } catch (Exception e){
                        // resim alınırken herhangi bir sıkıntı varsa varsayılan olarak ayarla
                        Picasso.get().load(R.drawable.add_photo_foreground).into(avatar);
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

        return view;
    }
}

and my fragment_profile.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F1EDED"
    tools:context=".ProfileFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:background="#2ECC71">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:orientation="horizontal"
                android:layout_marginTop="100dp">

                <ImageView
                    android:id="@+id/avatar"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:src="@drawable/add_photo_foreground"
                    android:layout_marginStart="20dp"
                    android:background="#6AE493"
                    android:padding="5dp">
                </ImageView>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/nameTxt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_marginStart="5dp"
                        android:layout_marginLeft="5dp"
                        android:textSize="25dp"
                        android:textColor="#FFFFFF"></TextView>

                    <TextView
                        android:id="@+id/emailTxt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="5dp"
                        android:layout_marginLeft="5dp"
                        android:textColor="#FFFFFF"></TextView>

                    <TextView
                        android:id="@+id/phoneTxt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="5dp"
                        android:layout_marginLeft="5dp"
                        android:textColor="#FFFFFF"></TextView>

                </LinearLayout>

            </LinearLayout>

        </RelativeLayout>
    </RelativeLayout>

</ScrollView>

I want to create a user profile page using Firebase Realtime Database. The user details come from using email for the current signed user. But my profile layout file does not work. How can I fix this problem? What's wrong in my code?

My DashboardActivity.java - For fragment transaction.

package com.gamze.pawsbook;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class DashboardActivity extends AppCompatActivity {

    //FirebaseAuth
    FirebaseAuth firebaseAuth;

    //Actionbar
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);

        //ActionBar ve başlığı
        actionBar = getSupportActionBar();
        actionBar.setTitle("Profile");

        firebaseAuth = FirebaseAuth.getInstance();

        //Bottom navigation
        BottomNavigationView navigationView = findViewById(R.id.navigation);
        navigationView.setOnNavigationItemSelectedListener(selectedListener);

        //Home Fragment'ı boş default fragment olarak ayarlanması
        actionBar.setTitle("Home"); //ActionBar başlığını değiştirme
        HomeFragment homeFragment = new HomeFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content, homeFragment, "");
        ft.commit();
    }

    // ... (Rest of your code)

}

activity_dashboard.xml

英文:

I created a new fragment which is ProfileFragment.java and also created fragment_profile.xml.
The program does not give any error but my layout doesnt show. What is wrong?

My ProfileFragment.java :


import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
public class ProfileFragment extends Fragment {
//firebase auth
FirebaseAuth firebaseAuth;
FirebaseUser user;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
//layout views
ImageView avatar;
TextView nameTxt, emailTxt, phoneTxt;
public ProfileFragment() {
//boş public constructor gerekli
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =  inflater.inflate(R.layout.fragment_profile, container, false);
//init firebase
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference(&quot;Users&quot;);
//init layout views
avatar = view.findViewById(R.id.avatar);
nameTxt = view.findViewById(R.id.nameTxt);
emailTxt = view.findViewById(R.id.emailTxt);
phoneTxt = view.findViewById(R.id.phoneTxt);
/* giriş yapan kullanıcıların bilgilerini email yada uid kullanarak &#231;ekmek zorundayız
Kullanıcı detaylarını email adreslerini kullanarak &#231;ekicez
orderbyChild query kullanarak giriş yapılan email ile email key ini eşleştirerek kullanıcı detaylarına ulaşılıyor
*/
Query query = databaseReference.orderByChild(&quot;email&quot;).equalTo(user.getEmail());
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
//gerekli veriler gelene kadar kontrol et
for (DataSnapshot ds: snapshot.getChildren()){
//verileri almak i&#231;in
String name = &quot;&quot;+ ds.child(&quot;name&quot;).getValue();
String email = &quot;&quot;+ ds.child(&quot;email&quot;).getValue();
String phone = &quot;&quot;+ ds.child(&quot;phone&quot;).getValue();
String image = &quot;&quot;+ ds.child(&quot;image&quot;).getValue();
//set data
nameTxt.setText(name);
emailTxt.setText(email);
phoneTxt.setText(phone);
try {
// resim alınırsa ayarla
Picasso.get().load(image).into(avatar);
} catch (Exception e){
// resim alınırken herangi bir sıkıntı varsa varsayılan olarak ayarla
Picasso.get().load(R.drawable.add_photo_foreground).into(avatar);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
return view;
}
}

and my fragment_profile.xml :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;ScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;#F1EDED&quot;
tools:context=&quot;.ProfileFragment&quot;&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;180dp&quot;
android:background=&quot;#2ECC71&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;120dp&quot;
android:orientation=&quot;horizontal&quot;
android:layout_marginTop=&quot;100dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/avatar&quot;
android:layout_width=&quot;120dp&quot;
android:layout_height=&quot;120dp&quot;
android:src=&quot;@drawable/add_photo_foreground&quot;
android:layout_marginStart=&quot;20dp&quot;
android:background=&quot;#6AE493&quot;
android:padding=&quot;5dp&quot;&gt;
&lt;/ImageView&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;TextView
android:id=&quot;@+id/nameTxt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;5dp&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginLeft=&quot;5dp&quot;
android:textSize=&quot;25dp&quot;
android:textColor=&quot;#FFFFFF&quot;&gt;&lt;/TextView&gt;
&lt;TextView
android:id=&quot;@+id/emailTxt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginLeft=&quot;5dp&quot;
android:textColor=&quot;#FFFFFF&quot;&gt;&lt;/TextView&gt;
&lt;TextView
android:id=&quot;@+id/phoneTxt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginLeft=&quot;5dp&quot;
android:textColor=&quot;#FFFFFF&quot;&gt;&lt;/TextView&gt;
&lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;
&lt;/RelativeLayout&gt;
&lt;/RelativeLayout&gt;
&lt;/ScrollView&gt;

I want to create a user profile page using firebase realtime database. The user details come from using email for current signed user. But my profile layout file does not work. How can I fix this problem? Whats wrong in my code?

My DasboardAvtivity.java -> For fragment transaction.

package com.gamze.pawsbook;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class DashboardActivity extends AppCompatActivity {
//FirebaseAuth
FirebaseAuth firebaseAuth;
//Actionbar
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
//ActionBar ve başlığı
actionBar = getSupportActionBar();
actionBar.setTitle(&quot;Profile&quot;);
//profileTxt = findViewById(R.id.profileTxt);
firebaseAuth = FirebaseAuth.getInstance();
//Bottom navigation
BottomNavigationView navigationView = findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(selectedListener);
//Home Fragment&#39;ı boş default fragment olarak ayarlanması
actionBar.setTitle(&quot;Home&quot;); //ActionBar başlığını değiştirme
HomeFragment homeFragment = new HomeFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content, homeFragment, &quot;&quot;);
ft.commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener selectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//itemlere tıklanma &#246;zelliği ekleme
switch (item.getItemId()){
case R.id.action_home:
//home fragment değişimi
actionBar.setTitle(&quot;Home&quot;); //ActionBar başlığını değiştirme
HomeFragment homeFragment = new HomeFragment();
FragmentTransaction ftHome = getSupportFragmentManager().beginTransaction();
ftHome.replace(R.id.content, homeFragment, &quot;&quot;);
ftHome.commit();
return true;
case R.id.action_map:
//map fragment değişimi
actionBar.setTitle(&quot;Map&quot;); //ActionBar başlığını değiştirme
MapFragment mapFragment = new MapFragment();
FragmentTransaction ftMap = getSupportFragmentManager().beginTransaction();
ftMap.replace(R.id.content, mapFragment, &quot;&quot;);
ftMap.commit();
return true;
case R.id.action_users:
//users fragment değişimi
actionBar.setTitle(&quot;Users&quot;); //ActionBar başlığını değiştirme
UsersFragment usersFragment = new UsersFragment();
FragmentTransaction ftUsers = getSupportFragmentManager().beginTransaction();
ftUsers.replace(R.id.content, usersFragment, &quot;&quot;);
ftUsers.commit();
return true;
case R.id.action_profile:
//profile fragment değişimi
actionBar.setTitle(&quot;Profile&quot;); //ActionBar başlığını değiştirme
ProfileFragment profileFragment = new ProfileFragment();
FragmentTransaction ftProfile = getSupportFragmentManager().beginTransaction();
ftProfile.replace(R.id.content, profileFragment, &quot;&quot;);
ftProfile.commit();
return true;
}
return false;
}
};
private void checkUserStatus() {
//mevcut kullanıcıyı al
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null){
//kullanıcı giriş yapmışsa burada kal
//giriş yapan kullanıcının email i
//profileTxt.setText(user.getEmail());
}
else{
//kullanıcı giriş yapmamışsa main activity&#39;e git
Intent intent = new Intent(DashboardActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
protected void onStart() {
//uygulamanın başlangıcını konrtol et
checkUserStatus();
super.onStart();
}
//options menu dahil etme
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//menuyu dahil etme
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
//menu itemlerine onClick &#246;zelliği aktifleştirme
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
//itemlerin id&#39;lerini al
int id = item.getItemId();
if (id == R.id.action_logout){
//hesaptan &#231;ıkış yap
firebaseAuth.signOut();
checkUserStatus();
}
return super.onOptionsItemSelected(item);
}
}

activity_dashboard.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.DashboardActivity&quot;&gt;
//FrameLayout fragmentlerin &#252;st&#252;ste gelebilmesi i&#231;in daha kullanışlıdır
&lt;FrameLayout
android:id=&quot;@+id/content&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;0dp&quot;&gt;
&lt;/FrameLayout&gt;
//BottomNavigationBar
&lt;com.google.android.material.bottomnavigation.BottomNavigationView
android:id=&quot;@+id/navigation&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentEnd=&quot;false&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:background=&quot;?android:attr/windowBackground&quot;
app:menu=&quot;@menu/bottombar_menu&quot;&gt;
&lt;/com.google.android.material.bottomnavigation.BottomNavigationView&gt;
&lt;/RelativeLayout&gt;

答案1

得分: 0

这是从fragment_profile.xml中显示的内容。

我的 fragment_profile.xml 不显示。我的程序有什么问题?

您的问题没有提供信息,无论您是否像上面那样获取了空视图,还是根本没有获取到任何视图。我将尝试根据两种假设进行回答。

  1. 空视图
  2. 根本没有视图

空视图

如果您获取到空视图,那么您必须在数据库响应中添加一些Logs语句,检查数据库是否返回了某些值。

public class ProfileFragment extends Fragment {

    //Log Statment
    private static final String TAG = "ProfileFragment";

    ......

    query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    //检查直到必要的数据到达
                    for (DataSnapshot ds: snapshot.getChildren()){
                        //获取数据
                        String name = ""+ ds.child("name").getValue();
                        String email = ""+ ds.child("email").getValue();
                        String phone = ""+ ds.child("phone").getValue();
                        String image = ""+ ds.child("image").getValue();

                        //设置数据
                        nameTxt.setText(name);
                        emailTxt.setText(email);
                        phoneTxt.setText(phone);
                        Log.d(TAG, "onDataChange: \nName: "+name+" email: "+ email);

                        try {
                            //如果存在图像,则设置图像
                            Picasso.get().load(image).into(avatar);
                        } catch (Exception e){
                            //如果在获取图像时出现问题,则默认设置
                            Picasso.get().load(R.drawable.add_photo_foreground).into(avatar);
                        }
                    }
                    Log.d(TAG, "onDataChange: Data is empty");
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    Log.d(TAG, "onCancelled: "+error);
                }
     }
     }
    .........

您还可以使用断点进行进一步调查。

根本没有视图

如果在ProfileFragment上视图没有改变,那么您必须检查您如何启动ProfileFragment。

getSupportFragmentManager().beginTransaction().replace(R.id.frgment_container, new ProfileFragment()).commit();

如果您使用导航组件,请确保您导航到正确的目标。

我还建议您在Profile Fragment下使用以下格式来构建代码:

public class ProfileFragment extends Fragment {

    //Firebase auth
    ............

    //Layout views
    ..............

    public ProfileFragment() {
        //必须有一个空的public构造函数
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //为此片段填充布局
        View view =  inflater.inflate(R.layout.fragment_profile, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //初始化Firebase
        ..................

        //初始化布局视图
        .....................

        /* 我们必须使用登录用户的信息来提取用户详细信息,使用电子邮件或UID
        我们将使用电子邮件地址提取用户详细信息
        通过使用orderbyChild查询,我们可以将登录电子邮件与电子邮件键匹配以获取用户详细信息
         */
        Query query = databaseReference.orderByChild("email").equalTo(user.getEmail());
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                .............
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                .............
            }
        });
    }
}

编辑(activity_dashboard.xml)

尝试以下布局文件以用于activity_dashboard.xml -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".DashboardActivity">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?attr/actionBarSize"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentEnd="false"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottombar_menu"/>

</RelativeLayout>

我使用了?attr/actionBarSize,这是Android的标准操作栏大小。
祝您编码愉快!

英文:

This is what is showing from fragment_profile.xml.

我的 fragment_profile.xml 不显示。我的程序有什么问题?

Your question doesn't give information whether you are getting an empty view like above or you are not getting any view. I will try to answer on two ansumption.

  1. Empty View
  2. No View at all

Empty View

If you are getting empty view then you must put some Logs statement in your database responce, check if that database is returning some value.

public class ProfileFragment extends Fragment {
//Log Statment
private static final String TAG = &quot;ProfileFragment&quot;;
......
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
//gerekli veriler gelene kadar kontrol et
for (DataSnapshot ds: snapshot.getChildren()){
//verileri almak i&#231;in
String name = &quot;&quot;+ ds.child(&quot;name&quot;).getValue();
String email = &quot;&quot;+ ds.child(&quot;email&quot;).getValue();
String phone = &quot;&quot;+ ds.child(&quot;phone&quot;).getValue();
String image = &quot;&quot;+ ds.child(&quot;image&quot;).getValue();
//set data
nameTxt.setText(name);
emailTxt.setText(email);
phoneTxt.setText(phone);
Log.d(TAG, &quot;onDataChange: \nName: &quot;+name+&quot; email: &quot;+ email);
try {
// resim alınırsa ayarla
Picasso.get().load(image).into(avatar);
} catch (Exception e){
// resim alınırken herangi bir sıkıntı varsa varsayılan olarak ayarla
Picasso.get().load(R.drawable.add_photo_foreground).into(avatar);
}
}
Log.d(TAG, &quot;onDataChange: Data is empty&quot;);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.d(TAG, &quot;onCancelled: &quot;+error);
}
}
}
.........

You can use breakpoints as well to further investigation.

No View at all

If view is not changing on ProileFragment then you must check how you are starting ProfileFragment.

getSupportFragmentManager().beginTransaction().replace(R.id.frgment_container, new ProfileFragment()).commit();

If you are using Navigation Component then make sure you are navigating to correct destination.

I will also suggest you to strcture the code in following format under Profile Fragment -

public class ProfileFragment extends Fragment {
//firebase auth
............
//layout views
..............
public ProfileFragment() {
//boş public constructor gerekli
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =  inflater.inflate(R.layout.fragment_profile, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//init firebase
.................
//init layout views
.......................
/* giriş yapan kullanıcıların bilgilerini email yada uid kullanarak &#231;ekmek zorundayız
Kullanıcı detaylarını email adreslerini kullanarak &#231;ekicez
orderbyChild query kullanarak giriş yapılan email ile email key ini eşleştirerek kullanıcı detaylarına ulaşılıyor
*/
Query query = databaseReference.orderByChild(&quot;email&quot;).equalTo(user.getEmail());
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
.................
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
...............
}
});
}
}

Edit (activity_dashboard.xml)

Try following layout file for activity_dashboard.xml -

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
tools:context=&quot;.DashboardActivity&quot;&gt;
&lt;FrameLayout
android:id=&quot;@+id/content&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_marginBottom=&quot;?attr/actionBarSize&quot;/&gt;
&lt;com.google.android.material.bottomnavigation.BottomNavigationView
android:id=&quot;@+id/navigation&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;?attr/actionBarSize&quot;
android:layout_alignParentEnd=&quot;false&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:background=&quot;?android:attr/windowBackground&quot;
app:menu=&quot;@menu/bottombar_menu&quot;/&gt;
&lt;/RelativeLayout&gt;

I am using ?attr/actionBarSize, which is the standard action bar size for android.
Happy Coding !

huangapple
  • 本文由 发表于 2020年8月7日 07:23:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63293073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定