App crashes when I include setContentView and setText

41 Views Asked by At

This is my first android app, pretty confused and not sure if I'm doing it right. My app is intended to be a countdown app for a specific date

Here is what my MainActivity class looks like. I replaced ... where the code I wrote relating to the actual countdown variable was - I have tested it in the console and that part of the program works fine.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        counting();
    }

    public void counting() {
        ...
        String reading = days + " days " + hours + " hours " + minutes + " minutes remaining";
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.textview_first);
        textView.setText(reading);
    }
}

It runs and shows me the 'hello' text string specified in strings.xml

 <string name="hello_first_fragment">hello</string>

as long as I get rid of both the

        setContentView(R.layout.activity_main);

as well as the

        textView.setText(reading);

lines under my counting function, but I don't want it to show that string, I would rather it display the value of the variable I want to choose. If I include either or both of them, it crashes the app.

I'm wondering if maybe I have the textview ID wrong - I got that from my fragment_first.xml file which has

"android:id="@+id/textview_first"" 

under TextView.

Another thing I have tested is with just making the textView.setText field have a non-variable string in it, like

            textView.setText("reading");

but it does the same thing

I'm not sure if I might have to somehow remove the string line in strings.xml somehow or edit it, I tried just deleting it and it wouldn't compile.

Anyone have any advice on what I'm doing wrong?

0

There are 0 best solutions below