【问题标题】:Android Studio: Getting id of Dialog TextView in another class?Android Studio:在另一个类中获取 Dialog TextView 的 id?
【发布时间】:2021-01-18 15:44:37
【问题描述】:

我正在尝试在 Activtiy A 的“remaining_amount_report”对话框中创建几个进度条:

public void setProgressBars(){
    ProgressBar_Setup setup = new ProgressBar_Setup();
    setup.createProgressBar(1, "product1", 80);
    setup.createProgressBar(2, "product2", 60);
    setup.createProgressBar(3, "product3", 100);
    setup.createProgressBar(4, "product4", 20);

}

因此,我创建了一个可以访问方法的类“ProgressBar_Setup”。 但问题是,由于我想覆盖对话框的一些 TextView 文本,我需要访问 Class 内的 Dialog。

该类扩展了 Activity A,所以我通常应该像这样获得访问权限:

public void createProgressBar(int position, String productname, int progress) {

    mposition = position;
    switch(mposition){
        case 1:
            progressbar[0].setProductname(((TextView) remaining_amount_report.findViewById(R.id.remaining_amount_productname_1)));

但是我得到一个空对象引用错误,这样做。

然后我尝试了一种不同的方法,通过向“createProgressBar”方法添加一个对话框变量(我猜这不是很好的行为):

 public void setProgressBars(){
    ProgressBar_Setup setup = new ProgressBar_Setup();
    setup.createProgressBar(1, "product1", 80, remaining_amount_report);
    setup.createProgressBar(2, "product2", 60, remaining_amount_report);
    setup.createProgressBar(3, "product3", 100, remaining_amount_report);
    setup.createProgressBar(4, "product4", 20, remaining_amount_report);

在 ProgressBar_Setup-Class 中

public void createProgressBar(int position, String productname, int progress, Dialog dialog) {

    mposition = position;
    switch(mposition){
        case 1:
            progressbar[0].setProductname(((TextView) dialog.findViewById(R.id.remaining_amount_productname_1)));

但还是同样的错误。

你能帮我吗?

谢谢!

【问题讨论】:

    标签: java android dialog


    【解决方案1】:

    我认为您需要先使用findViewById 链接progressbar 中的视图。

    【讨论】:

    • 你能给我一个例子吗?不知道你的意思..对不起。
    • 首先,上传整个类文件而不是单个函数以获得更多上下文总是更好,只有这样我才能看到变量是如何被引用的。谢谢!
    猜你喜欢
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多