I need to be able to access the double in the textview 'quota' in order to do some maths with it. Problem is I'm getting an error on the line where it is supposed to get the value. This may be because the textview is within an inflated layout, I'm not sure.
I've tried using the name set in my adaptor, which I thought might be the way to access it, but no luck.
Finding the textview in the main class, calling it quota
quota = findViewById(R.id.textView_dailyData_BricksQuota);
Finding the textview in the adapter class, calling it dataBricksQuota, tried calling this in the main class instead but doesn't work
TextView dataBricksQuota = view.findViewById(R.id.textView_dailyData_BricksQuota);
dataBricksQuota.setText(String.valueOf(project.getBricksQuota()));
Problem occurs on line one of this if statement, where I try to get the value of the textview in the main class
if (projectDay >= 2) {
String currentBrickQuota = quota.getText().toString();
double currentBrickQuotaDouble = Double.parseDouble(currentBrickQuota);
double averageBrickRunningQuota = ((2 * currentBrickQuotaDouble + averageBricksNDatabase) / projectDay);
String sql = "UPDATE activeProjectsTable SET averageBricks = ? WHERE ProjectId = (SELECT MAX(ProjectId) FROM activeProjectsTable)";
AusWideDatabase.execSQL(sql, new Double[] {
averageBrickRunningQuota
});
}
I would have thought that it would simply take the value of the textview so it can be used in the equation, however when I run the if statement, there's an error.
Error
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:7333)
at android.widget.TextView.performClick(TextView.java:14160)
at android.view.View.performClickInternal(View.java:7299)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27774)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.auswide.auswideapp.dailyData.LoadEodChecklist(dailyData.java:145)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:7333)
at android.widget.TextView.performClick(TextView.java:14160)
at android.view.View.performClickInternal(View.java:7299)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27774)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
You may do it, if you pass context of your main class. Code will be as follow
TextView textView = ((Activity)context).findViewById(R.id.yourTextView); // -> context must be context of your main class
textView.setText("Hello world"); // -> textView variable is not null, so it will assign text to textView
Another way would be inflating whole layout, but I don't find it useful in this case.