在mainActivity中查看frameLayout内的片段

时间:2018-12-10 11:15:06

标签: java android android-fragments

我正在开发我的第一个android应用程序。 **

  • 我要做什么:

1-在主要活动的 frameLayout 中查看此空白片段:
fragment_map.xml (片段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=".MapFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

2-这样,FrameLayout在mainActivity内部:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/mapPlaceHolder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></FrameLayout>

</android.support.constraint.ConstraintLayout>

3-尝试使用功能 showMap()

将此片段动态添加到FrameLayout中

MainActivity类

public class MainActivity extends AppCompatActivity {

    boolean activityIsReady= false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    @Override
    protected void onResume() {
        super.onResume();
        activityIsReady = true;

    }
    @Override
    protected void onPostResume() {
        super.onPostResume();
        showMap();
    }

    void showMap(){
        if(activityIsReady == true) {
            FragmentTransaction fragmentTransaction =  getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.mapPlaceHolder, new MapFragment());
            fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        }
    }

}

4- MapFragment 代码(我刚刚创建了一个新的片段,并且该Java类是自动生成的)

public class MapFragment extends Fragment {
   // TODO: Rename parameter arguments, choose names that match
   // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public MapFragment() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment MapFragment.
 */
// TODO: Rename and change types and number of parameters
public static MapFragment newInstance(String param1, String param2) {
    MapFragment fragment = new MapFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_map, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

问题是:应用程序立即启动后崩溃。

我试图在OnCreate()函数中调用showMap()。但是,它尚未执行。

LogCast :我发现这些行是红色的,但是我没有使用 AudioFlinger

2018-12-10 13:23:25.747 1496-2985/? E/AudioFlinger: not enough memory for AudioTrack size=131296
2018-12-10 13:23:25.747 1496-2985/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
2018-12-10 13:23:25.748 2732-6423/? E/AudioRecord: AudioFlinger could not create record track, status: -12
2018-12-10 13:23:25.756 2732-6423/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
2018-12-10 13:23:25.756 2732-6423/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.

1 个答案:

答案 0 :(得分:1)

由于接口而发生此问题。您有两种选择。

(1)在您的OnFragmentInteractionListener中实施MainActivity。喜欢,

public class MainActivity extends AppCompatActivity implements OnFragmentInteractionListener {
   .......
   @Override
   void onFragmentInteraction(Uri uri){
       ....
   }
}

(2)如果您不需要进行接口,则从onAttach中删除侦听器代码。

@Override
public void onAttach(Context context) {
  super.onAttach(context);
  //remove code from here.
}