java.lang.IllegalStateException:在另一个片段内的Calling片段中,Activity已被销毁,以替换framelayout

时间:2015-11-04 12:39:34

标签: android android-fragments

我是新手android和学习片段而不是多个,我有问题。当我从朋友那里调用行星片段" java.lang.IllegalStateException:Activity已被销毁" android运行时发生的FATAL异常是我的代码。我看到了this页面,但无法弄清楚如何使用它。我将不胜感激任何帮助

fragment_planet.xml

        <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center"
    android:padding="32dp" />

Friends.java

    package com.app.hubara;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Friends extends Fragment {

    public Friends() { }

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.content_friends, container, false);

        MainActivity main = new MainActivity();
        main.friends();

        return rootView;
    }


}

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="100dp"/>

    <Button
        android:id="@+id/display"
        android:height="70dp"
        android:width="300dp"
        android:layout_marginTop="0dp"
        android:text="Display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity

    package com.app.hubara;

import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import java.util.Locale;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.display);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Friends frag_friends = new Friends();
                fragmentManager = getFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.content_frame, frag_friends);
                fragmentTransaction.commit();
            }
        });

    }

    public void friends(){
        PlanetFragment planetFragment = new PlanetFragment();
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, planetFragment);
        fragmentTransaction.commit();
    }


    public static class PlanetFragment extends Fragment {

        public PlanetFragment() { }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
            int i = getArguments().getInt("Earth");
            String planet = getResources().getStringArray(R.array.planets_array)[i];
            int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                    "drawable", "Earth");
            ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
            getActivity().setTitle(planet);
            return rootView;
        }

    }


}

1 个答案:

答案 0 :(得分:3)

要访问活动,您不应该这样做:

MainActivity main = new MainActivity();
main.friends();

因为这会破坏当前的Activity,然后实例化另一个新的Activity。

您可以致电getActivity()

来正确访问它
MainActivity main = getActivity();
main.friends();

请注意,实际上建议您定义并使用接口在Fragment和Activity之间进行通信。如果您想进一步了解,请参阅the official training