Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to call <code>setContentView()</code> in your <code>onCreate()</code> method before using <code>findViewById()</code>. Here is an example of doing this...</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set this to whatever your layout is setContentView(R.layout.main); // Now you can call the activity's findViewById() method... TextView nt = (TextView)findViewById(R.id.notify); } </code></pre> <p><strong>Edit</strong> Your <code>nt</code> variable is not in the same scope as your <code>addNotification()</code> method. Fix this by making it a class member or passing it into your <code>addNotification()</code> method.</p> <p>Also, to compare strings you should really be using <code>.equals()</code> not <code>==</code>. <code>==</code> checks to see if it is the same object whereas <code>.equals()</code> will match the text between two strings.</p> <p><strong>Edit (again)</strong></p> <p>I'm going to assume you already have <code>nt</code> at the class level there's no way this would compile if not. Judging by what you've said, it seems your problem stems from the fact that you're creating a separate <code>nt</code> variable in your <code>onCreate()</code>. Change your code to something like the following (I've stripped out the unimportant parts).</p> <pre><code>public class YourActivity extends Activity { private TextView nt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Do not create a new instance, use the one you have... nt = (TextView)findViewById(R.id.notify); // Do whatever you were doing... } public void addNotification() { // This *should* work... if (nt.getText().equals("yes")) { // Do whatever you were doing... } else if (nt.getText().equals("no")) { // Do nothing... } } } </code></pre> <p>If this doesn't get you started then I'm not sure what the issue is. If this does not fix your problem you should post what you get from your <code>logcat</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload