简体   繁体   English

如何访问膨胀布局内的TextView的值

[英]How to access the value of a TextView that is inside an inflated layout

I need to be able to access the double in the textview 'quota' in order to do some maths with it. 我需要能够访问textview'quota'中的double,以便对它进行一些数学运算。 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. 这可能是因为textview在一个膨胀的布局中,我不确定。

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 在主类中查找textview,将其命名为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,尝试在主类中调用它,但不起作用

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语句的第一行,我尝试在主类中获取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
    });
}

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. 我原以为它只需要取得textview的值,因此可以在方程中使用,但是当我运行if语句时,会出现错误。

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)

Not exactly an answer to the question, but it just dawned on me that this is simply a visual aid for the user. 这不是问题的答案,但我突然意识到这只是对用户的视觉辅助。 I set up a global variable for the quota and modified it accordingly when the equation was run, then simply called the variable again to replace what it says on the textview. 我为配额设置了一个全局变量,并在运行等式时相应地对其进行了修改,然后再次简单地调用该变量来替换它在textview上所说的内容。

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. 另一种方法是膨胀整个布局,但在这种情况下我觉得它没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM