我收到错误
无法解析方法'添加 fragmentTransaction.add(R.id.fragment_container中,f1);
当我试图运行该应用程序时,它说:
遗憾的是,您的申请已停止
代码:
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity{
Button bn;
boolean status = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn = (Button) findViewById(R.id.bn);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager =getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(!status)
{
FragmentOne f1 = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container,f1);
fragmentTransaction.commit();
bn.setText("LOAD SECOND FRAGMENT");
status = true;
}
else
{
FragmentTwo f2 = new FragmentTwo();
fragmentTransaction.add(R.id.fragment_container,f2);
fragmentTransaction.commit();
bn.setText("LOAD FIRST FRAGMENT");
status = false;
}
}
});
}
}
答案 0 :(得分:0)
" MainActivity.java"你的片段的java必须
使用相同的 import android.app.Fragment;要么 import android.support.v4.app.Fragment;