安卓风格 - 在R文件中找不到符号变量



我有两种风格 - 付费应用和免费应用。付费应用程序在MainActivity上多了一个按钮的唯一区别,比如说"paidbutton"。付费应用有自己的布局,带有按钮android:id="@+id/paidbutton",而免费应用程序的布局没有此按钮。

在代码中,我使用这个:

if (BuildConfig.FLAVOR.equals("paidapp")) {
            View paidbutton = findViewById(R.id.paidbutton);
            paidbutton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //...
                }
            });
}

但是我在findViewById(R.id.paidbutton);中有一个错误cannot find symbol variable

如何在不克隆两种口味的 MainActivity 的情况下解决此问题.java?

编辑1 - 添加更多代码示例:

格拉德尔:

productFlavors {
    paidapp {
    }
    freeapp {
    }
}
/

app/src/main/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/goNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go next" />
</LinearLayout>
/

app/src/paidapp/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/goNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go next" />
    <Button
        android:id="@+id/paidbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="See more" />

</LinearLayout>
/

app/src/main/java/company/com/myapplication/MainActivity.java

package company.com.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View goNext = findViewById(R.id.goNext);
        goNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "goNext click", Toast.LENGTH_SHORT).show();
            }
        });
        if (BuildConfig.FLAVOR.equals("paidapp")) {
            View paidbutton = findViewById(R.id.paidbutton);
            paidbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "paidapp click", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}

EDIT2 - 我使用反射找到了一些解决方法,但仍在寻找正常的解决方案:

而不是View paidbutton = findViewById(R.id.paidbutton);我使用了View paidbutton = findViewById(getIdFromRId("paidbutton"));

其中getIdFromRId是:

private int getIdFromRId(String idName) {
    int id = 0;
    try {
        Class c = R.id.class;
        Field field = c.getField(idName);
        id = field.getInt(null);
    } catch (Exception e) {
        // do nothing
    }
    return id;
}

如果在freeapp产品变种中根本没有布局元素paidbutton则只需在产品变种文件夹freeapp资源 xml 文件之一中声明相应的 id 项。例如,创建包含以下内容的文件src/freeapp/res/values/ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="paidbutton" type="id"/>
</resources>

完成此操作后,编译通用代码将没有问题,因为将为两种产品风格定义符号R.id.paidbuttonfindViewById()调用将在paidapp生成中返回实际布局元素,并将freeapp生成中返回 null。

你能把你的paidbutton XML标签和你的MainActivity.java类导入吗?

您可能使用了错误的资源导入,例如import android.R;而不是import my_package.R;

你需要在 on create 中使用 if 子句,然后添加 setContentView,所以像这样:

if (BuildConfig.FLAVOR.equals("paidapp")) {
        setContentView(R.layout.activity_main_paid);
        View paidbutton = findViewById(R.id.paidbutton);
        paidbutton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //...
            }
        });
}
else{
    setContentView(R.layout.activity_main_free);
}

或者,您可以同时使用一种布局,并使按钮不可见

我希望这有帮助

最新更新