android :fragmentation使用中遇到的 NullPointerException

背景:fragmentation(单ACTIVITY+多个fragments)+brvah(  recyclerView多级自定义菜单功能)

目的:实现  菜单栏的点击,fragment 显示相应的内容

============================================

mainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".demo_flow.MainActivity">
    </FrameLayout>


    <RelativeLayout
        android:id="@+id/menu_layout_left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="@drawable/menu_bg"
        android:layout_marginRight="10dp"
        android:layout_marginTop="57dp" >
        
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

遇到的问题:

1、因为菜单点击显示 fragment

  在菜单的adapter 中要实现对fragment的 控制,

     我的实现方式:           回调函数

     (1)菜单的adapter中的部分代码:

           


private
SubClickListener subClickListener; public void setsubClickListener(SubClickListener topicClickListener) { this.subClickListener = topicClickListener; }
//接口,希望在mainActivity中实现
public interface SubClickListener { void OntopicClickListener(View v, int position); }
protected void convert(final BaseViewHolder holder, final MultiItemEntity item) {
switch (holder.getItemViewType()) {
            case TYPE_LEVEL_0:
                holder.setImageResource(R.id.iv_head, R.mipmap.head_img1);
                final Level0Item lv0 = (Level0Item) item;
                int subTitle = Integer.parseInt(lv0.subTitle.split("_")[0]);
                int itemID = Integer.parseInt(lv0.subTitle.split("_")[1]);
                switch (subTitle)
                {
                    case 1:
                        holder.setImageResource(R.id.iv_head,R.mipmap.head_img0);
                        break;
                    case 2:
                        holder.setImageResource(R.id.iv_head,R.mipmap.head_img1);
                        break;
                    case 3:
                        holder.setImageResource(R.id.iv_head,R.mipmap.head_img2);
                        break;
                    case 4:
                        holder.setImageResource(R.id.iv_head,R.mipmap.head_img2);
                        break;
                    case 5:
                        holder.setImageResource(R.id.iv_head,R.mipmap.head_img2);
                        break;

                }
                //if(subTitle=="1") System.out.println("---------------------level--------------------");
                //System.out.println("---------------------level-int-------------------");
                String name = lv0.title;
                if(name.indexOf(".doc")>0)
                {
                    holder.setTypeface(R.id.title, Typeface.SANS_SERIF);
                    holder.setImageResource(R.id.iv_head,R.mipmap.head_img_1);
                }
                holder.setText(R.id.title, lv0.title)
                        .setText(R.id.sub_title, lv0.subTitle)
                        .setImageResource(R.id.iv, lv0.isExpanded() ? R.mipmap.arrow_b : R.mipmap.arrow_r);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();
                        if (subClickListener != null) {
                            subClickListener.OntopicClickListener(v,pos);
                        }
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
               //此处省略其他分支……
        }
    }

  (2)mainActivity.java

  结构如下:

 

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

        MySupportFragment fragment = findFragment(HomeFragment.class);
        if (fragment == null) {
            loadRootFragment(R.id.fl_container, HomeFragment.newInstance());
        }
        initView();
    }
final ISupportFragment topFragment = getTopFragment();
private void initView() {
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
//        mDrawer.setDrawerListener(toggle);
        toggle.syncState();

        //added by ssqhan
        mRecyclerView = (RecyclerView)findViewById(R.id.rv);
        list=generateData();
        adapter=new ExpandableItemAdapter(list);
        adapter.setsubClickListener(new ExpandableItemAdapter.SubClickListener() {
            @Override
            public void OntopicClickListener(View v, int position) {  //实现菜单中adapter回调函数
                TextView tv=(TextView)v.findViewById(R.id.title);
                final String wbs_name = (String) tv.getText();
                boolean flag = (tv.getText().toString().indexOf(".doc")>0);
                if(flag)
                {
                    mDrawer.closeDrawer(GravityCompat.START);
                    mDrawer.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SartFragment(wbs_name);
                    }
                }, 300);
                }
            }
        });
        //adapter.get
        manager = new LinearLayoutManager(this);

        mRecyclerView.setAdapter(adapter);
        //important! setLayoutManager should be called after setAdapter
        mRecyclerView.setLayoutManager(manager);
        adapter.expandAll();
}
public void SartFragment(String tableName)
    {
        HomeFragment fragment=null;
        DiscoverFragment discoverFragment0=null;
        Bundle newBundle = new Bundle();
        System.out.println("---------------discovery---------------");

        switch (tableName) {
            case "避雷器实验报告.doc":
                fragment = findFragment(HomeFragment.class);
                newBundle.putString("from", "From:" + topFragment.getClass().getSimpleName());
                fragment.putNewBundle(newBundle);
                start(fragment, SupportFragment.SINGLETASK);
                break;
            case "电缆头施工记录.doc":
                discoverFragment0 = findFragment(DiscoverFragment.class);
                if (discoverFragment0 == null) {
                    popTo(HomeFragment.class, false, new Runnable() {
                        @Override
                        public void run() {
                            start(DiscoverFragment.newInstance());
                            Bundle myBundle = new Bundle();
                            DiscoverFragment discoverFragment=null;
                            if(topFragment==null) {
                   //这个地方,发现 topFragment 是空一直都是空 System.out.println(
"---------------------------------------"); System.out.println("----------topFragment is null----------"); System.out.println("---------------------------------------");
                    //loadRootFragment(R.id.fl_container, HomeFragment.newInstance());
} myBundle.putString(
"from", "From:" + topFragment.getClass().getSimpleName()); discoverFragment.putNewBundle(myBundle); start(discoverFragment, SupportFragment.SINGLETASK); } }); } else { newBundle.putString("from", "From:" + topFragment.getClass().getSimpleName()); discoverFragment0.putNewBundle(newBundle); start(discoverFragment0, SupportFragment.SINGLETASK); } break; } }

问题:如何解决,这个一直为空???

     

转载于:https://www.cnblogs.com/ssqhan/p/8010588.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值