illegalStateException about snackBar - android

I got illegalStateException when I compiled my snackbar. The result of App is that I can execute the first button but once I tried to use my second button the app will crush.
The code and error message are shown like this:
public void callIntent(View view) {
Intent intent = null;
switch (view.getId()) {
case R.id.ZhaoDisplay:
Toast toast = Toast.makeText(getApplicationContext(),
"Zhao Wang, 991556434; Website Name: Amazon", Toast.LENGTH_SHORT);
toast.show();
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.amazon.ca"));
startActivity(intent);
break;
case R.id.WangDisplay:
intent = new Intent(this, WangActivity.class);
//TextMulti
editInput = (EditText)findViewById(R.id.TextMulti);
String userInput = editInput.getText().toString();
intent.putExtra("valueFromUser", userInput);
Snackbar snackbar = null;
snackbar = Snackbar.make(findViewById(R.id.content),
"My Second Screen_Zhao Wang 991556434", 10);
snackbar.show();
startActivity(intent);
break;
default:
break;
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: zhao.wang.s991556434, PID: 15778
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:158)
at zhao.wang.s991556434.ZhaoActivity.callIntent(ZhaoActivity.java:60)
at java.lang.reflect.Method.invoke(Native Method)
I tried to put snackbar code part to my second activity and compile it but it showed the same error. May I ask for assistance about this? Thanks.

The issue is with passing a wrong view for the snackbar make method, instead try the following
snackbar = Snackbar.make(findViewById(android.R.id.content),
"My Second Screen_Zhao Wang 991556434", 10);
snackbar.show();

Related

Android api 29 and more Request permissions locations allow all the time

I want to get Android API 29 and above background location permissions and open the Android application location settings section and I want it to select "Allow all the time". But it gives an "ActivityNotFoundException" error, I tried a lot but I couldn't solve it, can anyone write an idea?
requestLocationPermission=registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){
if(ContextCompat.checkSelfPermission(requireContext(),Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(requireContext(),Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q){
requestLocationPermission.launch(arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION)).apply {
var intentGo = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS,Uri.parse("package: "+ requireContext().packageName))
println("paket name :" + requireContext().packageName)
startActivity(intentGo)
}
}
2022-07-26 18:09:08.498 8773-8773/com.crossplatform.appName/AndroidRuntime: FATAL EXCEPTION: main
Process: com.crossplatform.appName, PID: 8773
java.lang.RuntimeException: Failure delivering result ResultInfo{who=#android:requestPermissions:, request=1987209647, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.crossplatform.appName/com.crossplatform.appName.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.LOCATION_SOURCE_SETTINGS dat=package: com.crossplatform.appName}
at android.app.ActivityThread.deliverResults(ActivityThread.java:4845)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4886)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.LOCATION_SOURCE_SETTINGS dat=package: com.crossplatform.appName}
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2051)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1709)
at android.app.Activity.startActivityForResult(Activity.java:5192)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:705)
at android.app.Activity.startActivityForResult(Activity.java:5150)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:686)
at android.app.Activity.startActivity(Activity.java:5521)
at androidx.core.content.ContextCompat$Api16Impl.startActivity(ContextCompat.java:978)
at androidx.core.content.ContextCompat.startActivity(ContextCompat.java:318)
at androidx.fragment.app.FragmentHostCallback.onStartActivityFromFragment(FragmentHostCallback.java:167)
at androidx.fragment.app.Fragment.startActivity(Fragment.java:1445)
at androidx.fragment.app.Fragment.startActivity(Fragment.java:1433)
at com.crossplatform.appName.ui.account_other.Notifications.onViewCreated$lambda-3(Notifications.kt:196)
at com.crossplatform.appName.ui.account_other.Notifications.$r8$lambda$1iUgnMZF1SZtSKvOTXdmnDzob8k(Unknown Source:0)
at com.crossplatform.appName.ui.account_other.Notifications$$ExternalSyntheticLambda6.onActivityResult(Unknown Source:4)
at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:413)
at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:370)
at androidx.activity.ComponentActivity.onRequestPermissionsResult(ComponentActivity.java:795)
at androidx.fragment.app.FragmentActivity.onRequestPermissionsResult(FragmentActivity.java:490)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:8264)
at android.app.Activity.dispatchActivityResult(Activity.java:8114)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4838)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4886) 
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
2022-07-26 18:09:09.092 8773-8773/com.crossplatform.appName I/Process: Sending signal. PID: 8773 SIG: 9

Google places Autocomplete error: "Intent expected to contain a Place, but doesn't"

I am creating an simple app where user types any address and gets suggestions, after clicking on any suggestion it will be displayed in a textview.
Autcomplete initialization:
try {
val fields = listOf(
Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG,
Place.Field.ADDRESS)
val intent=Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this)
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE)
}catch (e: Exception){
e.printStackTrace()
}
onActivityResult() code:
if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE){
val place = Autocomplete.getPlaceFromIntent(data!!)
et_location.setText(place.address)
mLatitude=place.latLng!!.latitude
mLongitude=place.latLng!!.longitude
}
The problem is that whenever I type a single alphabet in the Autocomplete, the application crashes saying Intent expected to contain a Place, but doesn't.
What is the problem with my code?
This question didn't help me as I want place.address to return a string address.
logcat:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=2, data=Intent { (has extras) }} to activity {com.example.happyplaces/com.example.happyplaces.activities.AddHappyPlaceActivity}: java.lang.IllegalArgumentException: Intent expected to contain a Place, but doesn't.
at android.app.ActivityThread.deliverResults(ActivityThread.java:4268)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalArgumentException: Intent expected to contain a Place, but doesn't.
at com.google.android.libraries.places.internal.zzft.zza(com.google.android.libraries.places:places##2.4.0:5)
at com.google.android.libraries.places.internal.zzeg.zza(com.google.android.libraries.places:places##2.4.0:3)
at com.google.android.libraries.places.widget.Autocomplete.getPlaceFromIntent(com.google.android.libraries.places:places##2.4.0:2)
at com.example.happyplaces.activities.AddHappyPlaceActivity.onActivityResult(AddHappyPlaceActivity.kt:253)
at android.app.Activity.dispatchActivityResult(Activity.java:7276)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312) 
at android.app.ActivityThread.-wrap19(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Android TextView Showing Fatal Error in Kotlin

I am trying to initialize TextView in Kotlin as following
val textSelectedFood = findViewById<TextView>(R.id.textSelectedFood) as TextView
I also tried this
val textSelectedFood: TextView = findViewById<TextView>(R.id.textSelectedFood) as TextView
And also this
val textSelectedFood: TextView = findViewById(R.id.textSelectedFood) as TextView
And this
val textSelectedFood: TextView = findViewById<TextView>(R.id.textSelectedFood)
But still getting following errors. Don't know why
2021-01-10 23:38:26.640 29636-29636/com.imran.android.dinnerdecider E/d.dinnerdecide: Unknown bits set in runtime_flags: 0x8000
2021-01-10 23:38:27.091 29636-29636/com.imran.android.dinnerdecider E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.imran.android.dinnerdecider, PID: 29636
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.imran.android.dinnerdecider/com.imran.android.dinnerdecider.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
at android.content.Context.obtainStyledAttributes(Context.java:738)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
at com.imran.android.dinnerdecider.MainActivity.<init>(MainActivity.kt:12)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
I got hints from this question.
but the code changed a lot over the year.
That's why I am writing the current solution code in the below which solved my problem.
private lateinit var textSelectedFood: TextView // withoud lateinit code will show error
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textSelectedFood = findViewById(R.id.textSelectedFood)
}
If you do not have to "findViewById" have many better and safer options:
1.synthetic (deprecated)
Call the view directly in your code.
2.view binding
For more information check this link:https://developer.android.com/topic/libraries/view-binding
This technique is old now, just use "View binding".
Or you can just call the view by it's id and that'll be it.
In your case:
textSelectedFood.text = "Example of doing something"

Kotlin: How to call setText() using a Float data type?

How do I call setText() using float data type? I have already tried this
if (response.isSuccessful) {
val destination = response.body()
destination?.let {
client_names.setText(destination.client_name)
product_names.setText(destination.product_name)
amounts.setText(String.valueOf(destination.amount)) ----->
prices.setText(String.valueOf(destination.price)) ----->
comments.setText(destination.comment)
collapsing_toolbar.title = destination.client_name + destination.product_name
}
I have tried this answer from this link
How to call setText() using a Float data type?
but I get this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smartherd.globofly, PID: 8999
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at com.smartherd.globofly.activities.DestinationDetailActivity$loadDetails$1.onResponse(DestinationDetailActivity.kt:58)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 8999 SIG: 9
If you don't need any formatting just do:
setText("${destination.amount}")

android button causing crash [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am trying to pass login details to another class so that it can be used to login on my MySQL db (using php) but it is crashing giving me the exception: java.lang.IllegalStateException: Could not execute method for android:onClick
stack trace: {StackTraceElement[0]#5692}
658 10752-10752/uk.ac.abertay.contactslist E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.ac.abertay.contactslist, PID: 10752
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void uk.ac.abertay.contactslist.Login.Logino(uk.ac.abertay.contactslist.LoginDetails)' on a null object reference
at uk.ac.abertay.contactslist.LoginActivity.Loginbutton(LoginActivity.java:40)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
04-26 15:39:34.761 1384-3633/? I/chatty: uid=1041(audioserver) writer identical 8 lines
this is the code for the button
private Login login;
public void Loginbutton(View v) {
/* Get entered fields data. */
String email = field_email.getText().toString();
String password = field_password.getText().toString();
login.Logino(new LoginDetails(email,password));
}
when debugging it will run this and go back to the button and after that it will give the exception and crash
public class LoginDetails {
public String email = "";
public String password = "";
LoginDetails(String e, String p) {
email = e;
password = p;
}
}
this is the code I'm trying to run with the button above
public void Logino(LoginDetails loginDetails) {
LoginTask task = new LoginTask();
task.execute(loginDetails);
}
From your StackTrace
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void uk.ac.abertay.contactslist.Login.Logino(uk.ac.abertay.contactslist.LoginDetails)
The line login.Logino the object login is null, you have to initialize it.
Obs:Please use Java naming conventions with method names starting with low case char.

Categories

Resources