无法在AsyncTask中的onPostExecute中解析构造函数ArrayAdapter

时间:2016-04-28 11:12:28

标签: android listview android-asynctask android-arrayadapter

我知道这可能听起来像一个重复的问题,但我已经尝试了所有的解决方案没有任何作用。它显示错误“无法解析构造函数ArrayAdapter”我在Mainactivity中有listview,我已经传递给我正在搜索的第二个活动远程数据库并在列表视图中显示结果。 看看..谢谢你......      这是主要活动:

public class Main2Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private EditText location;
private TextView result ;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    location = (EditText)findViewById(R.id.editText1);
    result = (TextView)findViewById(R.id.textView4);
    lv = (ListView) findViewById( R.id.lv );
 }
 public void search(View view){
    String slocation = location.getText().toString();
    new SearchActivity(this,lv,1).execute(slocation);
}

SearchActivity

public class SearchActivity extends AsyncTask<String,Void,String[]>{
private Context context;
private int byGetOrPost = 0;
private TextView sresult;
ListView lv1;
JSONArray garages;
String[] names;

public SearchActivity(Context context,ListView lv1,int flag) {
    this.context = context;
    byGetOrPost = flag;
    this.lv1 = lv1;
}

protected void onPreExecute(){

}

@Override
protected String[] doInBackground(String... arg0) {
        try{
            String location = (String)arg0[0];

            String link="http://garagekhojo.in/androidtest.php";
            String data  = URLEncoder.encode("location", "UTF-8") + "=" + URLEncoder.encode(location, "UTF-8");

            URL url = new URL(link);
            URLConnection conn = url.openConnection();

            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

            wr.write( data );
            wr.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                sb.append(line);
                break;
            }

            String newsb = sb.toString();
            JSONObject jo = new JSONObject(newsb) ;
            garages = jo.getJSONArray("garages");
            for (int i = 0; i < garages.length(); i++) {
                JSONObject c = garages.getJSONObject(i);

                names[i] = c.getString("name");
            }
            return names;


        }
        catch(Exception e){
            return null;
        }

}

@Override
protected void onPostExecute(String[] names){
    ArrayAdapter<String> adapter;
    adapter = new ArrayAdapter<String>(this, R.layout.class,names);
    lv1.setAdapter(adapter);
}

}

2 个答案:

答案 0 :(得分:1)

您必须编辑SearchActivity.java。首先将其命名为SearchAsyncTask。你没有初始化name []字符串数组,而不是为什么它会抛出空指针异常。

以下是SearchAsyncTask.java的代码

public class SearchAsyncTask extends AsyncTask<String,Void,String[]>{
private Context context;
private int byGetOrPost = 0;
private TextView sresult;
ListView lv1;
JSONArray garages;

public SearchActivity(Context context,ListView lv1,int flag) {
   this.context = context;
   byGetOrPost = flag;
   this.lv1 = lv1;
}

protected void onPreExecute(){

}

@Override
protected String[] doInBackground(String... arg0) {
     try{
        String location = (String)arg0[0];

        String link="http://garagekhojo.in/androidtest.php";
        String data  = URLEncoder.encode("location", "UTF-8") + "=" + URLEncoder.encode(location, "UTF-8");

        URL url = new URL(link);
        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

        wr.write( data );
        wr.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while((line = reader.readLine()) != null)
        {
            sb.append(line);
            break;
        }

        String newsb = sb.toString();
        JSONObject jo = new JSONObject(newsb) ;
        garages = jo.getJSONArray("garages");
        String[] names = new String[garages.length()];
        for (int i = 0; i < garages.length(); i++) {
            JSONObject c = garages.getJSONObject(i);
            names[i] = c.getString("name");
        }
        return names;
    }
    catch(Exception e){
        return null;
    }
}

@Override
protected void onPostExecute(String[] names){
    if(names!=null){
       ArrayAdapter<String> adapter;
       adapter = new ArrayAdapter<String>(context, R.layout.class,names);
       lv1.setAdapter(adapter);
    }
}

您也应该应用null检查,因为在异常的情况下,您将返回null作为值。所以如果发生任何异常,它将再次崩溃。此外,您不需要定义全局变量String []名称,因为您没有使用该变量。我也编辑了班级的班级和名称。请检查。

答案 1 :(得分:0)

请将活动的上下文传递给异步任务并使用代替此

.controller('YourCtrl', function($ionicNavBarDelegate) {
     $ionicNavBarDelegate.showBackButton(false);
 })

上下文来自 adapter = new ArrayAdapter<String>(context, R.layout.class,names);

并且从不使用Activity作为AsyncTask的后缀(不良做法) (而不是SearchActivity使用SearchAsyncTask)