Android Studio code error - android-studio

the app was bulding fine, until I added some new layouts and when I run it this error shows within the line of codes
enter code
here public static final class id {
public static final int Nexus 5 (5_0")=0x7f070001;
public static final int dummy_button=0x7f070003;
public static final int fullscreen_content=0x7f070000;
public static final int fullscreen_content_controls=0x7f070002;
public static final int switch2=0x7f070004;
in second line: public static final int Nexus 5 (5_0")=0x7f070001;
it underlines it red from Nexus 5 (5_0")=0x7f070001; till the end of that line. what did I do wrong?

Your problem is that Nexus 5 (5_0") is not a valid identifier for a Java variables. You cannot have spaces, for example, in the names. When you declared the new layouts with the new ids, Android Studio automatically took the ID names and created the Java equivalents. The rest of your variables have a valid name and follow the naming convention for resource ids.
Try renaming the ID (in the layout file) to something similar to nexus_5.

Related

EF Core 7 t4 file - get string length

I've installed the default t4 templates for EF Core into my project, and now I want to edit the EntityType.t4 template so that for any varchar field, I generate a static variable with the max length. For example, the scaffold right now will generate a line like:
public string? CollateralName { get; set; }
When it does that, I also want it to add:
public const int CollateralNamePropertyMaxLength = 50;
I can see in the t4 file where it's writing the property, but I'm not sure how to get the max length of the string from the database column.

How to set image for android object

I'm having trouble trying to add an image inside the object
This is my Category entity
#PrimaryKey
private int categoryId;
#ColumnInfo
private String categoryName;
#ColumnInfo
private String categoryImage;
and then I tried adding image by this
db = Room.databaseBuilder(activity.getApplicationContext(),
AppDatabase.class, "foodOrder").allowMainThreadQueries().build();
Category category = new Category
(2, "Drink", "D:\\SourceCode\\Android\\PRM\\app\\src\\main\\res\\drawable-xxxhdpi\\pizza.jpg");
db.categoryService().insertAll(category);
but it didn't show the image. So what should I do ?
You have coded a path (D:\SourceCode\Android\PRM\app\src\main\res\drawable-xxxhdpi\pizza.jpg) that is on the computer that you are developing the App on. Your computer is not available to the device/emulator at run time.
You need to save the resource name and use that to retrieve the resource.
Perhaps refer to https://developer.android.com/reference/android/content/res/Resources

How prevent apktool sort global variables?

Hi first Forgive me for my weak english . i have a class named Class A in my android application with the content below :
public class A {
public static final SomeClass1 variable_C = new SomeClass1();
public static final SomeClass1 variable_A = new SomeClass1();
public static final SomeClass1 variable_D = new SomeClass1();
public static final SomeClass1 variable_B = new SomeClass1();
}
when i compile my project and then give my project apk to apktool to decompile it , apktool decompiles Class A Like below :
public class A {
public static final SomeClass1 variable_A = new SomeClass1();
public static final SomeClass1 variable_B = new SomeClass1();
public static final SomeClass1 variable_C = new SomeClass1();
public static final SomeClass1 variable_D = new SomeClass1();
}
apktool when decompile my project apk changes global variables ordering to alphabetical order .
How can i force apktool does not sort global variables in alphabetical order and keep main global variables ordering when decompile my apk ?
thanks for your answers .
This is not apktool's fault. Unlike the Java classfile format, the Android dex format requires that fields appear in sorted order, which means that the source level ordering is lost as soon as you compile your code.
From https://source.android.com/devices/tech/dalvik/dex-format#class-data-item
the defined static fields, represented as a sequence of encoded elements. The fields must be sorted by field_idx in increasing order.
field identifiers list. These are identifiers for all fields referred to by this file, whether defined in the file or not. This list must be sorted, where the defining type (by type_id index) is the major order, field name (by string_id index) is the intermediate order, and type (by type_id index) is the minor order. The list must not contain any duplicate entries.

Groovy/Gremlin class naming scheme (capital letter required)

What is the difference between the following Groovy/Gremlin snippets? (both saved as *.groovy files and run with ./gremlin.sh -e [filename].groovy)
class user
{
String username
static void main(String[] args)
{
user mtm = new user()
mtm.username = "MuffinTheMan"
println mtm.username
}
}
and
class User
{
String username
static void main(String[] args)
{
User mtm = new User()
mtm.username = "MuffinTheMan"
println mtm.username
}
}
The first gives 3 compilation errors similar to this one:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 7: Apparent variable 'mtm' was found in a static scope but doesn't
refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from
a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'mtm' but left out brackets in a place not allowed
by the grammar.
# line 7, column 14.
user mtm = new user()
The second compiles and runs just fine and outputs:
MuffinTheMan
It turns out the only difference is that the class name in the first begins with a lower case letter, while the class name in the second begins with an uppercase letter. There's a bit of discussion here, but it's difficult to find a lot of good info on the issue. I decided to post this because I was beating my head against the wall (not literally) trying to figure out why my code (class name having its first letter in lowercase) wouldn't compile, and perhaps others may have this issue.
If anyone else has a better link to a more full/clear discussion on the issue, post it!

Java - Adding List<String> from parameter to existing list

I want to be able to add new entries of parameter inputs to the list.
For example:
public static void theList (List<String> wholeList) {
wholeList = new ArrayList<String>();
wholeList.add("Lettuce");
wholeList.add("Bacon");
wholeList.add("Milk");
wholeList.add(wholeList); <--------- error - addAll doesn't fix it.
Above I tried ' wholeList.add(wholeList) '. What I intended to do is: Whatever additional (item (from parameter), when adding the input to run this method) item I need to add, will be added to the ' wholeList '.
As you can see, I have 3 items added to the list: Lettuce, Bacon and Milk. However, if I suddenly changed my mind, and want to add another item (via parameter), I can simply add it to the current list (wholeList).
Also, another question.
Is there a neater way to add a list of items instead of adding it one-by-one (whilst using the same list import)? Say, {"Lettuce", "Milk", "Bacon", etc}?
TY.
As I understand, addAll() is everything you need:
List<String> someList = new ArrayList<String>();
List<String> itemsToAdd = new ArrayList<String>();
itemsToAdd.add("one");
itemsToAdd.add("two");
someList.addAll(itemsToAdd);
// or use handy method which creates temporary list internally:
someList.addAll(Arrays.asList("three", "four"));
Well, your code does something very wrong.
You initialize the wholeList inside the method, and after the method is finished, it is gone (pointers in Java).
Also, you added the list on itself, so the code is probably not what you wanted to do.
you probably meant to create a new list inside the method and add all the items to the list in the parameter.
If so, you shouldn't use "new" on the list that you got from a parameter.
Actually, after reading the title of your question -
You need an existing list - it can't be with the name of the list in the parameter. Let's call it existingList.
After you get the list in the method, you shouldn't use the "new ArralyList" on it, as it will void the list from the parameter.
Your code should look like that:
public static void theList (List<String> wholeList) {
wholeList.add("Lettuce");
wholeList.add("Bacon");
wholeList.add("Milk");
existingList.add(wholeList);
The only "cleaner" way of adding the values to the list would be:
wholelist.addAll(Arrays.asList("Lettuce", "Bacon", "Milk"));
But I see the Top answer already states that. So, you could clean it up more by creating a array as a global private variable outside of the method. Also, as another answer said, you should have another seperate list that does not share the same name as the parameter list. Here is an example with libaries needed:
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class Example{
private List<String> globalList = new ArrayList<>();
private String[] list = {"Bacon", "Lettuce", "Milk"};
public static void theList (List<String> wholelist) {
wholelist.addAll(Arrays.asList(list));
globalList.addAll(wholeList);
}
If you wanted to use wholeList as the name for both lists, then you could change globalList above to wholelist, then:
public static void theList (List<String> wholelist) {
this.wholelist.AddAll(wholelist);
this.wholelist.addAll(Arrays.asList(list));
}
But I would avoid doing that.

Resources