- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望能够在我的主要 Activity 类中的两个 fragment 之间进行交换。但是当膨胀 fragment 时,我的应用程序崩溃了。下面是我的代码:
这是放置我的 fragment 的 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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_dialog"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="56dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<fragment
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"/>
</LinearLayout>
这是我的 fragment 的代码
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
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.example.user.swim.AsyncTasks.GeoCodingTask;
public class SearchLocation extends Fragment {
private EditText destination;
private Button search;
private RecyclerView recyclerView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_search_locatiom, null);
search = view.findViewById(R.id.search_destination);
destination = view.findViewById(R.id.destination);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
searchLocation(view);
return view;
}
下面是我的主要 Activity 中用于膨胀布局的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.fragment_place)!=null){
if (savedInstanceState != null) {
return;
}
SearchLocation fragment = new SearchLocation();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_place, fragment).commit();
}
我的 logcat 中出现以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.swim, PID: 20462
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.swim/com.example.user.swim.MainActivity}: android.view.InflateException: Binary XML file line #29: Binary XML file line #14: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #29: Binary XML file line #14: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
Caused by: java.lang.NullPointerException
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Fragment.instantiate(Fragment.java:617)
at android.app.FragmentContainer.instantiate(FragmentContainer.java:49)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3598)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:6182)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:862)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.user.swim.MainActivity.onCreate(MainActivity.java:87)
at android.app.Activity.performCreate(Activity.java:6980)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是 fragment_search_locatiom 的 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_margin="10dp"
android:text="@string/where_would_you_like_to_go"
android:textAlignment="center" />
<EditText
android:id="@+id/destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="10dp"
android:drawablePadding="5dp"
android:drawableStart="@drawable/ic_search_black_24dp"
android:drawableLeft="@drawable/ic_search_black_24dp"
android:textSize="18sp"
android:inputType="text"
android:background="@drawable/rectangle"
android:hint="Search for destination"
android:maxLines="1"
android:imeOptions="actionDone"
android:textAlignment="textStart" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/search_destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/search_destination" />
</LinearLayout>
我不想在 fragment 的 XML 中添加名称/类属性,因为当您查看官方文档时,添加这些属性将无法在运行时更改 fragment 。
我也曾尝试更改 MainActivity 类以扩展 ActivityFragment,但经过更多研究后我发现 ActivityFragment 是 AppCompatActivity 的子类。
最佳答案
我已经复制了您的代码并尝试使用它,当我在 SearchLocation 中使用 fragment 时会出错,所以我将其更改为 Framlayout。无法真正分辨出您的代码中有什么问题,但是在我进行更改后这有效主要 Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_place) != null) {
if (savedInstanceState != null) {
return;
}
SearchLocation fragment = new SearchLocation();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_place, fragment).commit();
}
}
搜索位置java
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class SearchLocation extends Fragment {
private EditText mSearch;
private Button mDestination;
private RecyclerView mRecyclerView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search_location, null);
mSearch = view.findViewById(R.id.search_destination);
mDestination = view.findViewById(R.id.destination);
mSearch.setText("Hello world");
RecyclerView mRecyclerView = view.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
/*searchLocation(view);*/
return view;
}
搜索位置 xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".SearchLocation">
<EditText
android:id="@+id/search_destination"
android:layout_width="match_parent"
android:layout_height="0dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/destination"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_destination" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/destination" />
</androidx.constraintlayout.widget.ConstraintLayout>
关于java - 在主要 Activity 中膨胀 fragment 会带来错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257441/
我需要使用view.getWidth(),但我不能,因为我还没有膨胀 View 。在使用 view.getWidth() 之前如何膨胀 View ? MainActivity 类: p
这个问题很难尝试和表述,但我会尽力而为。 基本上,我有一个应用程序,我想将代码拆分得更多。为了尝试解释这一点,我将举一个我的屏幕示例。 在我的主屏幕上,我有一个标题、用户详细信息、余额、下一个账单详细
我有一个 TableLayout,我在其中动态添加行,一开始是空的,我想在用户单击它时在该行中加载一个 xml。 我已经给行赋值了OnClick方法,但是不知道在onclick方法中进入时如何加载xm
我使用的是 C++ 中的 libcurl 库,这是一个相当大的库(.lib 文件大约 2MB),项目是用 CURL_STATICLIB 编译的 至于现在我只有一个 .cpp 文件,其中包含 heade
背景: 我正在努力使一堆 PNG 尽可能小。我正在使用诸如 PngOut、PngCrush 和 OptiPng 之类的工具。 问题: 我遇到了一个大小为 1434 KB 但只有 230 x 230 像
我一直在努力找出我的应用程序使用的内存不断增长的原因,直到在 heroku 上的生产中它失败并下降。 经过一些相当广泛的研究,使用 Top 观察 RSIZE 在本地增长并使用 Oink 尝试查明我只是
我有一个使用 C++ 版本的 zlibs deflate 压缩的数据 ArrayBuffer(使用默认值)。由于没有服务器,我现在需要在客户端中增加这些数据。我已经使用 C++ 中的默认 inflat
我的代码有问题: @Override public View getView(int position, View convertView, ViewGroup parent) {
我有一个 fragment ,我像下面一样对其进行膨胀,但它给出了运行时错误: public View onCreateView(LayoutInflater inflater, ViewGroup
我正在学习 Fragment,并且我是通过在 Fragment 中使用 FB Login 来学习的。但我不断收到异常 Error inflateing class com.facebook.login
第一种方法: LinearLayout parent = ...; View child = LayoutInflator.inflate(context, parent, true); 第二种方法:
我刚刚完成了一个站点构建,在该站点构建中,我们必须将文件提供给第 3 方以与他们的系统集成。构建必须是独立的,因为会有多方部署它们,能力水平各不相同。 我遇到的问题是,在下载我的 Javascript
我正在寻找一种方法来“扩充”Android XML 布局,这在编译时是未知的。我已经搜索了几个小时,总能找到答案,这是不可能的,因为 LayoutInflater 不能使用简单的 XML 文件。好的,
我有一个从 Faragment 扩展而来的类,叫做 Agenda。它的布局有一个 listView,但是当这个类返回到主 Activity 时,我得到了一个错误。这是错误和代码: > 03-11 13
我的应用程序一启动就崩溃了。我收到以下错误: android.view.InflateException: Binary XML file line #34: Error inflating clas
我想膨胀 R.id.catText,但如果我自己膨胀它,它永远不会显示。如果我inflate R.id.assets(容器),那么两个元素都会正常显示。我只是不想要容器。如何在不膨胀 R.id.ass
我尝试创建自定义 ViewGroup 类,但是当我使用方法 findViewById() 时它返回 null,但展开 View 是可以的。 代码是: public class HorizontalLi
我正在尝试创建一个在屏幕上滑动的 textView,但它会产生错误,但我不知道为什么。这是我的 xml: 还有我的 Activity 课: package com.exampl
我正在构建一个应用程序,我在其中使用了 glomadrain 的动画切换按钮。完成编码后出现以下错误: at com.android.internal.os.ZygoteInit.main(Zygot
我只是想按照 android dev 上的示例代码来膨胀 Activity 中的 fragment 。我有一个更复杂的项目正在工作,但我遇到了与这里的简单项目相同的错误。谁能指出我正确的方向? 扩展
我是一名优秀的程序员,十分优秀!