在mainActivity中查看frameLayout内的片段

loading...


-1

我正在研究我的第一个Android应用程序 . **

  • What I am trying to do:

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 class

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

The Problem is : the application crashes after it starts immediatly.

我试图在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回答

  • 1

    由于接口发生此问题 . 你有两个选择 .

    (1)MainActivity 中实施 OnFragmentInteractionListener . 喜欢,

    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.
    }
    
评论

loading...

暂时没有评论!