改造中的空对象引用

时间:2018-08-10 13:20:46

标签: android api retrofit2

有人知道为什么我有空对象引用吗?我尝试了很多解决方案,却一无所获。 Logcat和Android Profiler显示api数据,但我无法解决该问题。有什么想法吗?

public class MainActivity extends AppCompatActivity implements MainInterface {


@BindView(R.id.city_textview) TextView city_textview;
@BindView(R.id.temp_textview) TextView temp_textview;
@BindView(R.id.icon_imageview) ImageView icon_imageview;

private String TAG = "MainActivity";
private double latitude;
private double longitude;
private Currently currently;
private Currently currently1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(this);

    getWeather(currently);
    getCurrentConditions(currently1);

}


@Override
public  void getWeather(final Currently currently) {
    latitude = 37.8267;
    longitude = -122.4233;
    NetworkClient.getInstance().getWeather(latitude, longitude).enqueue(new Callback<Currently>() {

        @Override
        public void onResponse(@NonNull Call<Currently> call, @NonNull final Response<Currently> response) {
            getCurrentConditions(response.body());
        }

        @Override
        public void onFailure(Call<Currently> call, Throwable t) {
            Log.d(TAG, "Failed" + t.getMessage());
        }
    });



}


public void getCurrentConditions(Currently currentlyweather) {
    String timezone = currentlyweather.getTimezone();
   city_textview.setText(String.valueOf(currentlyweather.getTimezone()));

}
}

2 个答案:

答案 0 :(得分:0)

尝试此代码...

public class ApiClient {
private final static String BASE_URL = "https://api.github.com"; // define your url..

public static ApiClient apiClient;
private Retrofit retrofit = null;

public static ApiClient getInstance() {
    if (apiClient == null) {
        apiClient = new ApiClient();
    }
    return apiClient;
}

//private static Retrofit storeRetrofit = null;

public Retrofit getClient() {
    return getClient(null);
}


private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;
}

}

和api这样调用create接口。

public interface ApiInterface {
@POST("registrion")
Call<ResponseData> userReg(@Body User user);

}

并以这种方式在活动或片段中调用api。

 ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
    User user=new User();
    user.setEmail("abcd@gmail.com");
    user.setPassword("123");
    user.setFirstName("Vikas");
    Call<ResponseData> dataCall=apiInterface.userReg(user);
    dataCall.enqueue(new Callback<ResponseData>() {
        @Override
        public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
            if (response!=null && response.isSuccessful() && response.body()!=null){
                // show message
            }
        }

        @Override
        public void onFailure(Call<ResponseData> call, Throwable t) {
        Log.d("Error",t.getMessage());
        }
    });

答案 1 :(得分:0)

在您调用的onCreate()

getCurrentConditions(currently1);

由于您未将其作为第一个参数提供的变量currently1null。 显然,这意味着getCurrentConditions方法中定义的两行代码都将抛出NullPointerException

尝试为currently1变量设置一个值。