问题描述
为什么会变成这样code可能崩溃我的Android应用程序吗?
Why would this code could crash my android app ?
TextView txt_EnrollmentType = (TextView)findViewById(R.id.enrollmenttype);
String s1 = getResources().getString(R.string.enrollmenttype_value) ;
EnrollmentType.setVisibility(View.VISIBLE);
txt_EnrollmentType.setText(s1+ " " );
我深信R.id.enrollmenttype是present。所以txt_EnrollmentType不为空。
I am sure that the R.id.enrollmenttype is present. so txt_EnrollmentType is not null.
herer是上的logcat https://www.dropbox.com/s错误/r2fterr2hvi28/log.txt
herer is the errors on the logcat https://www.dropbox.com/s/r2fterr2hvi28/log.txt
推荐答案
您需要抬高一个布局
包含浏览
前试图访问他们或他们将空
。通过使用一个布局充气
或更常见的有的setContentView()
如下做到这一点。这是可以做到其他地方,但在的onCreate最常用的()
或 onResume()
You need to inflate a layout
containing the Views
before trying to access them or they will be null
. Do this either by using a layout inflater
or more commonly with setContentView()
as below. This can be done other places but is most commonly used in onCreate()
or onResume()
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.your_layout_file); // layout file containing the views minus the .xml extension
您浏览
的布局中存在
所以他们空
,直到膨胀
的布局
。您必须执行此之前试图用类似
Your Views
exist inside your layout
so they are null
until you inflate
the layout
. You must do this BEFORE trying to initialize them with something like
TextView txt_EnrollmentType = (TextView)findViewById(R.id.enrollmenttype);
这篇关于任何可能的原因,为什么这个价值的Android在c $ C崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!