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)