android.view.InflateException:二进制XML文件行#24:错误膨胀类片段

时间:2015-08-23 01:47:37

标签: android android-fragments fragment

我有一个有2个按钮的活动。我的活动中的片段会在按钮点击时更改。但我面临的问题是 - activity_main.xml。 以下是我的代码 -

<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:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="selectFrag" android:text="Select Fragment 1" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="selectFrag" android:text="Select Fragment 2" /> <fragment android:id="@+id/fragment_place" android:name="com.example.rahul.fragmentdemo.FragmentOne" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout="@layout/fragment_one" /> </LinearLayout>

MainActivity.java

package com.example.rahul.fragmentdemo; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void selectFrag(View view) { Fragment fr = null; if (view == findViewById(R.id.button2)) { fr = new FragmentTwo(); } else { fr = new FragmentOne(); } FragmentManager fm = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.replace(R.id.fragment_place, fr); fragmentTransaction.commit(); } }

FragmentOne.java

package com.example.rahul.fragmentdemo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FragmentOne extends Fragment { public FragmentOne() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { Log.i("Fragment Demo", "Fragment_One onCreate()"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_one, container, false); return view; } }

fragment_one.xml

<FrameLayout 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.rahul.fragmentdemo.FragmentOne"> <!-- TODO: Update blank fragment layout --> <TextView android:id="@+id/tvFragment_1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="This is Fragment 1" /> </FrameLayout>

FragmentOne.java

fragment_two.xmlFragmentTwo.java类似,我的代码分别有fragment_two.xmljava.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rahul.fragmentdemo/com.example.rahul.fragmentdemo.MainActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at android.view.LayoutInflater.inflate(LayoutInflater.java:365) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377) at android.app.Activity.setContentView(Activity.java:2144) at com.example.rahul.fragmentdemo.MainActivity.onCreate(MainActivity.java:18) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: android.support.v4.app.SuperNotCalledException: Fragment FragmentOne{3fdec5ae #0 id=0x7f0c0051} did not call through to super.onCreate() at android.support.v4.app.Fragment.performCreate(Fragment.java:1768) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116) at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218) at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)             at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)             at android.view.LayoutInflater.inflate(LayoutInflater.java:504)             at android.view.LayoutInflater.inflate(LayoutInflater.java:414)             at android.view.LayoutInflater.inflate(LayoutInflater.java:365)             at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)             at android.app.Activity.setContentView(Activity.java:2144)             at com.example.rahul.fragmentdemo.MainActivity.onCreate(MainActivity.java:18)             at android.app.Activity.performCreate(Activity.java:5937)             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我得到例外 -

MainActivity.java:18

- setContentView(R.layout.activity_main); 指的是

{{1}}

1 个答案:

答案 0 :(得分:1)

正如错误日志所述,a = 'cat' @a = 'dog' a #=> "cat" a = 'pig' @a #=> "dog" 中缺少super.onCreate

FragmentOne.java