任何可能的原因,为什么这个价值的Andr​​oid在c $ C崩溃? [英] any possible reason Why this code crash under Android?

查看:85
本文介绍了任何可能的原因,为什么这个价值的Andr​​oid在c $ C崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会变成这样code可能崩溃我的Andr​​oid应用程序吗?

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);    

这篇关于任何可能的原因,为什么这个价值的Andr​​oid在c $ C崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆