将我的程序加载到设备上进行测试时,因为我使用底部导航打开不同的片段,所以没有任何事情发生。如果您需要我添加更多代码,请说明。我已经为主要活动发布了我的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>
非常感谢,
杰克
答案 0 :(得分:2)
啊哈,看起来像是在调用空的构造函数。
变化:
transaction.replace(R.id.mainFrame, new shareFragment()).commit();
到
transaction.replace(R.id.mainFrame, shareFragment.newInstance("arg 1","arg 2")).commit();
此外,java约定是大写的类名。 shareFragment - &gt; ShareFragment
祝你好运,快乐的编码!
答案 1 :(得分: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 :(得分:1)
您的根布局不应该是“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 :(得分:1)
为什么不尝试这样的事情:
transaction.replace(R.id.mainFrame, shareFragment().newInstance("param1", " param2")).commit();
因为默认构造函数没有返回任何Fragment()。