我有两个框架的活动:Navigator(作为菜单)和ContentFragment作为详细视图。当我单击导航器中的按钮时,我想替换内容框架中显示的片段。
首先,我在导航器中创建了单击侦听器:
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;
}
然后我在我的活动中创建了回调接口:
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();
}
}
在那个函数中我需要片段:
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)
答案 0 :(得分:1)
我自己解决了这个问题,它正在试图给我试图添加的布局充气,使用错误的线性布局(支持类)