android
Could not execute method for android:onClick - retrofit
I'm trying to retrieve data from a web api. I use retrofit, I made the interface for it, but when I try to use it, I have an exception declare E/AndroidRuntime: FATAL EXCEPTION: main Process: com.wind.wind, PID: 2388 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:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.wind.wind.sign_in.onClicksignin(sign_in.java:57) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) My code: package com.wind.wind; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; import com.wind.wind.data_user.User; import com.wind.wind.remote.windAPI; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class sign_in extends AppCompatActivity { //EditText fields EditText login_; EditText password_; //String containers private String login; private String password; private windchatAPI api; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_in); login_ = (EditText) findViewById(R.id.editText); password_ = (EditText) findViewById(R.id.editText2); } public void onClicksignin(View v) { login = login_.getText().toString(); password = password_.getText().toString(); windchatAPI.Factory.getInstance().getUser().enqueue(new Callback<User>() { #Override public void onResponse(Call<User> call, Response<User> response) { Log.e("login success", response.body().getToken()); } #Override public void onFailure(Call<User> call, Throwable t) { Log.e("login failed", t.getMessage()); } }); } I think the problem is that windchatAPI is not declare, but I don't know how to do it. Can someone help me ? windAPI is my interface for hold informations: public interface windAPI { #GET("user/register") Call<User> getUser(); String URL = "my_url"; class Factory { private static windAPI service; public static windAPI getInstance() { if (service == NULL) { //creating the base URL from winchat api Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(URL).build(); service = retrofit.create(windAPI.class); return service; } else return service; } } } XML file: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="#+id/activity_sign_in" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="#dimen/activity_vertical_margin" android:paddingLeft="#dimen/activity_horizontal_margin" android:paddingRight="#dimen/activity_horizontal_margin" android:paddingTop="#dimen/activity_vertical_margin" tools:context="com.wind.wind.sign_in" android:background="#fffc00"> <TextView android:text="Login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:layout_marginStart="15dp" android:layout_marginTop="36dp" android:id="#+id/textView" android:textSize="18sp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:layout_below="#+id/textView" android:layout_alignStart="#+id/textView" android:layout_marginTop="19dp" android:id="#+id/editText" android:hint="Enter your login" style="#style/Widget.AppCompat.EditText" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:layout_marginTop="19dp" android:id="#+id/editText2" android:layout_below="#+id/textView2" android:layout_alignStart="#+id/textView2" style="#style/Widget.AppCompat.EditText" android:hint="Enter your password" /> <TextView android:text="Password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="22dp" android:id="#+id/textView2" android:layout_below="#+id/editText" android:layout_alignStart="#+id/editText" android:textSize="18sp" /> <Button android:text="Log in" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:id="#+id/button4" android:background="#android:color/holo_blue_dark" android:layout_below="#+id/editText2" android:layout_centerHorizontal="true" android:layout_width="200dp" android:textColor="#android:color/background_light" android:onClick="onClicksignin" /> When I delete in the java code the line windAPI.Factory... it works... EDIT: manifest added <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wind.wind"> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <!-- To auto-complete the email text field in the login form with the user's emails --> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.READ_PROFILE" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <!-- Used for retrofit api --> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="#string/app_name" android:supportsRtl="true" android:theme="#style/AppTheme"> <activity android:name=".Login_page"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".wind_sent" /> <activity android:name=".MainActivity" /> <activity android:name=".sign_in" /> <activity android:name=".register_activity"></activity> </application>
Please check 2 things: Have you declared your activity in the manifest? I can see you have a Caused by: java.lang.NullPointerException at com.wind.wind.sign_in.onClicksignin(sign_in.java:57) Is your RelativeLayout closed in the XML file?
in your sign_in class do this public class sign_in extends AppCompatActivity implement OnClickListener { //EditText fields EditText login_; EditText password_; //String containers private String login; private String password; private windchatAPI api; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_in); login_ = (EditText) findViewById(R.id.editText); password_ = (EditText) findViewById(R.id.editText2); findViewById(R.id.button4).setOnClickListener(this) } #Override public void onClick(View v) { login = login_.getText().toString(); password = password_.getText().toString(); windchatAPI.Factory.getInstance().getUser().enqueue(new Callback<User>() { #Override public void onResponse(Call<User> call, Response<User> response) { Log.e("login success", response.body().getToken()); } #Override public void onFailure(Call<User> call, Throwable t) { Log.e("login failed", t.getMessage()); } }); } and remove the onClick From the xml
Related Links
Android auto merge two listview in one screen
App crashes on Window.java getContext()
directory in FileOpenStream
Should I modify sync function to a Android Service?
zooming out from a png deforms it
view flipper is not flipping
google-tv emulator appears to start up logo screen, but then freeze the start up screen
Android: How to know whether the notification panel is already open programmatically?
How to call a new activity in the OnClickListener () LazyAdapter
Horizontal and vertical scrolling image gallery (Android)
Google Play Unknown Error in Live Mode
How to handle powerbutton in android
How to split value by semicolon or comma [closed]
Platform Android 4.3, Revision 2 shown as available in online docs but can't be seen in SDK Manager
ViewPager with Nested Fragments?
Where is a Bundle variable saved in Activity?