Android application stops after setText on TextVie

2019-01-12 12:57发布

问题:

I have an android program which sends to another activity after clicking on a button. Mainly, I want to set the text from textview in the new windows so that it corresponds to the selected button. For example, if I click on button Writers, the next new activity should have a textview on which the word Writers appears. Everything works fine, except when I try to setText on the TextView icon for category. I also tried to call this change in the first activity, before launching the second one, it didn't work. I also mention that if I comment the line with the setText, the program works just fine.

private String          category;
public final static String CATEGORY_MESSAGE = "e.c.project.CATEGORY";
public void onClick(View v) {
    switch(v.getId())
        {
            case R.id.actors:
                category = "actors";
                playTheGame(v);
            break;
            case R.id.cartoons:
                category = "cartoons";
                playTheGame(v);
        break;
            case R.id.singers:
                category = "singers";
                playTheGame(v);
            break;
            case R.id.writers:
                category = "writers";
                playTheGame(v);
            break;
        }
    }

    public void playTheGame( View view ){
        Intent intent = new Intent(this, PlayGame.class);
        String category = playGameButton.getText().toString();
        intent.putExtra(CATEGORY_MESSAGE, category);
//      TextView tv = (TextView) findViewById(R.id.categoryTV);
//      tv.setText(category);
        startActivity(intent);
    }

this is the OnCreate method from the second activity:

    private TextView            categoryTV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String category = intent.getStringExtra(GameCategories.CATEGORY_MESSAGE);

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

    setContentView(R.layout.activity_play_game);
    // Show the Up button in the action bar.
    setupActionBar();
}

回答1:

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