gpt4 book ai didi

java - android.view.InflateException : Not able to inflate the fragment

转载 作者:行者123 更新时间:2023-12-01 12:31:38 36 4
gpt4 key购买 nike

我正在 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 布局内膨胀 fragment

我收到的错误是:

    *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)

最佳答案

Caused by: java.lang.ClassCastException: com.stata.mobile.android.ui.MainHandlerFragment cannot be cast to android.support.v4.app.Fragment E/AndroidRuntime( 1347):

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

删除 import android.app.Fragment; 并在 MainHandlerFragment 类中添加 import android.support.v4.app.Fragment;

关于java - android.view.InflateException : Not able to inflate the fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25885273/

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