gpt4 book ai didi

java - 在主要 Activity 中膨胀 fragment 会带来错误

转载 作者:行者123 更新时间:2023-11-29 18:27:04 26 4
gpt4 key购买 nike

我希望能够在我的主要 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/

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