为什么下面的代码有问题,我的应用程序一直崩溃

Why does is the problem with the code below,My app keeps crashing

这是我的 Code.This 是独立的 java class MainActivity。 这段代码有什么问题。

package com.leftclick.faithful;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import java.io.IOException;
import org.jsoup.nodes.Document;
import org.jsoup.Jsoup;
import org.jsoup.Connection;

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UpDateManger updatemanger = new UpDateManger(this);
        updatemanger.execute();
    }

    //Update Manager
    private class UpDateManger extends AsyncTask<Void, Void, Void> {
        
        String latestversionurl = "https://raw.githubusercontent.com/Vaibhavraj23/FaithFul/main/latestVersion";
        String version = "0.1.0";
        public String result;
        public AppCompatActivity activity;

        public UpDateManger(AppCompatActivity a)
        {
            this.activity = a;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                //Connect to the website
                Connection connection = Jsoup.connect(latestversionurl);
                connection.timeout(5000);
                connection.maxBodySize(0);
                connection.userAgent("Mozilla/5.0");

                Document document = connection.get();
                this.result = document.toString();

                if(version!=this.result){
                    this.activity.setContentView(R.layout.updatetolatestversion);
                    
                }else{}

            } catch (IOException e) {}
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
    }
        
    
}

这是我的 Code.This 是独立的 java class MainActivity。 这段代码有什么问题。 我认为 UpDateManager Constructor 或 this.activity 变量或 jsoup 模块中存在一些问题 itself.please 帮助我。

我认为问题出在 doInBackground() 方法内部,您正在从后台线程更新视图,这是不可接受的。如果您想更新视图,请尝试使用以下代码。

      private class UpDateManger extends AsyncTask<Void, Void, String> {

    String latestversionurl = "https://raw.githubusercontent.com/Vaibhavraj23/FaithFul/main/latestVersion";
    String version = "0.1.0";
    public String result;
    public AppCompatActivity activity;

    public UpDateManger(AppCompatActivity a) {
        this.activity = a;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Void... voids) {
        try {
            //Connect to the website
            Connection connection = Jsoup.connect(latestversionurl);
            connection.timeout(5000);
            connection.maxBodySize(0);
            connection.userAgent("Mozilla/5.0");

            Document document = connection.get();
            this.result = document.toString();

        } catch (IOException e) {}
        return this.result;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if(version!=result){
            this.activity.setContentView(R.layout.updatetolatestversion);

        }else{}
    }
   }