片段oncreate方法中的null容器

时间:2016-03-03 16:23:44

标签: java android

我正试图在另一个活动中显示我的片段。起初一切都很好但是在编辑代码之后我的片段没有显示出来。这是我的主要活动内容XML代码:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.card_front, container, false);
    for (int i = 0; i < imgs.size(); i++) {
        CatModel catModel = new CatModel();
        catModel.txt = cat[i];
        catModel.imgid = imgs.get(i);
        arrayList.add(catModel);
    }
    recyclerView = (RecyclerView) inflater.inflate(R.layout.card_front, container, false).findViewById(R.id.recycler);
    StaggeredGridLayoutManager gridLayoutManager =
            new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(gridLayoutManager);
    recyclerView.setHasFixedSize(true);
    catAdapter = new CatAdapter(arrayList);
    recyclerView.setAdapter(catAdapter);
    return view;
}

片段类:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    }
    else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

主要活动类:

df1 = df1.reset_index()
pd.melt(df1, id_vars='index', value_vars=df1.columns.tolist()[1:])

}

当我调试我的应用程序时,片段类中的容器为null。为什么会这样?

2 个答案:

答案 0 :(得分:1)

当您应该使用之前已经充气并已分配给my $pattern = qr/(\d{2}):(\d{2})(?::(\d{2}))?/; 的布局时,您将从新增加的布局中分配recyclverView

这意味着您要返回的视图仍为空view。要解决此更改:

recyclerView

要:

recyclerView = (RecyclerView) inflater.inflate(R.layout.card_front, container, false).findViewById(R.id.recycler);

以便您使用与您要返回的视图相关联的recyclerView = (RecyclerView) view.findViewById(R.id.recycler);

答案 1 :(得分:1)

我在片段类中发现我的问题是With CTE_Values as ( Select distinct ClassID, SpecHours From MyTable ) Select Sum(SpecHours) From CTE_Values 。我改变了这个:

  public class SoftAsert
{
@Test
public void test()
{
    SoftAssert asert=new SoftAssert();
    asert.assertEquals(false, true,"failed");
    asert.assertEquals(0, 1,"brokedown");
    asert.assertAll();
}
}

到此:

findviewbyid