1

So I gave myself a homework assignment: Create a Class to do a calculation and return that variable to the MainActivity to be displayed

I thought it would be simple but for some reason my TextView is always null. When I tried to recreate the error in a new file I don't get the error with the TextView - I can set it just fine.

I've logged the values as the code runs so it seems that the variables are passing the data correctly. But the moment I tried to push ANY value into my textView, I get the error.

Here it is:

 package com.kserrattan.clickerdemo001;

 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.TextView;

 import org.w3c.dom.Text;


 public class MainActivity extends ActionBarActivity {

String countString;
TextView displayCount;
View parent;

public void playerTapped(View view) {

    MathCalculations mathCalculations = new MathCalculations();
    //parent = (View) view.getRootView();
    mathCalculations.addCount();

}

public void getCount(int count) {

    Log.i("addCount", Integer.toString(count));
    countString = (Integer.toString(count));
    displayString();

}

public void displayString() {

    displayCount.setText("HIE");

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    displayCount = (TextView) findViewById(R.id.countText);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
 }

SecondClass:

package com.kserrattan.clickerdemo001;

import android.util.Log;

/**
 * Created by Kris on 05/10/2016.
 */
public class MathCalculations {

int tapCounter;

public void addCount() {

    Log.i("addCount", "HERE");
    tapCounter += 1;
    MainActivity mainActivity = new MainActivity();
    mainActivity.getCount(tapCounter);

}

}

I can only provide part of the error because for some unknown reason the rest of the error isn't formatting correctly! Hopefully this bit is enough...:

--------- beginning of crash
10-05 20:00:51.640    2375-2375/com.kserrattan.clickerdemo001     E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.kserrattan.clickerdemo001, PID: 2375
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:4007)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4002)
k.serr
  • 13
  • 4
  • you're not supposed to create an `Activity` yourself. Remove this code: `MainActivity mainActivity = new MainActivity();` – nandsito Oct 06 '16 at 01:08
  • What do I do after I remove it? I want to understand why I'm not suppose to do what I did... but your short response doesn't lead me to understand. – k.serr Oct 06 '16 at 20:29

2 Answers2

0

You need activity context to call its method. Create the context value in onCreate method:

public class MainActivity extends ActionBarActivity {

String countString;
TextView displayCount;
View parent;
Context mContext; 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = this;
    displayCount = (TextView) findViewById(R.id.countText);
}

Then pass this context while calling addCount():

public void playerTapped(View view) {
    MathCalculations mathCalculations = new MathCalculations();
    //parent = (View) view.getRootView();
    mathCalculations.addCount(mContext);
}

Modify the addCount() to use context:

public void addCount(Context mContext) {
    Log.i("addCount", "HERE");
    tapCounter += 1;
    mContext.getCount(tapCounter);
}
LoveForDroid
  • 1,072
  • 12
  • 25
  • I made the above changes and it makes sense to pass the Context along the way, but adding "mContext.getCount" makes "getCount" unresolved. I'm trying to understand why, could you elaborate? – k.serr Oct 06 '16 at 20:32
0

Make your TextView as static variable in MainActivity

 public class MainActivity extends ActionBarActivity {

   String countString;
   public static TextView displayCount;
   View parent;

   public void playerTapped(View view) {

     MathCalculations mathCalculations = new MathCalculations();
     //parent = (View) view.getRootView();
     mathCalculations.addCount();

   }

   public static void getCount(int count) {

     Log.i("addCount", Integer.toString(count));
     countString = (Integer.toString(count));
     displayString();

   }

   public static void displayString() {

     displayCount.setText("HIE");

   }

   //...
 }

static variable will garbage collected after closing the app only

Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
  • This worked without any problems, thanks for the solution. I will look up some resources as to why it works; but, would you be able to elaborate or direct me to a resource? – k.serr Oct 06 '16 at 20:25
  • I read a bit more on this thread: (http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) and I understand why the above works now, thanks again ;) – k.serr Oct 06 '16 at 21:17