I wrote a code to get the current position of the device every time the user clicks on a button. But when I call the listener, the app just crashes. Can anyone give me an insight of what I did wrong? Thanks in advance.
public void onStartService()
{
addListenerLocation();
}
#SuppressLint("NewApi")
private void addListenerLocation()
{
mLocationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
LocationListener mLocationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
currentLat = location.getLatitude();
InstanciaObjetos.setPEDLAT(currentLat);
currentLon = location.getLongitude();
InstanciaObjetos.setPEDLON(currentLon);
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
//TODO -- Barney says what should happen
}
public void onProviderEnabled(String provider)
{
Location lastKnownLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastKnownLocation!=null)
{
currentLat = lastKnownLocation.getLatitude();
currentLon = lastKnownLocation.getLongitude();
}
}
public void onProviderDisabled(String provider)
{
}
};
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
}
to start the service I just call onStartService(). Is there anything else I need to do?
I've just attached the logcat. Now I realized what needs to be done (I guess).
09-21 23:17:41.209 8861-8861/br.com.appvendasfs E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.appvendasfs, PID: 8861
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:5253)
at appVendasFS.DAO.PedidosDAO.addListenerLocation(PedidosDAO.java:47)
at appVendasFS.DAO.PedidosDAO.onStartService(PedidosDAO.java:40)
at appVendasFS.DAO.PedidosDAO.Inserir(PedidosDAO.java:97)
at appVendasFS.UI.TelaVenda.Inserir_Pedido(TelaVenda.java:813)
at appVendasFS.UI.TelaVenda.onClick_botao(TelaVenda.java:593)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Call addListenerLocation(); in the service's onCreate() method instead.
Related
I used Firebase authentication in my Android application so when I press the button login when the text fields email and password are empty my application crash. I used Firebase version 10.0.1, This my code :
button_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String email_et = mail_login_et.getText().toString();
final String password_et = pass_login_et.getText().toString();
mAuth.signInWithEmailAndPassword(email_et, password_et)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d("LOGINACT", "signInWithEmail:onComplete:" + task.isSuccessful());
if (task.isSuccessful()) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
} else {
// startActivity(new Intent(LoginActivity.this, MainActivity.class));*
Log.w("LOGINACT", "signInWithEmail:failed", task.getException());
}
}
});
}
});
and this is the error log:
E/UncaughtException: java.lang.IllegalArgumentException: Given String is empty or null
at com.google.android.gms.common.internal.zzac.zzdv(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source)
at com.esprit.couverture4g.activities.LoginActivity$1.onClick(LoginActivity.java:83)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/art: Suspending all threads took: 25.358ms
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.esprit.couverture4g, PID: 3127
java.lang.IllegalArgumentException: Given String is empty or null
at com.google.android.gms.common.internal.zzac.zzdv(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source)
at com.esprit.couverture4g.activities.LoginActivity$1.onClick(LoginActivity.java:83)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
For a few days I've been trying to figure out this multithreading thing on Android Studio. I've only used SwingWorker in the past and after trying several ways to get my https connection to initiate off of the main thread I'm pulling my hair out. I've tried everything from using ASyncTask to just starting a thread everytime the connection gets made, but nothing seems to work. My app crashes every time. The most recent attempt is pictured below.
Edit: It's not a duplicate. I've read that thread and it doesn't do anything to help solve this problem. I'm attempting to execute this task outside of the main thread, but I don't know why my code isn't doing that already since I change to the background thread in run(); and even start a new thread just in case.
public class YelpActivity extends AppCompatActivity implements Runnable{
private yelpLoader load;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yelp);
}
public void run(){
Thread t = new Thread();
t.start();
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
YelpAPI test = new YelpAPI();
test.run();
}
public void yelpClick(View v){
run();
changeYelpResults(v,"test");
}
private void changeYelpResults(View v, String text){
TextView t = (TextView) findViewById(R.id.ResturauntID);
t.setText(text);
}
}
Error Log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: tastr.tastr, PID: 3560
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
at org.scribe.model.Request.send(Request.java:70)
at org.scribe.model.Request.send(Request.java:76)
at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:132)
at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89)
at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146)
at tastr.tastr.YelpAPI.run(YelpAPI.java:190)
at tastr.tastr.YelpActivity.run(YelpActivity.java:26)
at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31)
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at org.scribe.model.Response.<init>(Response.java:29)
at org.scribe.model.Request.doSend(Request.java:117)
at org.scribe.model.Request.send(Request.java:66)
at org.scribe.model.Request.send(Request.java:76)
at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:132)
at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89)
at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146)
at tastr.tastr.YelpAPI.run(YelpAPI.java:190)
at tastr.tastr.YelpActivity.run(YelpActivity.java:26)
at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31)
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
public class YelpActivity extends AppCompatActivity{
private yelpLoader load;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yelp);
}
public void run(){
new Thread(new Runnable() {
#Override
public void run() {
YelpAPI test = new YelpAPI();
test.run();
}
}).start();
}
public void yelpClick(View v){
run();
changeYelpResults(v,"test");
}
private void changeYelpResults(View v, String text){
TextView t = (TextView) findViewById(R.id.ResturauntID);
t.setText(text);
}
}
Note: It's not a good practice to implement Runnable in Activity
class. Use Core Java threads like I did in the run method or Use other
Android Background mechanism like AsyncTask/IntentService.
I have an array of pictures which i extract the longitude and latitude details from using exif and put them on a marker on the GPS map. Currently i am using static array which i plan to change to a dynamic array in the near future. The current code crashes my app everytime i try to set my marker, i have no idea whats wrong.
public void setMarker() {
double latitude, longitude;
String[] filepath = {"/storage/emulated/0/Pictures/CapturyGallery/CapImage1451887532716.jpg", "/storage/emulated/0/Pictures/CapturyGallery/CapImage1452411474153.jpg"};
ExifInterface intf = null;
for (int y = 0; y < filepath.length; y++) {
try {
intf = new ExifInterface(filepath[y]);
} catch (IOException e) {
e.printStackTrace();
}
if (intf != null) {
latitude = Double.parseDouble(intf.getAttribute(ExifInterface.TAG_GPS_LATITUDE));
longitude = Double.parseDouble(intf.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));
LatLng latLngCurrent = new LatLng(latitude, longitude);
MarkerOptions options = new MarkerOptions()
.position(latLngCurrent)
.title("test");
mMap.addMarker(options);
}
}
}
LOGCAT
`
01-11 15:19:30.251 32226-32226/com.sp.fyp.captury E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.sp.fyp.captury, PID: 32226
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4298)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6843)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4293)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6843)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NumberFormatException: Invalid double: "1/1,18/1,30/1"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at com.sp.fyp.captury.GPS.setMarker(GPS.java:181)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4293)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6843)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)`
According to the Standard Exif Tags (as defined in the Exif 2.3 standard)
The latitude/longitude is expressed as three RATIONAL values giving
the degrees, minutes, and seconds, respectively. When degrees, minutes
and seconds are expressed, the format is dd/1,mm/1,ss/1. When degrees
and minutes are used and, for example, fractions of minutes are given
up to two decimal places, the format is dd/1,mmmm/100,0/1
Thus, when you get the latitude and logitude from the ExifInterface, you are getting values like 1/1,18/1,30/1. Of course this String values cannot be converted to double using Double.parseDouble and throw a NumberFormatException.
You can take a look at How do I convert Exif long/lat to real values? to find a converter.
I am getting Null Pointer Exception on line 219: amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);. It was not happening before, today i saw it first time, i am using Android 5.0. Code mutes notification sound when activity starts and unmutes when it is destroyed.
Code:
private AudioManager amanager;
#Override
protected void onCreate(Bundle savedInstanceState) {
amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
}
#Override
protected void onResume() {
super.onResume();
if(amanager!=null) {
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
}
}
#Override
protected void onPause() {
if(amanager!=null) {
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
}
super.onPause();
}
Error:
java.lang.RuntimeException: Unable to resume activity {com.jemshit.itu/com.jemshit.nfcAttendance.ClassActivity}: java.lang.NullPointerException: Null pointer exception during instruction 'invoke-static {v0}, android.os.IBinder android.media.AudioService$VolumeStreamState$VolumeDeathHandler.access$5300(android.media.AudioService$VolumeStreamState$VolumeDeathHandler) // method#11492'
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3346)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Null pointer exception during instruction 'invoke-static {v0}, android.os.IBinder android.media.AudioService$VolumeStreamState$VolumeDeathHandler.access$5300(android.media.AudioService$VolumeStreamState$VolumeDeathHandler) // method#11492'
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1493)
at android.media.IAudioService$Stub$Proxy.setStreamMute(IAudioService.java:992)
at android.media.AudioManager.setStreamMute(AudioManager.java:1466)
at com.jemshit.nfcAttendance.ClassActivity.onResume(ClassActivity.java:219)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
at android.app.Activity.performResume(Activity.java:6338)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3335)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
It's an Android bug it's not your code. Put a try/catch block in your code to avoid the crash.
I'm getting an Exception when I'm executing this code:
public void onClick(View view){
(...)
CharSequence text1 = "Please insert a number.";
int id= view.getId();
(...)
else if(id== R.id.buttonOK){
if(editText.getText().toString().matches(""))
{
Toast.makeText(view.getContext(), text1, Toast.LENGTH_SHORT).show();
}
The exception is when Toast.makeText is executed. I've already tried as 1st argument:
getApplicationContext()
this
MyActivity.this
view.getContext()
and I always get this error. Can you please help me?
Exception:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
You can use the instance of Activity, for example:
MainActivity.this
In your case:
Toast.makeText(MainActivity.this, text1, Toast.LENGTH_SHORT).show();
If the error is in the code that you had given is then :
//make sure that you had declared and defined editext properly
else if(id== R.id.buttonOK){
if(editText.getText().toString().equals(""))
{
Toast.makeText(view.getContext(), "String", Toast.LENGTH_SHORT).show();
}