我在FragmentManager中调用新片段时遇到问题。 当我尝试在主要活动中调用片段时,我会收到此日志,但我不知道原因:
Process: com.robertot.timereport, PID: 31255
java.lang.IllegalArgumentException: No view found for id 0x7f07000a (com.robertot.timereport:id/content_frame) for fragment MyFragment{64c07710 #0 id=0x7f07000a}
这是我的MainActivity类:
public class MainActivity extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//...
}
private void LoadFragment(int position)
{
Fragment frgm = null;
FragmentManager fm = getSupportFragmentManager();
switch(position)
{
case 1:
frgm = new MyFragment();
fragmentTag = "newjob";
break;
}
fm.beginTransaction()
.replace(R.id.content_frame, frgm, fragmentTag) // CRASH HERE
.commit();
}
}
这是我的activity_main xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame">
</FrameLayout>
这是MyFragment类:
public class MyFragment extends Fragment
{
private CardListView listView;
private InterfaceProgressBar listenerProgrBar;
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
listenerProgrBar = (InterfaceProgressBar) activity;
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
return inflater.inflate(R.layout.summary,container,false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
listView = (CardListView) view.findViewById(R.id.cardlist);
}
@Override
public void onResume()
{
super.onResume();
//...do works
}
}
我做错了什么?
感谢您的支持!
答案 0 :(得分:0)
这是创建项目时的android示例:
public class MainActivity extends ActionBarActivity {
@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();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
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;
}
}
}
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.asdsd.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.asdsd.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
这应该是肯定的,我希望它能帮助你找到解决方案