android.view.InflateException:无法对片段进行膨胀

时间:2014-09-17 08:01:38

标签: java android android-fragments android-asynctask

我在Fragment类中编写了一个asyncTask代码,但我无法使其正常工作。相反的应用程序正在抛出InflateException。

以下是我的代码:

 public class MainActivity extends FragmentActivity{
  @Override
  protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    MainHandlerFragment firstFragment = new MainHandlerFragment();
    firstFragment.new MyTask().execute(query);
  }

MainHandlerFragment.java

 public class MainHandlerFragment extends Fragment{

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

 class MyTask extends AsyncTask<String, Void, String> {
    @Override
    protected Organization doInBackground(String... urls) {
       // AN Api call i am making here
    }

  }

activity_home.xml

 <?xml version="1.0" encoding="utf-8"?>

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <fragment
     android:id="@+id/home_detail"
     android:name="com.stata.mobile.android.ui.MainHandlerFragment"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"/>

   </FrameLayout>

home_detail.xml

   <Linearlayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

   <TextView
    android:id="@+id/home_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
   </LinearLayout>

我无法在MainActivity布局中夸大片段

我得到的错误是:

    *java.lang.RuntimeException:* Unable to start activity ComponentInfo{com.stata.mobile.android/com.stata.mobile.android.ui.MainActivity}: *android.view.InflateException*: Binary XML file line #9: Error inflating class fragment
E/AndroidRuntime( 1292):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
E/AndroidRuntime( 1292):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
E/AndroidRuntime( 1292):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime( 1292):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime( 1292):    at android.os.Handler.dispatchMessage(Handler.java:102)

E/AndroidRuntime( 1292): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
E/AndroidRuntime( 1292):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)


 *Caused by: java.lang.ClassCastException:* com.stata.mobile.android.ui.MainHandlerFragment cannot be cast to android.support.v4.app.Fragment
E/AndroidRuntime( 1347):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
E/AndroidRuntime( 1347):    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
E/AndroidRuntime( 1347):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)

1 个答案:

答案 0 :(得分:1)

  

引起:java.lang.ClassCastException:   com.stata.mobile.android.ui.MainHandlerFragment无法转换为   android.support.v4.app.Fragment E / AndroidRuntime(1347):

您必须导入support片段库并使用它并删除import android.fragment库。

删除import android.app.Fragment;并在import android.support.v4.app.Fragment;班级

中添加MainHandlerFragment