package com.example.richu.fragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeFragment(View view){
Fragment fragment;
if(view == findViewById(R.id.button3)){
fragment = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment1,fragment);
ft.commit();
}
if(view == findViewById(R.id.button4)){
fragment = new FragmentTwo();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment1,fragment);
ft.commit();
}
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.example.richu.fragment.MainActivity">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="2dp"
tools:layout_editor_absoluteY="5dp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="292dp"
tools:layout_editor_absoluteY="5dp" />
<fragment
android:id="@+id/fragment1"
android:name="layout.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="13dp"
tools:layout_editor_absoluteY="13dp" />
</LinearLayout>
FragmentOne.java錯誤
package layout;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.richu.fragment.R;
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
}
FragmentOne.xml
<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"
android:background="@color/colorAccent"
tools:context="layout.FragmentOne">
</FrameLayout>
FragmentTwo.java
package layout;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.richu.fragment.R;
public class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_two, container, false);
}
}
FragmentTwo.xml
<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"
android:background="@color/colorAccent"
tools:context="layout.FragmentTwo">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Hello" />
</FrameLayout>
我在學習片段基礎知識。剛在活動中添加了兩個按鈕。當單擊一個按鈕時,應該加載FragmentOne,並在單擊第二個按鈕時加載FragmentTwo。 MainActivity.java
中的以下代碼顯示錯誤,並且錯誤爲不兼容的類型。 FragmentOne
和FragmentTwo
是我創建的兩個片段。 - fragment = new FragmentOne();
- fragment = new FragmentTwo();
請爲此找到解決方案。謝謝。