【问题标题】:Null Pointer Exception in TextView but value is not nullTextView 中的空指针异常但值不为空
【发布时间】: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


【解决方案1】:

如果 textview_location 在您的 ViewPager 中,您无法访问它的实例,直到您为 ViewPager 充气。

【讨论】:

  • 谢谢,我知道正确的实例化只是一步一步地弄清楚 Android 让我犯了愚蠢的错误 :-) 感谢您的快速响应!
猜你喜欢
  • 2021-05-17
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多