以下是我的代碼,它給在更換片段部分跟它發現參數錯誤是在哪裏爲android.app.fragment需要android.support.v4.app.fragment告訴我什麼是錯誤在我的代碼,因爲我是新來這個所以不能得到這個問題做替換其他片段的片段給錯誤
package com.example.xainshah.fragmentexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.view.View;
import layout.Fragment1;
import layout.Fragment2;
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.button)){
fragment= new Fragment1();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_place,fragment); \\ Error is here 2nd argument is not compatible that's what the error says
}
if (view == findViewById(R.id.button)){
fragment= new Fragment2();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_place,fragment); \\ Error is here 2nd argument is not compatible that's what the error says
}
}
}
它的工作感謝的人 –