InvocationTargetException : Could not execute method of the activity - android

I am new in Android and I want to put Click event on XML instead of java, and I have implemented following method on Activity class
public void onInfoClick(View v){
switch(v.getId()) {
case R.id.ivAcceptAmericanExpress:
showPopup(v, getString(R.string.apt_american_express));
break;
}
}
and in XML , I have done following code
<ImageView
android:id="#+id/ivAcceptAmericanExpress"
android:layout_width="24.286dp"
android:layout_height="24.286dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/txtAcceptAmericanExpress"
android:src="#drawable/info_image"
android:onClick="onInfoClick" />
but when I click on Image I am getting following error
Could not execute method of the activity
android.view.View$1.onClick(View.java:4021)
android.view.View.performClick(View.java:4781)
android.view.View$PerformClick.run(View.java:19873)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:135)
android.app.ActivityThread.main(ActivityThread.java:5291)
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke(Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699):: Cause : java.lang.reflect.InvocationTargetException
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke(Method.java:372)
android.view.View$1.onClick(View.java:4016)
android.view.View.performClick(View.java:4781)
android.view.View$PerformClick.run(View.java:19873)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:135)
android.app.ActivityThread.main(ActivityThread.java:5291)
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke(Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

The InvocationTargetException usually points to an exception in the onClick method which you are trying to execute - in your case the onInfoClick method - you can confirm this by commenting out your case-statement and see if you still get the error. Once you have identified the problem - you can make the necessary changes to resolve the exception. You should probably surround the body of the method with a try-catch to be safe.

Related

Twitter button click crashing app ? Unable to extract the trust manager on okhttp3.internal.Platform$Android

issues is with Twitter button, this is working fine with Android 6 Moto G, while on android 5.0.1 Samsung Note 3 it crashes, on Simulator it works fine too,
However my code for Click is,
TwitterLoginButton twitterLoginButton = (TwitterLoginButton) findViewById(R.id.button_twitter);
twitterLoginButton.setCallback(new Callback<TwitterSession>() {
#Override
public void success(Result<TwitterSession> result) {
Log.d(TAG, "twitterLogin:success" + result);
handleTwitterSession(TwitterAuthProvider.getCredential(
result.data.getAuthToken().token,
result.data.getAuthToken().secret), false);
}
#Override
public void failure(TwitterException exception) {
Log.w(TAG, "twitterLogin:failure", exception);
}
});
twitterLoginButton.setOnClickListener(this);
Issues it is showing is,
java.lang.RuntimeException:Unable to start activity ComponentInfo{com.my.project/com.twitter.sdk.android.core.identity.OAuthActivity}: java.lang.IllegalStateException: Unable to extract the trust manager on okhttp3.internal.Platform$Android#2e0793c7, sslSocketFactory is class crittercism.android.q
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.IllegalStateException: Unable to extract the trust manager on okhttp3.internal.Platform$Android#2e0793c7, sslSocketFactory is class crittercism.android.q
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:187)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:60)
at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:718)
at com.twitter.sdk.android.core.internal.oauth.OAuthService.<init>(OAuthService.java:56)
at com.twitter.sdk.android.core.internal.oauth.OAuth1aService.<init>(OAuth1aService.java:69)
at com.twitter.sdk.android.core.identity.OAuthActivity.onCreate(OAuthActivity.java:69)
at android.app.Activity.performCreate(Activity.java:6283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5942) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
i searched a lot on google but unable to find any helpful, solution.
This is a problem with Crittercism. If updating that doesn't help you'll need to report the bug to them.

ParseTwitterUtils initialize throws NullPointerException

I am getting a NullPointerException from calling
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
Here is the stack trace
java.lang.RuntimeException: Unable to create application com.myapp.app.android.Application: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5015)
at android.app.ActivityThread.access$1600(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5837)
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:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
at com.parse.Parse.getParseDir(Parse.java:313)
at com.parse.ParseCorePlugins.getCurrentUserController(ParseCorePlugins.java:124)
at com.parse.ParseUser.getCurrentUserController(ParseUser.java:56)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:892)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:879)
at com.parse.ParseUser.registerAuthenticationProvider(ParseUser.java:1125)
at com.parse.ParseTwitterUtils.getAuthenticationProvider(ParseTwitterUtils.java:20)
at com.parse.ParseTwitterUtils.initialize(ParseTwitterUtils.java:50)
at com.myapp.app.android.Application.onCreate(Application.java:42)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5012)
            at android.app.ActivityThread.access$1600(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            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:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
My app used to work before I added the line ParseTwitterUtils.initialize(...). Here is the onCreate method of my Application class
public void onCreate(){
super.onCreate();
registerDatabaseTables();
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
Parse.enableLocalDatastore(this);
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
}
Unfortunately the instructions on parse.com do not say this, but you have to initialize Parse before you initialize Twitter. This is not obvious since you are required to do so much before you initialize parse. So anyway change your code to this:
public void onCreate(){
super.onCreate();
registerDatabaseTables();
Parse.enableLocalDatastore(this);
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
}

Cloudmine java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory;

I am trying to store some data using Cloudmine.
However, I got the error java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory; when I call initialize() for authorization.
I'm not running on a separate thread or something. I'm testing stuff out right now so I just have it so that when a button is clicked, it uploads some information from some EditText fields.
This is my code
public class ButtonClick implements View.OnClickListener {
private MainWindowActivity mainWindowActivity;
public ButtonClick(MainWindowActivity mainWindowActivity, Button doneBtn, Button addBtn) {
this.mainWindowActivity = mainWindowActivity;
addBtn.setEnabled(true);
addBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// This will initialize your credentials
// ERROR HERE
CMApiCredentials initialize = CMApiCredentials.initialize("id", "key");
SimpleCMObject location = new SimpleCMObject();
location.add("address", Runner.getAddressStr());
location.add("city", Runner.getCityStr());
location.add("state", Runner.getStateStr());
location.add("zip", Runner.getZipStr());
location.save(new ObjectModificationResponseCallback() {
public void onCompletion(ObjectModificationResponse response) {
Toast.makeText(mainWindowActivity, "Location saved: " + response.wasSuccess(), Toast.LENGTH_SHORT).show();
}
});
EditText address = (EditText) mainWindowActivity.findViewById(R.id.address);
address.setText("");
EditText city = (EditText) mainWindowActivity.findViewById(R.id.city);
city.setText("");
EditText state = (EditText) mainWindowActivity.findViewById(R.id.state);
state.setText("");
EditText zip = (EditText) mainWindowActivity.findViewById(R.id.zip);
zip.setText("");
}
}
This is the Logcat:
Process: prog, PID: 26580
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory;
at com.cloudmine.api.CMApiCredentials.<clinit>(CMApiCredentials.java:21)
at prog.ButtonClick.onClick(ButtonClick.java:52)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
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:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.slf4j.LoggerFactory" on path: DexPathList[[zip file "/data/app/prog-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at com.cloudmine.api.CMApiCredentials.<clinit>(CMApiCredentials.java:21)
            at prog.ButtonClick.onClick(ButtonClick.java:52)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            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:5257)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Suppressed: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
As I have commented, download zip file from SLF4J distribution and use as library in your project. Then, use slf4j-api-1.7.12.jar (and slf4j-android-1.7.12.jar if needed).
I had the same error and found this solution on github.
Just add the following to your grandle:
implementation 'org.slf4j:slf4j-api:1.7.25'
https://github.com/nomis/slf4j-android
I hade the same problem only on Android 5.0 - 5.1 (a real device and emulators). At that moment, I used Android Studio 2.3.3 and build tools v26.
I solved the problem turning off Instant Run that I didn't use anyway.
Steps:
go to File -> Settings -> Build, Execution, Deployment -> Instant run
Disable it (first checkbox).

Android Application Subclass Across Activities

I'm running into a NullPointerException, and I can't seem to figure out why. I wouldn't say I'm that experienced in android, so it seems easy for me to get lost when debugging these... :(
I've got an Application subclass defined as follows:
public class UserData extends Application {
private String email, firstName, lastName;
public String getEmail() {
return email;
}
public void setEmail(String str) {
email = str;
}
}
In one activity (loginpage), I set the email string:
UserData user = (UserData)getApplication();
user.setEmail(results);
When I log getEmail(), I see the results in this activity just fine.
In another activity (dashboard), I get the email string as follows:
UserData user = (UserData)getApplication();
Log.d("Email: ", user.getEmail());
This time, user.getEmail() returns a nullpointer. Can someone point out why this is occurring? I've got the subclass defined in my Manifest like this:
<application
android:name=".UserData"></application>
Error Log:
-12 18:10:51.261 19471-19471/habitatfinal.habitatfinal E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: habitatfinal.habitatfinal, PID: 19471
java.lang.RuntimeException: Unable to start activity ComponentInfo{habitatfinal.habitatfinal/habitatfinal.habitatfinal.dashboard}: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:139)
at habitatfinal.habitatfinal.dashboard.onCreate(dashboard.java:29)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            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:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
I found out what was going on. The problem is onPostExecute doesn't necessarily run after the onCreate() method finishes running. Because they are two different threads, the subclass may not be initialized before the program passes the intent into the next activity.
Therefore, the variables in the object weren't set before it hit the new Activity.

Android: java.lang.IllegalStateException when executing Toast.makeText

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();
}

Categories

Resources