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 求解 本人初学不太熟练
...全文
1328 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
基本功能 2016/10/31 添加抽奖功能,修复已知Bug(点击删除购物,出现闪退bug) 2016/10/28 添加微信分享功能 2016/10/28 添加忘记密码功能、完善用户评价系统、以及完善订单页面按钮功能及显示 2016/10/27 添加自动更新功能,修复已知的登陆闪退Bug 2016/10/26 添加订单查询(全部订单、待付款、待发货、待收货、待评价、退款和售后等查询)以及显示功能,收藏宝贝查询、及显示功能 2016/10/25 添加加入购物车功能、移除购物车中的商品和下单之后更新购物车等功能 2016/10/24 添加评论功能的显示、登陆用户的收藏和取消收藏功能、下单页面的显示、以及下单功能的实现 2016/10/23 添加商品的类别显示、分类查询展示、固定数据的搜索(暂时数据固定) 2016/10/22 添加用户注销功能、头像的上传以及更新用户个人资料功能 2016/10/20 添加注册、登陆、加载默认头像等功能、解决ViewPager FragmentFragment被预加载问 2016/10/19 进行主页、微淘、问大家、购物车、我的淘宝等页面的设计、添加轮播图、资讯滚动条功能 2016/10/18 构建基本MVP框架 开发过程中遇到的问题(可能导致程序无法运行的bug)及解决方案 当用户未登录时,点击购物车,登陆之后,程序闪退 出现问题 :NullPointerException 解决方案: 使用Fragment的延时加载(懒加载)实现对数据的加载 拍照时无法进行图片的裁剪(不断加载) 解决步骤如下: Activity跳转时图库时的Intent如下 Intent takePhotoIntent = new Intent( “android.media.action.IMAGE_CAPTURE”); takePhotoIntent.putExtra( MediaStore.EXTRA_OUTPUT, imageUri); 在onActivityResult()方法中调用P层进行处理,相关代码如下 String uri = Environment.getExternalStorageDirectory() “/icoImage.jpg”; if(!allSelectedPicture.contains(uri)){ allSelectedPicture.add(uri); } ViewPager和Fragment结合使用,Fragment出现被预计载的情况 解决方案 项目中使用的主要技术及框架 框架 ButterKnife 注解绑定获取控件功能 Picasso 网络和本地图片的加载功能 okhttp 网络连接功能

80,471

社区成员

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

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