My First App crashes when pressing a button

前端 未结 1 1749
孤独总比滥情好
孤独总比滥情好 2021-01-16 09:34

I tried numerous times to make the app from the MyFirstApp Tutorial on developers.android (https://developer.android.com/training/basics/firstapp/index.html) work. I\'m test

相关标签:
1条回答
  • 2021-01-16 09:50

    your problem is here (DisplayMessageActivity):

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
    
       Intent intent = getIntent();
       String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    
       TextView textView = new TextView(this);
       textView.setTextSize(40);
       textView.setText(message);      
       setContentView(textView);
    
       if (savedInstanceState == null) {
           getSupportFragmentManager().beginTransaction()
                  .add(R.id.container, new PlaceholderFragment()).commit();
       }
    }
    

    you are trying to add the Fragment to R.id.container which is not part of the Activity's view hierarchy, since the only thing that it contains is a TextView

    0 讨论(0)
提交回复
热议问题