Error when accessing variable from another class - java

I am trying to use a variable from another class. however, it shows an error at the following code:
public class ItemDetailActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState, String[] args) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);
DummyContent Position = new DummyContent();
int picCheck = Position.getPos();
}
}
Here is the getter:
public int getPos(int position) {
return position;
}
Can you show me what my problem here is?
The error:
Error:(31, 32) error: method getPos in class DummyContent cannot be applied to given types;
required: int
found: no arguments
reason: actual and formal argument lists differ in length

Here:
int picCheck = Position.getPos();
Intending to use:
public int getPos(int position)
Notice that one line wants an int parameter; and that the other line doesn't give one.
And the real answer here is: the compiler message already tells you so. Required int, found: no arguments.
Meaning: java compiler messages are easy to read most of the time.
Thus the answer beyond the simple left-over here: if your Java skills are on a level that makes it hard to understand such messages, you are most likely overburdening yourself at this point by trying to do Android programming.

Related

findViewById not accepting type argument even with compileSdkVersion 30

I generally use kotlin for android but my college wants me to use Java. So I created a new java project in android studio.
The problem is I don't want to cast the return value of findViewById() manually. Instead I want to pass EditText as type parameter which the function is not accepting as anticipated. I get the error :
/home/onkar/AndroidStudioProjects/MyApplication2/app/src/main/java/io/github/omkar76/myapplication/MainActivity.java:16: error: cannot find symbol
Log.d("EDITTEXT", findViewById<EditText>(R.id.first_name).getText());
^
symbol: variable findViewById
location: class MainActivity
Why isn't this working? Why is method not found? I even checked source of AppCompatActivity and it does contain required method:
#Override
public <T extends View> T findViewById(#IdRes int id) {
return getDelegate().findViewById(id);
}
My Java code is :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("EDITTEXT", findViewById<EditText>(R.id.first_name).getText());
}
}
If I don't pass type argument EditText, findViewById returns View which then I need to manually cast. According to this post starting with api 26 you don't need to cast the returned value. I have API 30 SDK. So why this is not working. I am aware of workarounds like assigning return value to an EditText reference but my interest is in knowing why existig approach doesn't work.
Let me know if any other details are required. Please point out what I am doing wrong. Thanks!
I think you're confused with the syntax of java.
Here public <T extends View> T findViewById(#ResId int id) means :
return value cast to T, T is resolved with the left side of the assignment declaration before the equals sign. In the following example
Edittext x = view.findViewById(R.id.abc)
So here T get assigned as Edittext as Edittext is on the left side of the assignment declaration which then returns edittext as a view. Then you can call x to getstring.
Hope this makes it clearer
Actually, Java has no such syntax as Kotlin for findViewById.
The Kotlin syntax:
findViewById<EditText>(R.id.first_name)
The equivalent in Java:
(EditText)findViewById(R.id.first_name);
So, you can do like this in java:
Log.d("EDITTEXT", ((EditText)findViewById(R.id.first_name)).getText().toString());
Note: You have to put toString() to convert from Editable to String explicitly in Java
The simplest way use a variable name
EditText editext = findViewById(R.id.first_name);
then you could use
Log.d("EDITTEXT", editext.getText().toString());

How can I use findviewbyid in static class?

I have a static class:
public static void culculateFprice(){
TextView FinalBuy = (TextView) findViewById(R.id.buyText);
int Pprice = MainActivity.pepperoni.getFinalPrice();
int Cprice = MainActivity.calzone.getFinalPrice();
int QCprice = MainActivity.quattrostagioni.getFinalPrice();
int QFprice = MainActivity.quattroformaggi.getFinalPrice();
int Mprice = MainActivity.mexican.getFinalPrice();
int FinalPrice = Pprice + Cprice + QCprice + QFprice + Mprice;
FinalBuy.setText("Стоимось вашего заказа: " + FinalPrice + " руб.");
}
How can I use find findViewById in this class?
I call this method from this method
public static void onPlus(int i){
ArrayList<String> list = listok();
switch (list.get(i)){
...
}
adapteR.refreshData(listokadd());
culculateFprice();
}
And have problem "Non-static method "findViewById(int)" cannot be referenced from a static context"
I assume this function exists in your Activity class and you would like to process some information based on the View. There are two ways you can solve this:
Remove the static modifier and let the instance take care of this.
Create a field that holds a View reference that gets created as soon as your layout is set.
So your Activity will hold the field:
class MyActivity extends Activity {
static View FinalBuy; // needs to be static, otherwise would give same error
}
#Override
void onCreate(Bundle savedInstance) {
// after setLayout
FinalBuy = (TextView) findViewById(R.id.buyText);
}
But to save your code from a potential runtime error, use an if...else block in your static method:
public static void culculateFprice(){
if(FinalBuy != null) {
// your code here.
}
}
This problem is basic Java and not Android. That is how Java language is designed to provide instance vs. static methods for the programs.
This static View declaration leads to a potential design flaw because this View will outlive the Activity. Instead of this, you should consider using the approach I suggested in the list as option 1 and see why you are unable to use an instance method in your program. You should read more on this thread.
This will likely need you to change your other fields of MainActivity to be declared as non-static as well!

What is the best practice for passing object reference to different methods?

First, I would like to thank you in advance for reading/answering my first question on stack overflow. I am new to java (android studio) and struggling a bit with understanding all these objects…
I am building my first app and I have created a new class to store and handle some Tile properties.
What I want, is to instantiate the object once then use a reference to that object and I will pass it to other methods for changing the data inside TileProperties. (I believe this is a good optimization of the memory usage)
I have tried a few solutions that I found but couldn’t make them work fully or couldn’t understand the code.
Here is what I did, which works now but I am wondering if it’s the best practice and also if it's a good option for the future:
public class TileProperties {
private String length ;
private String width ;
public static TileProperties object; // I created a static object TileProperties
// which I use to pass the reference
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
The code below shows my activity class where the user enters all the properties
Also, its where I initiate my object Tiles
public class TileSettingActivity extends AppCompatActivity {
TileProperties tiles = new TileProperties();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tile_setting);
}
public void clickOkButton (View view) {
TileProperties.object = tiles; // save the object reference tiles to be used by others
EditText lengthTile = findViewById(R.id.editText2);
tiles.setLength(lengthTile.getText().toString());
Log.i("Length:", tiles.getLength());
}
And then in my main activity I wrote this:
public class MainActivity extends AppCompatActivity {
public void clickScanButton(View view){
TileProperties obj ; // define object reference
obj = TileProperties.object; // get the reference of the orginal object Tiles
Log.i("info:", obj.getLength());
}
Thank you!!
No this is not "best practice". It is not even "good practice". It is arguably wrong. Certainly it would be wrong in a larger application.
Let's start with this class:
public class TileProperties {
private String length ;
private String width ;
public static TileProperties object; // I created a static object TileProperties
// which I use to pass the reference
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
}
In order of least important to most important, the problems are:
There are various style issues. The most important are that the indentation is incorrect and your choice of object for a variable name is bad. Variable names should convey relevant meaning to the reader.
Documentation. The comment // I created a static object ... should be a proper javadoc comment, that describes the purpose of this (public !) variable.
Public variables break encapsulation and encourage excessive coupling. They are not Object Oriented. They should be avoided.
Public static variables are worse because they also represent global state.
If you are going to implement a single shared "properties" object for the application, there are three ways to do it.
You can create a single instance of the class and pass it as a parameter to all of the parts of the application that need it. This can be clumsy if you have to pass the reference to lots of places.
You can use the Singleton design pattern. A simple example for your use-case would be:
public class TileProperties {
// state variables
private static TileProperties instance = new TileProperties();
private TileProperties() { } // This prevents creation of multiple
// instances of the "singleton"
public static TileProperties getInstance() {
return instance;
}
// getters and setters for state variables, etc.
}
// In main
TileProperties props = TileProperties.getInstance();
props.setLength(42);
There are other ways to implement singletons in Java; e.g. if the singleton needs to be initialized lazily.
Use Dependency Injection (DI).
Dependency injection is considered to be superior to Singleton classes in large-scale applications because singletons present problems for unit testing. However, DI requires a framework such as Spring to implement the injection. That is a whole new learning curve.

Android Studio Unknown Class

When I declare double variables, and later try to assign values to them, it says unknown class . Secondly, any method calls from objects I have created say cannot resolve symbol .
My code:
private double xCurrentPos, yCurrentPos;
private ImageView test = (ImageView) findViewById(R.id.square);
xCurrentPos = test.getLeft();
yCurrentPos = test.getTop();
This code says that xCurrentPos and yCurrentPos are both unknown classes and that it cannot resolve getLeft or getTop.
Any help would be greatly appreciated. :)
You can't do assignments of variables like that in the context of the class.
From what I can tell you're doing something like this which gives you the errors you described.
public class YourActivity extends Activity {
private double xCurrentPos, yCurrentPos;
private ImageView test = (ImageView) findViewById(R.id.square);
xCurrentPos = test.getLeft();
yCurrentPos = test.getTop();
...
}
You would have to do the assignment as they are being declared like this:
private int xCurrentPos= test.getLeft();
private int yCurrentPos = test.getTop();
Or you do the assignments within an actual method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
xCurrentPos = test.getLeft();
yCurrentPos = test.getTop();
}
Second, doing this:
private ImageView test = (ImageView) findViewById(R.id.square);
will not work anyway as this will get initialized before onCreate so test will be null.
Even doing this in onCreate after you set the content view will give you values of 0 for both getLeft and getTop as the your views have not been drawn yet. For that you can refer to this. You basically need to do it in an event where you views have already been drawn.
The View class's getLeft() and getTop methods return an int, not a double. You don't need to use double type.

Problem accessing variable[] from another class

I know this a pretty basic question, and already found another ones like mine, but I honestly don't know what I'm doing wrong.
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public int teste;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
public int getTest()
{
return teste;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
teste = 2;
....
}
}
and other class:
try{
InteractiveArrayAdapter adapt = new InteractiveArrayAdapter(this,
getAPPS(0));
int test = adapt.getTest();
Toast.makeText(this, Integer.toString(test), Toast.LENGTH_LONG).show();
Log.v("TAG",Integer.toString(test));
}catch(Exception e)
{
Log.v("EXCEPTION",e.toString());
}
EDIT: I was getting null for a stupid mistake, and now I'm getting the primitive and expected 0 as most of you say.
At some point of my app, everytime a checkboxes is clicked that method getView is executed. I want to store that to an array[] of strings progressively (i+1) (i just put int to be easier to understand - realize now it was a mistake), and then when users inputs ok I want to access the whole array. Wondering if it's possible the way I want.
So when I do this
InteractiveArrayAdapter adapt = new InteractiveArrayAdapter(this,
getAPPS(0));
This is meaningless, because I don't need to execute anything again, I just want to retrieve the created array - if possible!
Your code won't even compile. return this.teste; should be return this.test;.
Well, this isn't a direct copy/paste, since this obviously wouldn't compile. Whenever you're dealing with an actual error or issue, it's really best to paste the actual code. We're all programmers, so we can read it.
But based on the structure you've shown above, either the typo you've put in the line return this.teste (should be return this.test) is in your code, or you didn't initialize the instance variable test in your constructor.
Without showing us the actual code you're writing, it's impossible to say (especially the section that initializes the test variable, and the part that returns its value are missing - we're not mind readers, I'm afraid).
So, those are two potential candidates. On another note, however, if you mark the test variable as public, then you don't need to have getter/setter methods for them, since any class can access them without going through a method call. That's what public does.
But that is what should happen according to your code. You don't call B method to update teste variable.

Categories

Resources