Ich versuche, eine benutzerdefinierte Live-Vorlage zu erstellen, um Ansichten in activity_main.xml an MainActivity.java (w/Butterknife) als globale Variablen mit der ID der Ansicht als Variablenname zu binden.android - groovy Skriptfehler in benutzerdefinierten Live-Vorlage
bindScript.groovy
//Arguments to be used in live template
def cN = _1 //'MainActivity'
def pN = _2 //'com.example.myapp'
def bool = _3 // true or false depending on whether we want the id or the view type
//Extract app Name from package. Assume that package name is name of project directory
def dN = pN.substring(pN.lastIndexOf(".") + 1).trim()
//Extract words from MainActivity camel-case. Assume layout file is activity_main.xml
layoutFileWords = cN.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")
//Make layout name corresponding to activity class
def layoutName = layoutFileWords[layoutFileWords.size()-1].toLowerCase()
for (int i = 0; i<layoutFileWords.size() - 1; i++) {
layoutName = layoutName + "_" + layoutFileWords[i].toLowerCase()
}
layoutName = layoutName + ".xml"
//Create layout directory path from package name
def layoutDir = "C:\\Users\\" + userName + "\\AndroidStudioProjects\\" + dN
+ "\\app\\src\\main\\res\\layout\\"
//Full layout name and qualified paths
def layoutFile = layoutDir + layoutName
//Read Layout File, parse XML and get Elements that have attribute "android:id"
// Assume we want to inflate all views with attribute "android:id"d
def String fileContents = new File(layoutFile).text
def layout = new XmlSlurper().parseText(fileContents)
def nodes = layout.depthFirst().findAll{
it["@android:id"].text().startsWith("@+id/")
}
def viewTypes = []
def ids = []
nodes.each { node ->
// println "Node Element: ${node.name().substring(node.name().lastIndexOf(".") + 1).trim()}"
// println "Node id: ${node["@android:id"].text().substring(5)}"
viewTypes.add(node.name().substring(node.name().lastIndexOf(".") + 1).trim())
ids.add(node["@android:id"].text().substring(5))
}
//return either View Type or id, depending on live template "bool" argument
if (bool) {
viewTypes[0]
} else {
ids[0]
}
und hier ist die Live-Vorlage
@Bind(R.id.$ID$)
$VIEWTYPE$ $ID$;
Name | Expression
ID | groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), false)
VIEWTYPE | groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), true)
und hier ist die activity_main.xml Datei
<?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">
<EditText
android:id="@+id/editText1"
android:layout_width="85dp"
android:layout_height="43dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Players"
android:textAlignment="center"/>
</LinearLayout>
Wenn ich das groovy Skript in Android laufen Studio groovy Konsole mit den hartcodierten Argumente, ich gebe genau das, was erwartet wird. Allerdings, wenn ich versuchen, die Live-Vorlage zu laufen bekomme ich diese in der MainActivity
public class MainActivity extends AppCompatActivity {
@Bind(R.id.No such property:UserssnajerabindScript for class:Script1)
No such property:UsersuserNamebindScript for class:Script1
No such property:UsersuserNamebindScript for class:Script1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
}
Ich bin ziemlich sicher, dass ich meine groovy Skript Argumente richtig bin vorbei, aber ich muss zugeben, dass ich bin neu in groovyScripts in Live-Vorlagen als per die docs so wird jede Hilfe zur Lösung dieses Fehlers sehr geschätzt.
try: 'ID | groovyScript ("C: \\ Benutzer \\ benutzername \\ bindScript.groovy", className(), currentPackage(), false) ' –
@tim_yates mein Freund, du hast mich gerettet. Schreibe die Antwort auf und ich werde es akzeptieren. – flopshot
Fertig :-D froh, dass es geholfen hat! Habe Spaß! –