//这种错误只是在使用support-v4的时候出现,而使用android.app的时候就不会出错
//由于我使用的Android Studio 所以build.gradle中已经添加了appcompat-v7库的依赖
//会自动引入support-v4;
package com.example.myapplication;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replaceFragment(new RightFragment());
//在实例化RightFragment()的时候报错
Button button = (Button)findViewById(R.id.button_1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1;
replaceFragment(new RightFragment2());
//在实例化RightFragment2()的时候报错
break;
default:
break;
}
}
});
}
//replaceFragment方法
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_fragment,fragment);
transaction.commit();
}
}由于是新手有些地方可能错误可能很明显还请口下留情
添加回答
举报
0/150
提交
取消