[英]Why does my emulator not show anything despite no errors being shown?
I'm currently trying to figure out how to create a ListView
together with a bottom navigation bar, however, though after running the emulator it did not show anything.我目前正在尝试弄清楚如何与底部导航栏一起创建
ListView
,但是,虽然在运行模拟器后它没有显示任何内容。 Why is this so?为什么会这样?
This is my activity_main.xml file这是我的 activity_main.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
app:menu="@menu/home"
/>
</LinearLayout>
This is my nav_graph.xml file这是我的 nav_graph.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph.xml"
app:startDestination="@id/home">
<fragment
android:id="@+id/home"
android:name="sg.edu.rp.c346.a3pdwork.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/About"
android:name="sg.edu.rp.c346.a3pdwork.AboutFragment"
android:label="fragment_about"
tools:layout="@layout/fragment_about" />
<fragment
android:id="@+id/Login"
android:name="sg.edu.rp.c346.a3pdwork.LoginFragment"
android:label="fragment_login"
tools:layout="@layout/fragment_login" />
</navigation>
This is my home.xml file这是我的 home.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:title="Home"
android:icon="@drawable/ic_action_home"
/>
<item
android:id="@+id/Login"
android:title="Login"
android:icon="@drawable/ic_action_login"
/>
<item
android:id="@+id/About"
android:title="About"
android:icon="@drawable/ic_action_about"
/>
</menu>
This is my fragment_home.xml file这是我的 fragment_home.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<ListView
android:id="@+id/listViewDetails"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
This is my HomeFragment.java file这是我的 HomeFragment.java 文件
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
This is my fragment_login.xml file这是我的 fragment_login.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".LoginFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
This is my LoginFragment.java file这是我的 LoginFragment.java 文件
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class LoginFragment extends Fragment {
public LoginFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_login, container, false);
}
}
This is my fragment_about.xml file这是我的 fragment_about.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".AboutFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
This is my AboutFragment.java file这是我的 AboutFragment.java 文件
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class AboutFragment extends Fragment {
public AboutFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
}
}
This is my MainActivity.java file这是我的 MainActivity.java 文件
package sg.edu.rp.c346.a3pdwork;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavController navController = Navigation.findNavController(this,
R.id.nav_host_fragment);
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
NavigationUI.setupWithNavController(bottomNav, navController);
}
}
Ruchin.鲁钦。 Wish you many good things to come with android developing.
祝你在android开发中能有很多好的东西。
In this situation I've got 2 things to tell you:在这种情况下,我有两件事要告诉你:
First: Your BottomNavigationView
has no android:layout_width
and android:layout_height
attributes.第一:您的
BottomNavigationView
没有android:layout_width
和android:layout_height
属性。 I think you just forgot to put them in here (if so, it is not any problem, I just wanted to make sure you notice that).我想你只是忘了把它们放在这里(如果是这样,那没有问题,我只是想确保你注意到这一点)。
Second: Your layout is shown correctly.第二:您的布局显示正确。 Why you see nothing?
为什么什么都看不见? Because you have two views
TextView
and ListView
which have android:layout_height="match_parent"
.因为您有两个视图
TextView
和ListView
,它们具有android:layout_height="match_parent"
。 So at first TextView
is rendered and then ListView
is rendered on it.因此,首先呈现
TextView
,然后在其上呈现ListView
。
How can you see some results?你怎么能看到一些结果? That's simple.
这很简单。 Just change
FrameLayout
to LinearLayout
, set orientation
to vertical
and set TextView 's height to wrap_content
.只需将
FrameLayout
更改为LinearLayout
,将orientation
设置为vertical
并将TextView的高度设置为wrap_content
。 You can put these changes in your fragment_home.xml file:您可以将这些更改放在您的fragment_home.xml文件中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment" />
<ListView
android:id="@+id/listViewDetails"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.