Fragment无法显示 闪退

w6623282 2018-01-22 11:38:11
主activity:
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;


public class second extends AppCompatActivity{
private Fragment mHome;
private Fragment mShare;
private Fragment mCar;
private Fragment mMine;
private Fragment mMore;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
FragmentManager manager = getSupportFragmentManager();
//获取FragmentTransaction对象
final FragmentTransaction transaction = manager.beginTransaction();
//先隐藏所有的Fragment
hideFragments(transaction);
mHome= new homepage();
transaction.replace(R.id.contentContainer, mHome).commit();
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {
//单击事件 menuItemId 是 R.menu.bottombar_menu 中 item 的 id
switch (tabId) {
case R.id.home:
//如果微信对应的Fragment没有实例化,则进行实例化,并显示出来
if (mHome == null) {
mHome = new homepage();
transaction.add(R.id.contentContainer, mHome, "taghome").commit();
Toast.makeText(getApplicationContext(), "home", Toast.LENGTH_LONG).show();
} else {
//如果微信对应的Fragment已经实例化,则直接显示出来
transaction.show(mHome);
}
break;
case R.id.share:
if (mShare == null) {
mShare = new sharepage();
transaction.add(R.id.contentContainer, mShare, "tagshare").commit();
} else {
//如果微信对应的Fragment已经实例化,则直接显示出来
transaction.show(mShare);
}
case R.id.car:
if (mCar == null) {
mCar = new carpage();
transaction.add(R.id.contentContainer, mCar, "tagcar").commit();
} else {
//如果微信对应的Fragment已经实例化,则直接显示出来
transaction.show(mCar);
}
break;
case R.id.mine:
if (mMine == null) {
mMine = new minepage();
transaction.add(R.id.contentContainer, mMine, "tagmine").commit();
} else {
//如果微信对应的Fragment已经实例化,则直接显示出来
transaction.show(mMine);
}
break;
case R.id.morecar:
if (mMore == null) {
mMore = new morepage();
transaction.add(R.id.contentContainer, mMore, "tagmore").commit();
} else {
//如果微信对应的Fragment已经实例化,则直接显示出来
transaction.show(mMore);
}
break;

}
}
});
}
private void hideFragments(FragmentTransaction transaction) {
if (mHome != null) {
transaction.hide(mHome);
}
if (mCar != null) {
transaction.hide(mCar);
}
if (mShare != null) {
transaction.hide(mShare);
}
if (mMine != null) {
transaction.hide(mMine);
}
if (mMore != null) {
transaction.hide(mMore);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

}
}
Fragment java:
public class homepage extends Fragment{
@Nullable
@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View tab1view = inflater.inflate(R.layout.tab1, container, false);
return tab1view;
}
}
fragment xml
<fragment
android:id="@+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomBar"
/>
bottom插件xml:
<tab
android:id="@+id/home"
title="1"
android:icon="@drawable/home"
barColorWhenSelected="#FF1493" />
<tab
android:id="@+id/share"
android:icon="@drawable/lease"
title="2"
barColorWhenSelected="#00FF7F"
/>
<tab
android:id="@+id/car"
android:icon="@drawable/sale"
title="3"
barColorWhenSelected="#7B1FA2"
/>
<tab
android:id="@+id/mine"
android:icon="@drawable/mine"
title=“4”
barColorWhenSelected="#FF5252"
/>
<tab
android:id="@+id/morecar"
android:icon="@drawable/sale"
title="5"
barColorWhenSelected="#FF9800"
/>
</tabs>
自己还用了一个tab bottom插件 但是如果创建fragment实例便会闪退 去掉代码只显示插件 不显示fragment 求解 本人初学不太熟练
...全文
1214 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jing丶無雙 2018-01-24
  • 打赏
  • 举报
回复
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.g.newapp/com.example.g.newapp.second}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment 这个报错的关键提示我已经给你标红,这里的意思是无法加载你的second这个activity,因为你second的布局XML的存在引入fragment类型的错误。 Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment Caused by: java.lang.NullPointerException 这最终导致了空指针异常,因此看来你需要检查你的XML引入fragment是否存在
usecf 2018-01-24
  • 打赏
  • 举报
回复
看log报的空指针错误 你把xml也帖出来 顺便检查下你的代码 1.有没有导入这个包import android.support.v4.app.FragmentActivity; 2. 有没有继承这个 extends FragmentActivity
w6623282 2018-01-24
  • 打赏
  • 举报
回复
引用 12 楼 xj396282771 的回复:
http://www.cnblogs.com/linfenghp/p/5561023.html你得学习下动态添加Fragment
ok了 。。。不知道为什么看别的教程布局文件里少一个fragment还是FrameLayout。。 。一直没弄明白是干嘛的 谢谢了
Jing丶無雙 2018-01-24
  • 打赏
  • 举报
回复
http://www.cnblogs.com/linfenghp/p/5561023.html你得学习下动态添加Fragment
w6623282 2018-01-24
  • 打赏
  • 举报
回复
引用 10 楼 xj396282771 的回复:
你把这个 <fragment android:id="@+id/contentContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottomBar" /> 换成这个 <FrameLayout android:id="@+id/contentContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottomBar" > </FrameLayout>
这样可以正常打开但是不显示内容 空白
Jing丶無雙 2018-01-24
  • 打赏
  • 举报
回复
你把这个 <fragment android:id="@+id/contentContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottomBar" /> 换成这个 <FrameLayout android:id="@+id/contentContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottomBar" > </FrameLayout>
w6623282 2018-01-24
  • 打赏
  • 举报
回复
引用 4 楼 xj396282771 的回复:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.g.newapp/com.example.g.newapp.second}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment 这个报错的关键提示我已经给你标红,这里的意思是无法加载你的second这个activity,因为你second的布局XML的存在引入fragment类型的错误。 Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment Caused by: java.lang.NullPointerException 这最终导致了空指针异常,因此看来你需要检查你的XML引入fragment是否存在
头疼 我看不出哪里不对呀 xml我都贴出来了 求帮忙看下
w6623282 2018-01-24
  • 打赏
  • 举报
回复
引用 6 楼 usecf 的回复:
Android Fragment是Android4.0以上才有的;而FragmentActivity是为了兼容4.0以下版本的Fragment使用的。 所以如果你想兼容4.0以下Android版本使用Fragment的话,框架Activity需要继承FragmentActivity,FragmentActivity这个类是在android.support.v4.app.FragmentActivity里的。
我找不到哪里有错啊 就是闪退 我是还做了一个登录的activity然后点击登陆进入second activity 点完登陆就闪退了。。。
w6623282 2018-01-24
  • 打赏
  • 举报
回复
引用 6 楼 usecf 的回复:
Android Fragment是Android4.0以上才有的;而FragmentActivity是为了兼容4.0以下版本的Fragment使用的。 所以如果你想兼容4.0以下Android版本使用Fragment的话,框架Activity需要继承FragmentActivity,FragmentActivity这个类是在android.support.v4.app.FragmentActivity里的。
tab1 xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="30sp" android:text="The Weixin Tab!" /> </LinearLayout>
w6623282 2018-01-24
  • 打赏
  • 举报
回复
引用 3 楼 usecf 的回复:
看log报的空指针错误 你把xml也帖出来 顺便检查下你的代码 1.有没有导入这个包import android.support.v4.app.FragmentActivity; 2. 有没有继承这个 extends FragmentActivity
second 布局xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.g.newapp.second"> <fragment android:id="@+id/contentContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottomBar" /> <com.roughike.bottombar.BottomBar android:id="@+id/bottomBar" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" app:bb_inActiveTabColor="#222222" app:bb_activeTabColor="@color/white" app:bb_tabXmlResource="@menu/bottombar_menu" /> </RelativeLayout> 导入的是 android.support.v4.app.Fragment;和FragmentActivity有区别么
usecf 2018-01-24
  • 打赏
  • 举报
回复
Android Fragment是Android4.0以上才有的;而FragmentActivity是为了兼容4.0以下版本的Fragment使用的。 所以如果你想兼容4.0以下Android版本使用Fragment的话,框架Activity需要继承FragmentActivity,FragmentActivity这个类是在android.support.v4.app.FragmentActivity里的。
w6623282 2018-01-23
  • 打赏
  • 举报
回复
引用 1 楼 usecf 的回复:
闪退 看看报什么错误 抓下log
01-23 14:54:39.216 18423-18423/com.example.g.newapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.g.newapp, PID: 18423 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.g.newapp/com.example.g.newapp.second}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment Caused by: java.lang.NullPointerException at java.lang.VMClassLoader.findLoadedClass(Native Method) at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738) at java.lang.ClassLoader.loadClass(ClassLoader.java:363) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:499) at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3637) at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111) at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338) at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.example.g.newapp.second.onCreate(second.java:26) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 先谢谢大神了 好几天也解决不了
usecf 2018-01-23
  • 打赏
  • 举报
回复
闪退 看看报什么错误 抓下log

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧