Я очень новичок в разработке Android. Я просмотрел несколько руководств по фрагментам, но по какой-то причине не могу добавить фрагмент из моего основного вида. Я не пытаюсь сделать что-нибудь мучительно сложное. Все, что я хочу, - это создавать свои фрагменты и добавлять их в мою основную деятельность. Вот и все. Но каждый раз, когда я запускаю свое приложение, я получаю только пустой экран. Я отлаживаю это, и это собирается к фрагменту. Это наводит меня на мысль, что проблема может быть в файле XML Laout (возможно).
Вот мой MainActivity.java
package com.android.myCompany.nameOfApp;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.foo_frame_layout, new FooFragment());
ft.commit();
}
}
Вот мой Fragment.cs
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class FooFragment extends Fragment {
public FooFragment () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText(R.string.hello_blank_fragment);
return textView;
}
}
Вот мой макет activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity"
android:background="@drawable/background_portrait">
<FrameLayout
android:id="@+id/copyright_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
Вот мой фрагмент_foo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/copyright_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FooFragment">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="102dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="false"
android:cropToPadding="false"
app:layout_constraintBottom_toTopOf="@+id/imageView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
<ImageView
</RelativeLayout>
Может кто-нибудь сказать мне, что не так с моей настройкой? Большое спасибо заранее.