我正在尝试单击按钮时获得简单的片段。但我收到InflateException,应用立即崩溃。在StackOverflow中找到了几个答案,没有人为我工作。希望任何人都可以帮助我进行解释。
MainActivity.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Line 16
button.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewfragment();
}
});
}
public void viewfragment()
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment1 fragment = new BlankFragment1();
fragmentTransaction.replace(R.id.fragment1, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
.......
......
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
/>
<Button>
......
<Button/>
</android.support.constraint.ConstraintLayout>
blank_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#654"
android:id="@+id/thisisfframent"
tools:context=".BlankFragment"
android:orientation="vertical" />
BlankFragment.class
public class BlankFragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.blank_fragment, container, false);
}
}
错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.berunda.audio.fragmentact, PID: 28350
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.berunda.audio.fragmentact/com.berunda.audio.fragmentact.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2638)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2720)
at android.app.ActivityThread.access$900(ActivityThread.java:196)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1538)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5982)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:561)
at android.view.LayoutInflater.inflate(LayoutInflater.java:437)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.berunda.audio.fragmentact.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6444)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1116)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2720)
at android.app.ActivityThread.access$900(ActivityThread.java:196)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1538)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5982)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:809)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:731)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:862)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:825)
at android.view.LayoutInflater.inflate(LayoutInflater.java:537)
at android.view.LayoutInflater.inflate(LayoutInflater.java:437)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.berunda.audio.fragmentact.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6444)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1116)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2720)
at android.app.ActivityThread.access$900(ActivityThread.java:196)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1538)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5982)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment .BlankFragment1: make sure class name exists, is public, and has an empty constructor that is public
at android.app.Fragment.instantiate(Fragment.java:632)
at android.app.Fragment.instantiate(Fragment.java:596)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2228)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:5751)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:389)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:781)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:731)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:862)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:825)
at android.view.LayoutInflater.inflate(LayoutInflater.java:537)
at android.view.LayoutInflater.inflate(LayoutInflater.java:437)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.berunda.audio.fragmentact.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6444)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1116)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2720)
at android.app.ActivityThread.access$900(ActivityThread.java:196)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1538)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5982)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
Caused by: java.lang.ClassNotFoundException: Didn't find class ".BlankFragment1" on path: DexPathList[[zip file "/data/app/com.berunda.audio.fragmentact-2/base.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.berunda.audio.fragmentact-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.berunda.audio.frag
谢谢。
答案 0 :(得分:0)
如果要在运行时添加片段,则需要将activity_main.xml更改为以下
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
.......
......
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
/>
<Button>
......
<Button/>
</android.support.constraint.ConstraintLayout>