【问题标题】:Android Studio Fragments not loadingAndroid Studio 片段未加载
【发布时间】:2018-08-14 20:49:09
【问题描述】:

在设备上加载我的程序进行测试时,当我使用底部导航打开不同的片段时,什么都没有发生。如果您需要我添加更多代码,请说出来。我已经为 Main Activity 发布了我的 XML 和 Java,并且 XML 包含我的布局的 mainFrame。

public class MainActivity extends AppCompatActivity {

private TextView mTextMessage;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (item.getItemId()) {
            case R.id.shareFrame:
                transaction.replace(R.id.mainFrame, new shareFragment()).commit();
                mTextMessage.setText(R.string.title_share);
                return true;
            case R.id.settingsFrame:
                transaction.replace(R.id.mainFrame, new settingsFragment()).commit();
                mTextMessage.setText(R.string.title_settings);
                return true;
            case R.id.helpFrame:
                transaction.replace(R.id.mainFrame, new helpFragment()).commit();
                mTextMessage.setText(R.string.title_help);
                return true;
        }
        return false;
    }
};

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

    mTextMessage = (TextView) findViewById(R.id.message);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.mainFrame, new shareFragment()).commit();
    }

}

这是我的 shareFragment.java 的 Java

public class shareFragment 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 shareFragment() {
    // 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 shareFragment.
 */
// TODO: Rename and change types and number of parameters
public static shareFragment newInstance(String param1, String param2) {
    shareFragment fragment = new shareFragment();
    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_share, 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 {
        Toast.makeText(context, "Share", Toast.LENGTH_SHORT).show();
    }
}

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

这里是 XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/shareFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.swapdrop.swapdrop.shareFragment">

</FrameLayout>

【问题讨论】:

  • 发布您的布局
  • 仅用于我的 MainActivity?我还有 3 个其他片段
  • 以包含主框架布局的为准
  • 完成,抱歉,耽搁了一段时间
  • 你能只发布你的片段代码吗?

标签: android android-studio android-fragments


【解决方案1】:

尝试用这个改变你的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.swapdrop.swapdrop.MainActivity"
    android:background="#d3d3d3">

    <FrameLayout
        android:id="@+id/mainFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

现在mainFrame 是 FrameLayout 而不是父布局

【讨论】:

  • 试过了,变化不大,还是谢谢你的帮助。
  • 抱歉,还是没有区别
【解决方案2】:

您的根布局不应该是“mainFrame”。您应该有一个子 ViewGroup 来包含该片段。像这样的:

<RelativeLayout 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="match_parent">

    <FrameLayout
        android:id="@+id/mainFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:background="#d3d3d3" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

</RelativeLayout>

【讨论】:

    【解决方案3】:

    看起来你调用的是空的构造函数。

    变化:

    transaction.replace(R.id.mainFrame, new shareFragment()).commit();
    

    transaction.replace(R.id.mainFrame, shareFragment.newInstance("arg 1","arg 2")).commit();
    

    此外,java 约定是大写的类名。 shareFragment -> ShareFragment

    【讨论】:

      【解决方案4】:

      为什么不试试类似的东西:

      transaction.replace(R.id.mainFrame, shareFragment().newInstance("param1", " param2")).commit();
      

      因为默认构造函数没有返回任何 Fragment()。

      【讨论】:

        猜你喜欢
        • 2020-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-07
        • 2015-06-16
        • 1970-01-01
        • 2012-11-25
        相关资源
        最近更新 更多