gpt4 book ai didi

android - 非法参数异常 : No view found for id 0x7f07003c for fragment

转载 作者:太空狗 更新时间:2023-10-29 16:05:08 26 4
gpt4 key购买 nike

我是 fragment 的新手。我尝试了简单的 fragment 示例。但它会抛出错误。我不知道出了什么问题。

public class NewActivity extends Activity {
int mStackLevel = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
Fragment newFragment = NewFragment
.newInstance(mStackLevel);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.simple_fragment, newFragment).commit();
} else {
mStackLevel = savedInstanceState.getInt("level");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("level", mStackLevel);
}
public static class NewFragment extends Fragment {
int mNum;
Context context;

static NewFragment newInstance(int num) {
NewFragment f = new NewFragment();

// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);

return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
context = getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.example_fragment, container,
false);
return v;
}
}

}

我的 logcat 显示:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.FragmentExample/com.example.FragmentExample.NewActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f07003c for fragment NewFragment{40ff0850 #0 id=0x7f07003c}

最佳答案

ft.add(R.id.simple_fragment, newFragment).commit();

您错过了 Activity 的 setContentView。如果没有 View 层次结构,则无法添加您的 fragment 。

注意:您的 fragment 由 Activity 托管。所以需要先给activity设置内容。

关于android - 非法参数异常 : No view found for id 0x7f07003c for fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17740440/

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