Any Alternate method for Class Casting - android

Following statement is not executing on some devices.
AddEvent act1 = (AddEvent) getLocalActivityManager().getCurrentActivity();
Is there any alternate method for above statement. On some devices it works fine but on other devices it gives exception.
Edit: My application is developed in tab. OnActivity results is used for getting picture from camera activity. Dont know where the resides but when I puts the above statement in try catch then no force close occurs and exception is shown there.

The problem is not really with the casting, but the assumption that the Object returned by getLocalActivityManager().getCurrentActivity() is of type AddEvent. It would be difficult to say in what scenario does the getLocalActivityManager().getCurrentActivity() return an Object that is of not the AddEvent type without looking at your implementation.
The following piece of code will check if the returned Object is indeed of type AddEvent:
if(getLocalActivityManager().getCurrentActivity() instanceof AddEvent){
AddEvent act1 = (AddEvent) getLocalActivityManager().getCurrentActivity();
}
But in your case, it is recommended to check in what scenario does the getLocalActivityManager().getCurrentActivity() return an instance of fable.eventippo.Home as against fable.eventippo.AddEvent.

I am not sure where do you write this line but logcat output says: You are trying to cast Home Activity into AddEvent and this is why its giving you ClassCastException.
FYI, getLocalActivityManager().getCurrentActivity(); is returning Home.

Related

NavController.getBackStackEntry() dosent work as exptected

Trying to find if destination contains in back stack using NavController.getBackStackEntry(R.id.destination) but im always getting IllegalArgumentException. Im 100% sure that destination that im trying to find is in back stack (i checked it in debug mode via navController.backStack). Documentation to getBackStackEntry function is #param destinationId ID of a destination that exists on the back stack.
I noticed that R.id.destination value is different from one that was in the graph, so i tryed to recive correct one from graph[R.id.destination] and graph.findNode(R.id.destination) but i got IAE and null.
What am i doing wrong and how to get correct destinationID to pass it to function?
All these methods dosent work in debug evaluate expression (in which i was testing thoose).
If launched in regular app logic all is working fine

NoSuchMethodError (NoSuchMethodError: The getter 'length' was called on null. Receiver: null Tried calling: length) [duplicate]

I have some code and when I run it produces an error, saying:
NoSuchMethod: the method 'XYZ' was called on null
What does that mean and how do I fix it?
Why do I get this error?
Example
As a real world comparison, what just happened is this conversation:
Hey, how much gas is left in the tank of the car?
What are you talking about, we don't have a car.
That is exactly what is happening in your program. You wanted to call a function like _car.getGasLevel(); but there is no car, the variable _car is null.
Obviously, in your program it might not be a car. It could be a list or a string or anything else really.
Technical explanation
You are trying to use a variable that is null. Either you have explicitly set it to null, or you just never set it at all, the default value is null.
Like any variable, it can be passed into other functions. The place where you get the error might not be the source. You will have to follow the leads from the actual null value to where it originally came from, to find what the problem is and what the solution might be.
null can have different meanings: variables not set to another value will be null, but sometimes null values are used by programmers intentionally to signal that there is no value. Databases have nullable fields, JSON has missing values. Missing information may indeed be the information itself. The variable bool userWantsPizzaForDinner; for example might be used for true when the user said yes, false when the user declined and it might still be null when the user has not yet picked something. That's not a mistake, it's intentionally used and needs to be handled accordingly.
How do I fix it?
Find it
Use the stack trace that came with the error message to find out exactly which line the error was on. Then set a breakpoint on that line. When the program hits the breakpoint, inspect all the values of the variables. One of them is null, find out which one.
Fix it
Once you know which variable it is, find out how it ended up being null. Where did it come from? Was the value never set in the first place? Was the value another variable? How did that variable got it's value. It's like a line of breadcrumbs you can follow until you arrive at a point where you find that some variable was never set, or maybe you arrive at a point where you find that a variable was intentionally set to null. If it was unintentional, just fix it. Set it to the value you want it to have. If it was intentional, then you need to handle it further down in the program. Maybe you need another if to do something special for this case. If in doubt, you can ask the person that intentionally set it to null what they wanted to achieve.
simply the variable/function you are trying to access from the class does not exist
someClass.xyz();
above will give the error
NoSuchMethod: the method 'xyz' was called on null
because the class someClass does not exist
The following will work fine
// SomeClass created
// SomeClass has a function xyz
class SomeClass {
SomeClass();
void xyz() {
print('xyz');
}
}
void main() {
// create an instance of the class
final someClass = SomeClass();
// access the xyz function
someClass.xyz();
}

Error flutter NoSuchMethodError (NoSuchMethodError: The getter 'length' was called on null. Receiver: null [duplicate]

I have some code and when I run it produces an error, saying:
NoSuchMethod: the method 'XYZ' was called on null
What does that mean and how do I fix it?
Why do I get this error?
Example
As a real world comparison, what just happened is this conversation:
Hey, how much gas is left in the tank of the car?
What are you talking about, we don't have a car.
That is exactly what is happening in your program. You wanted to call a function like _car.getGasLevel(); but there is no car, the variable _car is null.
Obviously, in your program it might not be a car. It could be a list or a string or anything else really.
Technical explanation
You are trying to use a variable that is null. Either you have explicitly set it to null, or you just never set it at all, the default value is null.
Like any variable, it can be passed into other functions. The place where you get the error might not be the source. You will have to follow the leads from the actual null value to where it originally came from, to find what the problem is and what the solution might be.
null can have different meanings: variables not set to another value will be null, but sometimes null values are used by programmers intentionally to signal that there is no value. Databases have nullable fields, JSON has missing values. Missing information may indeed be the information itself. The variable bool userWantsPizzaForDinner; for example might be used for true when the user said yes, false when the user declined and it might still be null when the user has not yet picked something. That's not a mistake, it's intentionally used and needs to be handled accordingly.
How do I fix it?
Find it
Use the stack trace that came with the error message to find out exactly which line the error was on. Then set a breakpoint on that line. When the program hits the breakpoint, inspect all the values of the variables. One of them is null, find out which one.
Fix it
Once you know which variable it is, find out how it ended up being null. Where did it come from? Was the value never set in the first place? Was the value another variable? How did that variable got it's value. It's like a line of breadcrumbs you can follow until you arrive at a point where you find that some variable was never set, or maybe you arrive at a point where you find that a variable was intentionally set to null. If it was unintentional, just fix it. Set it to the value you want it to have. If it was intentional, then you need to handle it further down in the program. Maybe you need another if to do something special for this case. If in doubt, you can ask the person that intentionally set it to null what they wanted to achieve.
simply the variable/function you are trying to access from the class does not exist
someClass.xyz();
above will give the error
NoSuchMethod: the method 'xyz' was called on null
because the class someClass does not exist
The following will work fine
// SomeClass created
// SomeClass has a function xyz
class SomeClass {
SomeClass();
void xyz() {
print('xyz');
}
}
void main() {
// create an instance of the class
final someClass = SomeClass();
// access the xyz function
someClass.xyz();
}

Preserving bundled char sequence

So my activity depends on a character sequence sent to it as such
id = bundle.getCharSequence("id").toString();
This works - except every now and then if I exit the activity and return to it the data I need from
item = realm.where(LogEntry.class).contains("id", id).findFirst();
Gets garbage collect? (not sure) - but I get a lot of errors such as
java.lang.NullPointerException: Attempt to invoke virtual method 'double dataObjects.LogEntry.getLatitude()' on a null object reference
Referring that item is now null which leads to the activity being empty.
I thought making them global would fix this, but obviously it has not. My question is - is there any way to preserve this bundled information?
Try use breakpoints to see the content of "id" and you should try use:
args.putString("id",stringValue); in your arg settings
and retrieve using:
String id = args.getString("id");
Anyway, the problem seems to be LogEntry object, wich is null, the code that you provided doesn't let us know how are you creating the LogEntry object. Use the Run tab to see wich line of your code is making that exception and use breakpoints to see in what part the asignation/creation of LogEntry you are getting null.

Passing String array using intent

i am struck with passing String array from one activity to another using intent. while passing only a String from two activities, its work clear. but whenever i tried to access string array i am getting force close error. can any one guide me to rectify the problem.
You can use just the Intent.setExtra(String, Serializable) method. It accepts any Serializable object. If that does not work, please attach results from your LogCat, "Closed unexpectedly" just means your app failed, LogCat tells why it failed.
use Parcelable to do this.
please refer to this link http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/

Categories

Resources