gpt4 book ai didi

Android:从 fragment 中将 fragment 添加到 Activity

转载 作者:行者123 更新时间:2023-11-30 00:05:57 27 4
gpt4 key购买 nike

我得到了一个包含两个框架的 Activity :Navigator(作为菜单)和 ContentFragment 作为详细 View 。当我单击导航器中的按钮时,我想替换显示在内容框架中的 fragment 。

所以首先,我在我的导航器中创建我的点击监听器:

public class Navigator extends Fragment {
private Callbacks mCallbacks;
public interface Callbacks {
void onFunctionSelected(int functionID);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
mCallbacks = (Callbacks) getActivity();
}

@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parentVG, Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_navigator, parentVG, false);

mOsciloscope = (Button) v.findViewById(R.id.navigatorbutton_oscilloscope);
mOsciloscope.setText(R.string.navigator_oscilloscoop);
mOsciloscope.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Oscilloscope navigation button pressed
mCallbacks.onFunctionSelected(1);
}
});

return v;
}

然后我在我的 Activity 中创建回调接口(interface):

public class MainActivity extends AppCompatActivity
implements Navigator.Callbacks {

public void onFunctionSelected(int functionID) {
Fragment newContentFragment = ContentFragment.newInstance(functionID);

getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, newContentFragment)
.commit();
}
}

在那个函数中我需要 fragment :

public class ContentFragment extends Fragment {
private static final String ARGS_FUNCTION_ID = "functionID";

public static ContentFragment newInstance(int functionID) {
Bundle args = new Bundle();
args.putSerializable(ARGS_FUNCTION_ID, functionID);
ContentFragment fragment;

switch (functionID) {
case 1:
//fragment = new Oscilloscope();
fragment = new ContentFragment();
break;
default:
fragment = new ContentFragment();
break;
}

fragment.setArguments(args);
return fragment;
}
}

当我尝试运行此测试代码时,应用程序启动,它,当我单击按钮时,应用程序停止运行...知道我的错在哪里吗?

提前致谢。

编辑:logcat:

02-27 16:39:21.838 2356-2356/com.example.user.meetcombi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.meetcombi, PID: 2356
android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.constraint.LinearLayout
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.constraint.LinearLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.constraint.LinearLayout" on path: DexPathList[[zip file "/data/app/com.example.user.meetcombi-1/base.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.user.meetcombi-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.user.meetcombi-1/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:616)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at com.example.user.meetcombi.ContentFragment.onCreateView(ContentFragment.java:39)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2295)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1382)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1624)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1689)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2470)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2260)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2213)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2122)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:746)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

最佳答案

我自己解决了这个问题,它试图膨胀我试图添加的布局,使用了错误的线性布局(支持类)

关于Android:从 fragment 中将 fragment 添加到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012344/

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