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

时间:2015-01-22 07:10:50

标签: android android-asynctask execute

所以我试着拨打一个字符串,电话:

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由于某种原因没有收到漫画名称,因为执行方式,任何想法都猜测它?

编辑:第一次使用API​​,如果有任何愚蠢的问题,请对不起 错误消息:引起:java.lang.nullPointerException:println需要一条消息

2 个答案:

答案 0 :(得分:0)

您缺少在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
  }

答案 1 :(得分:0)

您的应用程序正在异常,因为Asynctask中的数据为null,您正在打印并传入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

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

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