Java NullPointerException可以';t将文本从类设置为活动

Java NullPointerException可以';t将文本从类设置为活动,java,android,Java,Android,我对编码相当陌生,我的长期目标是创建一个android天气应用程序。我已经创建了主活动和一个类,并且我正在使用Weatherlib使事情变得更简单 主要活动请求许可并使用GPS获取位置。这些坐标被发送到getweather方法,该方法使用OpenWeatherMap获取信息。当检索到数据时,在本例中为cloud percentage,我希望它将其传递给TextView clouds的主活动setText。我的问题是,当我尝试从OnWeatherRetrieve执行此操作时,它会抛出一个NullP

我对编码相当陌生,我的长期目标是创建一个android天气应用程序。我已经创建了主活动和一个类,并且我正在使用Weatherlib使事情变得更简单

主要活动请求许可并使用GPS获取位置。这些坐标被发送到getweather方法,该方法使用OpenWeatherMap获取信息。当检索到数据时,在本例中为cloud percentage,我希望它将其传递给TextView clouds的主活动setText。我的问题是,当我尝试从OnWeatherRetrieve执行此操作时,它会抛出一个NullPointerException

public class MainActivity extends AppCompatActivity {

private double latitude;
private double longitude;
private FusedLocationProviderClient fusedLocationClient;
private String response;
private TextView clouds;
private Context context;
private Context x;
private WeatherOWM weatherOWM;


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

    clouds = findViewById(R.id.clouds);


    x = this;
    context = getApplicationContext();
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    requestPermissions();

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        return;
    }
    fusedLocationClient.getLastLocation().addOnSuccessListener (this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            if (location != null){
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                System.out.println(latitude + " " + longitude);
                WeatherOWM loc = new WeatherOWM(latitude, longitude, context);
                loc.getWeather();
            }
        }
    });

}
public void setClouds(Integer cloud) {
    clouds.setText(cloud);
}

private void requestPermissions(){
    ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, 1);
}
public类MainActivity扩展了AppCompatActivity{
私人双纬度;
私人双经度;
私有FusedLocationProviderClient fusedLocationClient;
私有字符串响应;
私有文本视图云;
私人语境;
私有上下文x;
私人韦瑟奥姆韦瑟奥姆;
//获取位置
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
云=findViewById(R.id.clouds);
x=这个;
context=getApplicationContext();
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
请求权限();
fusedLocationClient=LocationServices.getFusedLocationProviderClient(此);
if(ActivityCompat.checkSelfPermission(此,访问\u FINE\u位置)!=PackageManager.PERMISSION\u已授予){
返回;
}
fusedLocationClient.getLastLocation().addOnSuccessListener(这是新的OnSuccessListener()){
@凌驾
成功时的公共无效(位置){
如果(位置!=null){
双纬度=location.getLatitude();
double longitude=location.getLongitude();
System.out.println(纬度+“”+经度);
WeatherOWM loc=新的WeatherOWM(纬度、经度、上下文);
loc.getWeather();
}
}
});
}
公共云(整型云){
clouds.setText(cloud);
}
私有void requestPermissions(){
requestPermissions(这是一个新字符串[]{ACCESS\u FINE\u LOCATION},1);
}
}

公共类WeatherOWM{
私人双纬度;
私人双经度;
私人背景文学硕士;
WeatherConfig config=新的WeatherConfig();
私有对象配置;
私有对象小时预测;
私人客体活动;
私人活动;
公共天气OWM(双纬度、双经度、上下文ma){
这个。纬度=纬度;
这个经度=经度;
这个。ma=ma;
}
公众天气{
TextView云=新的TextView(ma);
//初始化客户端
WeatherClient=null;
config.ApiKey=“已编辑”;
试一试{
客户端=(新建WeatherClient.ClientBuilder()).attach(ma)
.httpClient(WeatherDefaultClient.class)
.provider(新的OpenweathermapProviderType())
.config(配置)
.build();
}catch(WeatherProviderInstallationE异常){
e、 printStackTrace();
}
getHourForecastWeather(新的WeatherRequest(纬度、经度),新的WeatherClient.HourForecastWeatherEventListener(){
@凌驾
WeatherRetrieved上的公共无效(WeatherHourForecast WeatherHourForecast){
List hourList=weatherHourForecast.getHourForecast();
用于(小时预测小时预测:小时列表){
天气=小时预测。天气;
长时间戳=hourForecast.timestamp;
mainActivity.setClouds(hourForecast.weather.clouds.getPerc());
System.out.println(hourForecast.weather.clouds.getPerc());
}
}
@凌驾
公共无效onWeatherError(WeatherLibException wle){
System.out.println(“错误”);
}
@凌驾
连接错误上的公共无效(可丢弃的t){
System.out.println(“错误”);
}
});
}

}

在WeatherOWM类中,您创建了MainActivity的实例,如下所示:

private MainActivity mainActivity;
在这个方法中,你写了:

mainActivity.setClouds(hourForecast.weather.clouds.getPerc());
问题是变量“mainActivity”没有值,并且是空的,因此必须给它赋值

因此,在WeatherOWM类中,编写以下代码:

mainActivity = new MainActivity();

请回复您的问题是否已解决

main活动
正是一项完整的活动。您不应该自己创建实例
mainActivity = new MainActivity();