在主要活动中夸大片段会带来错误

时间:2019-10-06 12:55:01

标签: java android android-fragments

我希望能够在主活动类中的两个片段之间交换。但是,当增加片段时,我的应用程序崩溃了。下面是我的代码:

这是片段的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>

这是我的片段的代码

 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;
    }

下面是我主要活动中用于增加布局的代码

    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>

我不想在片段的XML中添加name / class属性,因为当您查看官方文档时,添加这些属性将无法在运行时更改片段。

我也尝试过更改MainActivity类以扩展ActivityFragment,但经过更多研究,我发现ActivityFragment是AppCompatActivity的子类。

2 个答案:

答案 0 :(得分:2)

当我在SearchLocation中使用片段时,我已经复制了您的代码并尝试使用它,这会导致错误,因此我将其更改为Framlayout。 无法真正分辨出代码中的错误,但这在我进行了更改后可以正常工作 MainActivity

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();
        }
    }

SearchLocation 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;

    }

search_location 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>

答案 1 :(得分:0)

您可以尝试使用Framelayout代替fragment,如下所示

<!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" />