gpt4 book ai didi

android - 膨胀 ListFragment 类时出错

转载 作者:行者123 更新时间:2023-11-30 00:48:53 25 4
gpt4 key购买 nike

我的应用程序一启动就崩溃了。我收到以下错误:

android.view.InflateException: Binary XML file line #34: Error inflating class

我已经查看了其他几个类似的问题,但没有一个解决方案适合我。为什么会这样?错误中的第 34 行指的是

<fragment>..</fragment> 

在 mainActivity.xml 中

MainActivity.java

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;




public class MainActivity extends AppCompatActivity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




}

public void FragmentOneClick(View v)
{
ListFragment myFragment=new fragmentOne();
FragmentManager fm= getSupportFragmentManager();//getFragmentManager();

FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_switch, myFragment );
ft.commit();

}

public void FragmentTwoClick(View v)
{
Fragment myFragment=new fragmentTwo();
FragmentManager fm= getSupportFragmentManager();

FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_switch, myFragment);
ft.commit();

}

}

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.nirvan.fragment_example.MainActivity">

<Button
android:onClick="FragmentOneClick"
android:text="Fragment1"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:layout_weight="2"
>
</Button>

<Button
android:text="Fragment2"
android:id="@+id/button2"
android:onClick="FragmentTwoClick"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:layout_alignTop="@+id/button1"
android:layout_alignRight="@+id/fragment_switch"
android:layout_alignEnd="@+id/fragment_switch">
android:layout_weight="2"
</Button>

<fragment
android:id="@+id/fragment_switch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_below="@+id/button1"
android:name="com.example.nirvan.fragment_example.fragmentOne"
tools:layout="@layout/fragment_fragment_one" />

</RelativeLayout>

FragmentOne.java

//import android.app.ListFragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
//import android.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.support.v4.app.ListFragment;

public class fragmentOne extends android.support.v4.app.ListFragment
{
String dates[]={"1","2","3","4","5","6","7","8","9"};




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView;
rootView=inflater.inflate(R.layout.fragment_fragment_one, container, false);



return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,dates);
setListAdapter(adapter);
}
}

FragmentTwo.java

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class fragmentTwo extends Fragment
{



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_two, container, false);
}


}

FragmentOne.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.nirvan.fragment_example.fragmentOne">

<!-- TODO: Update blank fragment layout -->

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_gravity="left|top" />


</FrameLayout>

fragmentTwo.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.nirvan.fragment_example.fragmentTwo"
android:background="#548f47">

<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment 2" />






</FrameLayout>

日志

 FATAL EXCEPTION: main
Process: com.example.nirvan.fragment_example, PID: 26870
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nirvan.fragment_example/com.example.nirvan.fragment_example.MainActivity}: android.view.InflateException: Binary XML file line #34: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.nirvan.fragment_example.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
at android.app.ActivityThread.access$800(ActivityThread.java:139) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5086) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
at android.app.ListFragment.ensureList(ListFragment.java:402)

最佳答案

用这个替换你的 FragmentOne.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">

<!-- TODO: Update blank fragment layout -->

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top" />


</FrameLayout>

这里只需要将ListView的id改成这个

 android:id="@android:id/list"

关于android - 膨胀 ListFragment 类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41365796/

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