我有一个导航抽屉活动,我附加了不同的片段
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, rp_fragment.OnFragmentInteractionListener, sensors_fragment.OnFragmentInteractionListener, stats_fragment.OnFragmentInteractionListener {
我试图修改片段中包含的textview的text属性但是我一直在视图上得到一个空指针异常(java.lang.NullPointerException:尝试调用虚方法' void com .teicrete.alucard.sempi.sensors_fragment.ctv_tvst(int)'在空对象引用上。)
Mainactivity:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (id == R.id.nav_camera) {
ft.replace(R.id.fragment_placeholder, new rp_fragment());
ft.commit();
} else if (id == R.id.nav_gallery) {
ft.replace(R.id.fragment_placeholder, new sensors_fragment());
ft.commit();
gentempdata();
sensors_fragment sf = (sensors_fragment)fm.findFragmentById(R.id.fragment_sensors);
sf.ctv_tvst(1);
片段:
public class sensors_fragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
TextView ctv_tv;
TextView otv_tv;
View view;
...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors, container, false);
TextView ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
...
public void ctv_tvst(int mtext) {
ctv_tv.setText(mtext);
}
我在这里看过类似的帖子,但我无法解决我的问题。我能够修改textview的唯一地方是片段的onCreateView(参见上面的代码)。如果我尝试从mainactivity或其他任何地方做到这一点我得到空指针错误。
任何见解?
编辑:请在android_griezmann的帖子中查看我的回复。 现在,我正在做我需要做的一切,在片段类本身,然后我加载它们。我仍然想弄清楚为什么我无法从外部访问其方法或视图。
答案 0 :(得分:1)
从onCreateView更改这些行:
TextView ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)view.findViewById(R.id.otv_tv);
要:
ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
otv_tv = (TextView)view.findViewById(R.id.otv_tv);
目前您正在TextView ctv_tv
分配对新对象的引用,但实际上您希望将此引用分配到类字段,这样您就不必添加TextView
。
答案 1 :(得分:0)
您的问题是Fragment
本身不是TextView
。您将片段对象视为null而不是textview。
因此,尝试找到如下所示的片段。
if (id == R.id.nav_gallery) {
ft.replace(R.id.fragment_placeholder, new sensors_fragment(),"YOUR_TAG_NAME");
ft.commit();
gentempdata();
sensors_fragment sf = (sensors_fragment) fm.findFragmentByTag("YOUR_TAG_NAME");
sf.ctv_tvst(1);
}
也改变这个方法!
public void ctv_tvst(int mtext) {
ctv_tv.setText("" + mtext);
}
答案 2 :(得分:0)
替换以下代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors,container, false);
TextView ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
带
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors,container, false);
ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
创建变量String ctv_tv_string
并指定ctv_tvst()
方法中的值,不要分配给TextView
。它不会引发错误,因为你在oncreateview
ctv_tv.setText(ctv_tv_string)
答案 3 :(得分:0)
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView ctv_tv = (TextView)getView().findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)getView().findViewById(R.id.otv_tv);
}
覆盖创建的onactivity,然后引用你的视图..事实上,它是获取引用你的视图的最佳位置,而不会获得NullPointer异常
答案 4 :(得分:0)
如果您想在替换或添加时传递数据,则在片段构造函数中传递数据,如下所示
else if (id == R.id.nav_gallery) {
ft.replace(R.id.fragment_placeholder, new sensors_fragment("your data"));
ft.commit();
然后在你的fragmet中根据构造函数的数据更改textview值
答案 5 :(得分:0)
这是问题所在。只需删除 TextView
即可 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors, container, false);
**TextView** ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
**TextView** otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}