Fragment Intermediate (II):Dynmically adding row in a fragment, Android

╄→гoц情女王★ 提交于 2019-12-02 05:44:16
Raghunandan

Change this

TableLayout tl=(TableLayout) getActivity().findViewById(R.id.mainLayout);

to

TableLayout tl=(TableLayout) view.findViewById(R.id.mainLayout);

This is one mistake which avoids NullPointerException.

And use getActivity() to initialize views. To know why read

When to call activity context OR application context?

Matej Špilár

I had the same issue, and it was because I passed the application context instead of the activity context.

        TableRow tr = new TableRow(getActivity());

        TextView textview1 = new TextView(getActivity());
        textview1.setText("unhappy");
        tr.addView(textview1);

        TextView textview2 = new TextView(getActivity());
        textview2.setText("happy");
        tr.addView(textview2);

also you should read trought this

Android: How to track down the origin of a InflateException?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!