执行时出错,String 不会调用 doInbackground

Error while execute, String won't call into doInbackground

所以我正在尝试调用一个字符串,调用:

EditText EditTextSearch = (EditText)findViewById(R.id.editText1);
if(!isEmpty(EditTextSearch)){
    Toast.makeText(this, "Getting Information", Toast.LENGTH_LONG).show();
    Log.v("checkText =>", EditTextSearch.getText().toString());
    getComics getComicInfo = new getComics(EditTextSearch.getText().toString(), 1);
    getComicInfo.execute();
}

之后我调用 asynctask

class getComics extends AsyncTask<Void, Void, Void>{ 
        private String comicName;
        private String comicNameForSearch;
        private int getOptionNumber;
        String jsonString = "";
        String result = ""; 

     getComics(String comicName, int getOptionNumber)
      { 
         Log.v("Check name A =>", comicName); //show name

      }

    //set option for search
    public void SetOptionNumber(int getOptionNumber){   
        this.getOptionNumber = getOptionNumber;

    }

    public int getOptionNumber(){
        return this.getOptionNumber;
    }

    //the data that the user searching for
    public void SetComicName(String comicName){
        this.comicName = comicName; 
    }

    public String GetComicName(){
        return this.comicName;
    }


/*the request to the API, include fixing space and getting information about the main object, data, later on i'll call 
  the object I got to show the the results array */

    @Override
    protected Void doInBackground(Void... params) {     
        Log.v("is callingB =>", "Yes");
        comicNameForSearch = GetComicName();
        Log.v("check name=>", comicNameForSearch ); // make the app crash

无论如何,如果我试图 Log.v,应用程序崩溃,是什么让我认为 DoInBackground 出于某种原因没有收到 comicName,猜测是因为执行方式,任何想法?

编辑:第一次使用 API 如有任何愚蠢的问题,请见谅 错误 message:Caused 来自:java.lang.nullPointerException:println 需要一条消息

您缺少在 Constructor 中分配值,因此会导致 NullPointerException

改变这个

getComics(String comicName, int getOptionNumber)
  { 
     Log.v("Check name A =>", comicName); //show name
  }

进入

getComics(String comicName, int getOptionNumber)
  { 
     this.comicName = comicName; 
     this.getOptionNumber = getOptionNumber;
     Log.v("Check name A =>", comicName); //show name
  }

您的应用出现异常,因为您在 Asynctask 中打印并传入 doInBackground 的数据为空。

将您的 Asynctask 更改为:

class getComics extends AsyncTask<Void, Void, Void>{ 
        private String comicName;
        private String comicNameForSearch;
        private int getOptionNumber;
        String jsonString = "";
        String result = ""; 

     getComics(String comicName, int getOptionNumber)
      { 
         this.comicName = comicName;
         this.getOptionNumber = getOptionNumber;
         Log.v("Check name A =>", comicName); //show name

      }

    //set option for search
    public void SetOptionNumber(int getOptionNumber){   
        this.getOptionNumber = getOptionNumber;

    }

    public int getOptionNumber(){
        return this.getOptionNumber;
    }

    //the data that the user searching for
    public void SetComicName(String comicName){
        this.comicName = comicName; 
    }

    public String GetComicName(){
        return this.comicName;
    }


/*the request to the API, include fixing space and getting information about the main object, data, later on i'll call 
  the object I got to show the the results array */

    @Override
    protected Void doInBackground(Void... params) {     
        Log.v("is callingB =>", "Yes");
        comicNameForSearch = GetComicName();
        Log.v("check name=>", comicNameForSearch ); // make the app crash

另一种方法是调用您在 class 中创建的 setter 方法,如下所示:

  getComics getComicInfo = new getComics(EditTextSearch.getText().toString(), 1);
getComicInfo .SetComicName(EditTextSearch.getText().toString());
getComicInfo .SetOptionNumber(1);
    getComicInfo.execute();