我需要能够在textview'quota'中访问double以便进行一些数学运算。问题是我在应该获取值的行上遇到错误。我不确定这可能是因为textview处于膨胀布局内。
我尝试使用在适配器中设置的名称,我认为这可能是访问它的方式,但是没有运气。
在主类中查找文本视图,称其为配额
quota = findViewById(R.id.textView_dailyData_BricksQuota);
在适配器类中找到textview,将其命名为dataBricksQuota,尝试在主类中调用它,但不起作用
TextView dataBricksQuota = view.findViewById(R.id.textView_dailyData_BricksQuota);
dataBricksQuota.setText(String.valueOf(project.getBricksQuota()));
此if语句的第一行出现问题,我尝试在其中获取主类中textview的值
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
});
}
我原本以为它将只使用textview的值,以便可以在公式中使用它,但是当我运行if语句时,会出现错误。
错误
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)
答案 0 :(得分:0)
并非完全是问题的答案,但我突然意识到这只是用户的视觉辅助。我为配额设置了一个全局变量,并在运行方程式时对其进行了相应的修改,然后再次简单地调用该变量以替换其在文本视图中显示的内容。
答案 1 :(得分:0)
如果您传递主类的上下文,则可以这样做。代码如下
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
另一种方法是扩大整个布局,但在这种情况下我认为它没有用。