这里有趣的问题。我有两个相同的Fragments
和他们都import android.support.v4.app.Fragment
,但是当我尝试在他们上执行FragmentTransaction.replace()
编译器接受MyFrag2但在MyFrag1上给出错误。片段导入问题与FragmentTransaction.replace搞不清
我知道是什么造成它,但我不知道为什么。如果我将MyFrag1中的导入从android.support.v4.app.Fragment
更改为android.app.Fragment
,则错误消失。如果我在这两个类上导入了android.app.Fragment
,则会出现错误,并且MyFrag2上出现错误。就片段的设置而言,片段类是相同的。是什么赋予了?
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class SettingsFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_fragmentactivity);
if(findViewById(R.id.fragment_container) != null) {
if(savedInstanceState != null) return;
MyFrag1 frag = new MyFrag1();
android.app.FragmentTransaction transaction;
transaction = getFragmentManager().beginTransaction();
//ERROR ON THIS LINE
//"replace (int, android.app.Fragment) to (int, MyFrag1)"
transaction.replace(R.id.fragment_container, frag)
transaction.addToBackStack(null);
transaction.commit();
}
}
public void someOtherMethod(long id) {
MyFrag2 frag = new MyFrag2();
Bundle args = new Bundle();
args.putLong("id", id);
frag.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//NO ERROR!? The compiler happily accepts my offering.
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
}
MyFrag1
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
public class MyFrag1 extends Fragment {
private ListView listView;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
...
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.frag1_layout, container, false);
...//setup listadapter etc.
return view;
}
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
//setContentView(R.layout.settings_devicelist);
}
...
}
MyFrag2
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
public class MyFrag2 extends Fragment {
ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.frag2_layout, container, false);
...//setup listadapter etc.
return view;
}
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
//setContentView(R.layout.settings_nodedmxdevice);
}
...
}
做到了!认为这件事很简单 – bwoogie 2015-02-05 18:11:47