我试图从网络API中检索数据。 我使用改造,我为它创建了接口,但是当我尝试使用它时,我有一个异常声明
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)
我的代码:
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());
}
});
}
我认为问题是windchatAPI没有声明,但我不知道该怎么做。 有人能帮助我吗?
windAPI是我保存信息的界面:
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文件:
<?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" />
当我在java代码中删除windAPI.Factory行时......它有效......
编辑:清单已添加
<?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>
答案 0 :(得分:0)
请检查两件事:
Caused by: java.lang.NullPointerException
at com.wind.wind.sign_in.onClicksignin(sign_in.java:57)
RelativeLayout
文件中的XML
是否已关闭?答案 1 :(得分:0)
执行此操作
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());
}
});
}
并从xml中删除onClick