Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a method in another activity from an activity
    primarykey
    data
    text
    <p>I know that we can't call a method from an Activity that's in another Activity. I'm trying to figure out the best way around this.</p> <p>Here is my code. This is the method I am trying to call. It is in my <code>ScoreCard</code> activity.</p> <pre><code>public void numPlayerSetup(){ { int[] ids = { R.id.TextView11, R.id.TextView12, R.id.TextView13 }; for(int i : ids) { TextView tv = (TextView)findViewById(i); tv.setVisibility(View.INVISIBLE); } } </code></pre> <p>Here is how I am trying to call the method. <code>score</code> is an object of the <code>ScoreCard</code> class.</p> <pre><code>public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3){ int item = spinner.getSelectedItemPosition(); if(item==1){ Log.i("error","This Sucks"); score.numPlayerSetup(); } } </code></pre> <p>I tried to put the <code>numPlayerSetup</code> method in a different class that would not extend <code>Activity</code>, just contain the logic, but I can't use the <code>findViewById()</code> method without extending activity.</p> <p>This is how I am calling it.</p> <pre><code>public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3){ int item = spinner.getSelectedItemPosition(); ArrayList&lt;TextView&gt; myTextViewList = new ArrayList&lt;TextView&gt;(); TextView tv1 = (TextView)findViewById(R.id.TextView14); myTextViewList.add(tv1); if(item==1){ Log.i("error","This Sucks"); Setup.numPlayerSetup(myTextViewList); } </code></pre> <p>Then this is the class I am calling.</p> <pre><code>public class Setup { TextView tv; public static void numPlayerSetup(ArrayList&lt;TextView&gt; tvs){ for(TextView tv : tvs) { Log.i("crash","This Sucks"); tv.setVisibility(View.INVISIBLE); //this line is highlighted in the debugger as the line my error is coming from } } } </code></pre> <p>It logs the message in the logcat and gives me a null pointer exception. The debugger says that the value for tv is null. Is this why I am getting a null pointer exception?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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