我创建了一个FragmentOne extends Fragment
,我收到以下编译时错误:
"无法转换为片段!",
我正在关注此tutorial
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view){
Fragment fragment;
if(view == findViewById(R.id.button1)){
fragment = new FragmentOne();
}
}
}
答案 0 :(得分:3)
您正在使用AppcompatActivity。所以将片段导入替换为
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
而不是
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;