【问题标题】:Android TextView.setText force closesAndroid TextView.setText 强制关闭
【发布时间】:2014-04-16 08:28:57
【问题描述】:

我正在尝试Google University Android lab1 你被要求根据另一个活动通过 Intent 传递的值更改 TextView 的文本内容。

我尝试了其余的代码,但是... 为什么当我添加“tv.settext(...) line”时我的应用程序强制关闭?

public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*
         * Fetch and display passed string.
         */
        TextView tv = (TextView) findViewById(R.id.HelloTV);
        Bundle extras = this.getIntent().getExtras();
        if (extras != null) {
            String nameStr = extras.get("Username").toString();
            if (nameStr != null) {
                tv.setText("Hello "+nameStr);         
            }
        }
        setContentView(R.layout.main);
    }
}

【问题讨论】:

  • extras.get("Username"),检查这个值,这可能是 null。在 logcat 中查看。
  • 你能在这里发布错误日志和xml布局吗?现在看起来 'tv' 可以为 null 就行了。
  • @bhups - 我检查用户名不是吗? - 如果 (nameStr != null)...
  • 此处的错误日志和布局 - pastebin.ca/1939350
  • HelloWorld.java 的第 22 行有什么内容? tv.setText 还是 extras.get?

标签: android textview


【解决方案1】:

查看错误日志,更好的是,查看调试会话——可以看到第22行出现空指针异常:

           tv.setText("Hello "+nameStr);         

这是因为 tv == null。它应该由以下行初始化:

    TextView tv = (TextView) findViewById(R.id.HelloTV);

但是要在布局中使用 id,您必须始终在当前活动中注册视图。这行应该早就包含在 onCreate 方法中:

    setContentView(R.layout.main);

这是工作中的 Helloworld 类:

public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /*
         * Fetch and display passed string.
         */
        TextView tv = (TextView) findViewById(R.id.HelloTV);
        Bundle extras = this.getIntent().getExtras();
        if (extras != null) {
            String nameStr;
            if (extras.get("Username") != null) {
                nameStr = extras.get("Username").toString();
                tv.setText("Hello "+nameStr);           
            }
        }
    }
}

这个 Helloworld 类正确地从活动开始时发送的附加信息中检索用户的姓名,并显示个性化的问候语。

感谢 Konstantin Burov 和上一个问题 here 我找到了答案

【讨论】:

    【解决方案2】:

    您的项目布局文件夹中是否只有一个 .xml 文件?如果您有一个用于主要活动的 xml 文件和一个由更新的 eclipse 提供的 fragment.xml 文件,您需要在自动生成的“onCreateView”函数中执行 setText。这是片段(包含视图元素)和主布局组合的地方。 因此,在您的代码中找到以下行或创建它:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {  ...   }
    

    然后在这个函数中设置你的视图的文本或其他需要的元素(例如btn、textView ...)。如:

            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
    
                        //set the text of textView
            TextView txvMain = (TextView) rootView.findViewById(R.id.txvMain);
            txvMain.setText("SetText works now");
    
                        //set a drawable as the background of the textView
            txvMain.setBackgroundResource(drawable.ic_launcher);
    
            return rootView;
        }
    

    我会期待任何进一步的问题。问候

    【讨论】:

      猜你喜欢
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多