我使用android studio
在这段代码上出错了无法解析方法' add(int,android.trey.startingpoint.nl.SecondActivity.PlaceHolderFragment)'
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second, container, false);
return rootView;
}
}
在单独的帖子中,我发现当你删除if部分时,事情应该很好,除了它会引发另一个错误。
感谢您的帮助!
编辑:进口:
import android.app.Activity; import android.support.v4.app.Fragment;
导入android.os.Bundle; import android.view.LayoutInflater;进口 android.view.Menu; import android.view.MenuItem;进口 android.view.View; import android.view.ViewGroup;进口 android.widget.Toast;
导入com.google.android.gms.maps.CameraUpdateFactory;进口 com.google.android.gms.maps.GoogleMap;进口 com.google.android.gms.maps.MapFragment;进口 com.google.android.gms.maps.model.BitmapDescriptorFactory;进口 com.google.android.gms.maps.model.CameraPosition;进口 com.google.android.gms.maps.model.LatLng;进口 com.google.android.gms.maps.model.MarkerOptions;
答案 0 :(得分:3)
PlaceholderFragment可能是支持片段。
将getFragmentManager更改为getSupportFragmentManager(),如下所示:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
您的活动还必须扩展FragmentActivity。
注意:
如果您不打算使用支持片段,则只需更改片段的导入即可。
更改导入android.support.v4.app.Fragment;导入android.app.Fragment;
答案 1 :(得分:1)
试试这个:
change getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
to
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment()).commit();
答案 2 :(得分:0)
将import android.app.Fragment;
更改为android.support.v4.app.Fragment;
像
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
答案 3 :(得分:0)
在您的片段文件中
使用import android.support.v4.app.Fragment;
而不是import android.app.Fragment;
比能解决它;
以下是示例:
在您的活动文件中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_travel_message);
TravelMessageFragment fragment = new TravelMessageFragment();
getSupportFragmentManager().beginTransaction().add(R.id.travelInit,fragment).commit();
}
比你的片段文件:
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TravelMessageFragment extends Fragment{
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_travel_message, group, false);
return view;
}
}
答案 4 :(得分:-1)
无法解析方法'add(int,android.trey.startingpoint.nl.SecondActivity.PlaceHolderFragment)'
改变两件事
第一件事:
你可以创建类“android.trey.startingpoint.nl.SecondActivity.PlaceHolderFragment”的对象,而不是放入类名
像
if(findViewById(R.id.fragment_container) != null) {
if(savedInstanceState != null) {
return;
}
PlaceHolderFragment mFragment = new PlaceHolderFragment();
mFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,th
irdFragment).commit();
}
第二件事:
转到类文件PlaceHolderFragment并从“android.support.app.Fragment”更改导入“android.support.v4.app.Fragment”
答案 5 :(得分:-2)
将import android.support.v4.app.Fragment;
更改为import android.app.Fragment;
以及getSupportFragmentManager()
到
getFragmentManager
{
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
它对我有用