IllegalArgumentException (java) from a class which extends RealmObject - java

I am getting this error:
IllegalArgumentException
while performing the below method. I have no idea why it is happening.
Any idea whats wrong here ??
public void sendNoteWithoutImage(){
Toast.makeText(getContext(), "Step 1", Toast.LENGTH_LONG).show();
// saving objects
Note notesRealmClass = new Note();
notesRealmClass.setTitle(titleStr);
Toast.makeText(getContext(), "Step 2", Toast.LENGTH_LONG).show();
ChannelIDs = TextUtils.join(" ",selectedItems);
Toast.makeText(getContext(), "Step 3", Toast.LENGTH_LONG).show();
notesRealmClass.setObjId(objId);
Toast.makeText(getContext(), "Step 4", Toast.LENGTH_LONG).show();
// save object asynchronously
Backendless.Persistence.save(notesRealmClass, new AsyncCallback<Note>() {
public void handleResponse(Note note) {
Toast.makeText(getContext(), "Step 5", Toast.LENGTH_LONG).show();
// new Contact instance has been saved
Toast.makeText(getActivity(), "Successfully posted ", Toast.LENGTH_SHORT).show();
}
public void handleFault(BackendlessFault fault) {
Toast.makeText(getContext(), "Step 6", Toast.LENGTH_LONG).show();
Log.d("ERROR : ", "" + fault.getMessage());
Log.d("ERROR Code: ",""+fault.getCode());
Toast.makeText(getActivity(), "" + fault.getMessage(), Toast.LENGTH_SHORT).show();
// an error has occurred, the error code can be retrieved with fault.getCode()
}
});}
As you can see i put numbered toasts to check which parts of the codes are executing. From step 1 to 4, everything is fine, but not in step 5. I am getting an error directly on step 6 and the error's print is:
02-18 12:54:09.025 25161-25161/pb.package D/ERRORĀ :: rx/Observable
02-18 12:54:09.025 25161-25161/pb.package D/ERRORĀ Code:: IllegalArgumentException

Your issue should be when creating your Toast inside AsyncCallBack. Because you're using it inside an anonymous class, AsyncCallBack, you can't simply call getContext or getActivity because you are no longer in the scope of the Activity. Try this:
Toast.makeText(NameOfYourActivityClass.this, "Successfully posted ", Toast.LENGTH_SHORT).show();
For example, let's say your Acitivity is called "NotesActivity" then you would do:
Toast.makeText(NotesActivity.this, "Successfully posted ", Toast.LENGTH_SHORT).show();

Related

Toast appearing at inappropriate time

I have created if else statement in which if the user is registered, toast will display "User already registered", otherwise it will register the user and another toast will display "User registered successfully". But when i hit register button, both the toasts appear instead of just "user registered successfully". Tell me how the first toast should not appear with it. Below is my code.
if (dataSnapshot.child(username2et.getText().toString()).exists()) {
mDialog.dismiss();
Toast.makeText(RegisterScreen.this, "Username already registered", Toast.LENGTH_SHORT).show();
} else {
mDialog.dismiss();
User user = new User(emailet.getText().toString(), password2et.getText().toString(), phonenoet.getText().toString(), carplatenoet.getText().toString());
table_user.child(username2et.getText().toString()).setValue(user);
Toast.makeText(RegisterScreen.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
finish();
}

mToast.cancel() is not working

I made a method to make sure my toast messages will be displayed immediately, without having to wait the previous toast to dissapear. The method :
public void myToaster(String message){
if(mToast!=null){
mToast.cancel();
}
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
I'm using Android Studio with API23.
mToast is never assigned/reassigned . This
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
should be
mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
mToast.show();
Toast.cancel doesn't remove the current Toast immediately. The fade out animation takes place nevertheless

Java Android Toast not showing after boolean declaration

The toast that says not reachable is not showing up, but all the above are showing up, why does my code stop execution on that line?
if (msg_from.equals(MainActivity.PHONENUMBER)){
if (msgBody.startsWith("Your")){
//have seen this toast
Toast.makeText(context, "Yes!!", Toast.LENGTH_SHORT).show();
//have seen this toast
LocationManager imLoca = SysService.locationMan;
Toast.makeText(context, "Yes 2!!", Toast.LENGTH_SHORT).show();
//have seen
boolean gpsEnabled = imLoca.isProviderEnabled(LocationManager.GPS_PROVIDER);
Toast.makeText(context, "not reachable", Toast.LENGTH_SHORT).show();
//but this does not show up, and any thing after this line is not working;
boolean netOn = imLoca.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsEnabled){
//do some thing
}
else if (netOn){
//do some thing
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
}
}
I copied this code into my test project and added a toast to the if and the else if for gpsEnabled and netOn.
It looks like the first if is the one that is getting executed as gpsEnabled is true.
Hope this helps, good luck.
if (gpsEnabled){
//do some thing
Toast.makeText(context, "1", Toast.LENGTH_SHORT).show();
}
else if (netOn){
//do some thing
Toast.makeText(context, "2", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
Imloca returns null.Problem is this.

NPE in an in app purchase

This is my code, an attempt to launch an in app purchase when a button is pressed:
purchaseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(purchased.equals("remove_ads")) {
Toast.makeText(getApplicationContext(), "You already own the item.",
Toast.LENGTH_LONG).show();
}
else{
try{
Bundle buyIntentBundle = mservice.getBuyIntent(3, getPackageName(),
"remove_ads", "inapp", "key");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
try{
startIntentSenderForResult(pendingIntent.getIntentSender(), ///NPE here
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
}
catch(IntentSender.SendIntentException ee){
Toast.makeText(getApplicationContext(), "Error was: " + ee,
Toast.LENGTH_LONG).show();
}
catch(NullPointerException n){
Toast.makeText(getApplicationContext(), "Error was: " + n,
Toast.LENGTH_LONG).show();
}
}
catch (RemoteException e){
Toast.makeText(getApplicationContext(), "Error was: " + e,
Toast.LENGTH_LONG).show();
mHelper.flagEndAsync();
mHelper.launchPurchaseFlow(store.this, "remove_ads", 10001,
mPurchaseFinishedListener, "key");
}
}
}
});
and for some reason I get the NPE at the line indicated startIntentSenderForResult... and I don't understand what could cause it. Previously I used this code in another in app purchase exactly as shown except it was a different sku. Could it make a difference since I have 2 identical copies of this code block shown in the same class? Stacktrace doesn't show anything useful either, just the NPE.

Activity context.this cannot be resolved to a variable (inside Toast)

I'm trying to insert a button to rate app in my activity, with a toast for if market isn't found. But I'm getting a "Context cannot be resolved to a variable" on Activity.this:
Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
Toast.makeText(Activity.this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
}
I've also tried:
Toast.makeText(this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
But then I get Multiple markers at this line
- The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
I've made a simple button toast the same way (without try/catch) before, and then it worked fine..
What have I done wrong?
If your class is extending with Activity means use like this
Toast.makeText(ClassName.this, "Couldn't launch the market",Toast.LENGTH_LONG).show();
or
Toast.makeText(getApplicationContext(), "Couldn't launch the market",Toast.LENGTH_LONG).show();
If your Class is extending with Fragment means use like this:
Toast.makeText(getActivity(), "Couldn't launch market",Toast.LENGTH_LONG).show();
Your answer:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LENGTH_LONG).show();
Try:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LEGTH_LONG).show();
Try this...
Uri uri = Uri.parse("market://details?id="
+ getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't launch the market", Toast.LENGTH_LONG)
.show();
}
});
}
Hope this will help you...

Categories

Resources