gpt4 book ai didi

java - 未找到 fragment MyFragment id 0x 的 View

转载 作者:行者123 更新时间:2023-12-02 15:15:54 26 4
gpt4 key购买 nike

我在 FragmentManager 中调用新 fragment 时遇到问题。当我尝试在主 Activity 中调用 fragment 时,我收到此日志,但我不知道为什么:

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
}
}

我做错了什么?

感谢支持!

最佳答案

这是创建项目时的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>

这肯定有效,我希望它能帮助您找到解决方案

关于java - 未找到 fragment MyFragment id 0x 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24129619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com