Android application stops after setText on TextView

*爱你&永不变心* 提交于 2019-11-27 16:29:33

You need to call setContentView(R.layout.activity_play_game); before categoryTV = (TextView) findViewById(R.id.categoryTV); Otherwise your TextView is null

   private TextView, categoryTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_play_game);  // make this call BEFORE initializing ANY views
       Intent intent = getIntent();
       String category = intent.getStringExtra(GameCategories.CATEGORY_MESSAGE);

       categoryTV = (TextView) findViewById(R.id.categoryTV);
       categoryTV.setText(category);


        // Show the Up button in the action bar.
         setupActionBar();
    }

Your Views exist in your Layout so if you try to access any of them before inflating your Layout, with setContentView() or an inflater, they will be null resulting in a NPE when you try to call a method on them such as setText()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!