【发布时间】:2014-01-05 06:44:11
【问题描述】:
我有一个ViewPager,我正在向其中添加布局。在布局中,我有 textviews,但是当我尝试通过按下按钮更改它们的文本值时,应用程序崩溃并且 LogCat 在我更改 TextView 的文本值的行上给了我一个 NullPointerExeception。我检查了我将TextView 更改为的值,它们不为空。知道我可能做错了什么吗?
代码更新: 代码片段(我在其中初始化 TextView 并将 Layout 膨胀到 ViewPager 中):
textview_location = (TextView) findViewById(R.id.textview_location);
//Adapter Class for ViewPager:
viewpagerAdapter = new ViewPagerAdapter();
viewpager_weather = (ViewPager) findViewById(R.id.pager_weather);
//Set the Adapter:
viewpager_weather.setAdapter(viewpagerAdapter);
//Create an initial view to display:
layoutInflater =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate a layout:
layout_weatheroverview =(RelativeLayout)layoutInflater.inflate(R.layout.viewpager_weatheroverview, null);
//Add the layout view to the ViewPager:
viewpagerAdapter.addView (layout_weatheroverview, 0);
//Notify that there has been data mod:
viewpagerAdapter.notifyDataSetChanged();
现在在下一个 sn-p 中,我只需设置一个文本更改并获得一个空点异常:
textview_location.setText(location);
【问题讨论】:
-
贴出logcat日志和activity的代码示例
-
TextView没有正确初始化给你 NPE -
我确实已经对其进行了初始化...让我发布一些代码。等一下,谢谢大家。
-
请在您的问题中也包含一些logcat的代码和内容,这将有助于其他人为您的查询提供一个良好而清晰的解释。
标签: android null textview android-viewpager