我的应用程序崩溃了 java.lang.NullPointerException
My app crushed for a java.lang.NullPointerException
我正在尝试用我的 api 和 retrofit 2 开发一个 android 应用程序,但不幸的是我的应用程序多次崩溃,我得到了一个像这样的消息,
java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.apitest.firsttry/com.apitest.firsttry.MainActivity}: java.lang.NullPointerException: 尝试调用接口空对象引用上的方法 'retrofit2.Call com.apitest.firsttry.api.ApiInterface.getAllData()' 和 原因:java.lang.NullPointerException:尝试在空对象引用上调用接口方法 'retrofit2.Call com.apitest.firsttry.api.ApiInterface.getAllData()' 这里是 ApiInterface.java
的代码
import com.apitest.firsttry.Models.DataModels;
import com.apitest.firsttry.Response.GetDataResponse;
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiInterface {
@GET("fetchdata.php")
Call<GetDataResponse> getAllData();
}
这里是 ApiClient.java
的代码
package com.apitest.firsttry.api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
public static Retrofit RETROFIT=null;
public static Retrofit getClient(){
if (RETROFIT==null) {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Gson gson = new GsonBuilder().create();
RETROFIT = new Retrofit.Builder().baseUrl("https://wight-baths.000webhostapp.com/").client(okHttpClient).addConverterFactory(GsonConverterFactory.create(gson)).build();
}
return RETROFIT;
}
}
这是MainActivity的代码
package com.apitest.firsttry;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.apitest.firsttry.Adapter.DataAdapter;
import com.apitest.firsttry.Models.DataModels;
import com.apitest.firsttry.Response.GetDataResponse;
import com.apitest.firsttry.api.ApiClient;
import com.apitest.firsttry.api.ApiInterface;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
DataAdapter dataAdapter;
ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getdata();
initializing();
}
private void initializing(){
recyclerView=findViewById(R.id.recycler_view);
Retrofit retrofit= ApiClient.getClient();
apiInterface=retrofit.create(ApiInterface.class);
}
private void setDataAdapter(List<DataModels>dataModels){
dataAdapter=new DataAdapter(this, dataModels);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(dataAdapter);
}
private void getdata(){
apiInterface.getAllData().enqueue(new Callback<GetDataResponse>() {
@Override
public void onResponse(Call<GetDataResponse> call, Response<GetDataResponse> response) {
try {
if (response!=null){
if (response.body().getStatus().equals("1")){
setDataAdapter(response.body().getData());
}else {
Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){
Log.e("exp",e.getLocalizedMessage());
}
}
@Override
public void onFailure(Call<GetDataResponse> call, Throwable t) {
Log.e("failer",t.getLocalizedMessage());
}
});
}
}
如何解决?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getdata();
initializing();
}
在 调用 getdata()
之前调用 initializing()
。您在initializing()
中设置了apiInterface
,在initializing()
returns之前无法使用。
我正在尝试用我的 api 和 retrofit 2 开发一个 android 应用程序,但不幸的是我的应用程序多次崩溃,我得到了一个像这样的消息, java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.apitest.firsttry/com.apitest.firsttry.MainActivity}: java.lang.NullPointerException: 尝试调用接口空对象引用上的方法 'retrofit2.Call com.apitest.firsttry.api.ApiInterface.getAllData()' 和 原因:java.lang.NullPointerException:尝试在空对象引用上调用接口方法 'retrofit2.Call com.apitest.firsttry.api.ApiInterface.getAllData()' 这里是 ApiInterface.java
的代码
import com.apitest.firsttry.Models.DataModels;
import com.apitest.firsttry.Response.GetDataResponse;
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiInterface {
@GET("fetchdata.php")
Call<GetDataResponse> getAllData();
}
这里是 ApiClient.java
的代码package com.apitest.firsttry.api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
public static Retrofit RETROFIT=null;
public static Retrofit getClient(){
if (RETROFIT==null) {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Gson gson = new GsonBuilder().create();
RETROFIT = new Retrofit.Builder().baseUrl("https://wight-baths.000webhostapp.com/").client(okHttpClient).addConverterFactory(GsonConverterFactory.create(gson)).build();
}
return RETROFIT;
}
}
这是MainActivity的代码
package com.apitest.firsttry;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.apitest.firsttry.Adapter.DataAdapter;
import com.apitest.firsttry.Models.DataModels;
import com.apitest.firsttry.Response.GetDataResponse;
import com.apitest.firsttry.api.ApiClient;
import com.apitest.firsttry.api.ApiInterface;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
DataAdapter dataAdapter;
ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getdata();
initializing();
}
private void initializing(){
recyclerView=findViewById(R.id.recycler_view);
Retrofit retrofit= ApiClient.getClient();
apiInterface=retrofit.create(ApiInterface.class);
}
private void setDataAdapter(List<DataModels>dataModels){
dataAdapter=new DataAdapter(this, dataModels);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(dataAdapter);
}
private void getdata(){
apiInterface.getAllData().enqueue(new Callback<GetDataResponse>() {
@Override
public void onResponse(Call<GetDataResponse> call, Response<GetDataResponse> response) {
try {
if (response!=null){
if (response.body().getStatus().equals("1")){
setDataAdapter(response.body().getData());
}else {
Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){
Log.e("exp",e.getLocalizedMessage());
}
}
@Override
public void onFailure(Call<GetDataResponse> call, Throwable t) {
Log.e("failer",t.getLocalizedMessage());
}
});
}
}
如何解决?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getdata();
initializing();
}
在 调用 getdata()
之前调用 initializing()
。您在initializing()
中设置了apiInterface
,在initializing()
returns之前无法使用。