Java runnable ListViewAdapter导致一个;无法强制转换为android.content.Context“;错误

Java runnable ListViewAdapter导致一个;无法强制转换为android.content.Context“;错误,java,android,listview,adapter,Java,Android,Listview,Adapter,我目前正在创建一个Android GoogleMaps应用程序,并在在线服务器的数据库中存储了一个位置列表。我正在成功地完成这些操作,现在我想使用Actionbar searchview创建一个用户可以搜索的listview,它将更改当前加载的数据,以仅显示链接到当前选定位置的数据 但是,在将ListViewAdapter代码更改为可运行代码后,尝试将arraylist放入适配器时,由于调用它的位置,我遇到了无法转换为android.content.Context的错误。我的主要活动代码如下:

我目前正在创建一个Android GoogleMaps应用程序,并在在线服务器的数据库中存储了一个位置列表。我正在成功地完成这些操作,现在我想使用Actionbar searchview创建一个用户可以搜索的listview,它将更改当前加载的数据,以仅显示链接到当前选定位置的数据

但是,在将ListViewAdapter代码更改为可运行代码后,尝试将arraylist放入适配器时,由于调用它的位置,我遇到了无法转换为android.content.Context的错误。我的主要活动代码如下:

TownSelector ts;
ListView townList;
ListViewAdapter townAdapter;  <---set variable for adapter here
String[] townID;    
String[] townName;
ArrayList<TownSelector> arraylist = new ArrayList<TownSelector>();

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

    // Hashmap for ListView
    locationList = new ArrayList<HashMap<String, String>>();

    // Locate the ListView in listview_main.xml
    townList = (ListView) findViewById(R.id.listview);

}

class LoadAllInfo extends AsyncTask<String, String, String> {

    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> paramsLocations = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject jsonLocations = jParser.makeHttpRequest(url_all_locations, "GET", paramsLocations);


        //Get all offer
        if(jsonLocations != null)
        {
            // Check your log cat for JSON reponse
            Log.d("All Locations: ", jsonLocations.toString());
            try {

                // Checking for SUCCESS TAG
                int success = jsonLocations.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Offers
                    locations = jsonLocations.getJSONArray(TAG_LOCATIONS);

                    // looping through All Offers
                    for (int i = 0; i < locations.length(); i++) {
                        JSONObject c = locations.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_LID);
                        String locationName = c.getString(TAG_LNAME);

                        // creating new HashMap
                        HashMap<String, String> locationsListMap = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        locationsListMap.put(TAG_LID, id);
                        locationsListMap.put(TAG_LNAME, locationName);

                        // adding HashList to ArrayList
                        locationList.add(locationsListMap);

                    }
                }                   
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return null;            
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into map marker
                 * */

               //TODO setMapMarkers

                townID = new String[locationList.size()];
                townName = new String[locationList.size()];
                for(int l = 0; l <= (locationList.size() - 1); l++)
                {
                    Log.d("Hello", "location loop");
                    Log.d("Hello", locationList.get(l).toString().split("Location_ID=")[1].split(",")[0]);                      
                    townID[l] = locationList.get(l).toString().split("Location_ID=")[1].split(",")[0];

                    Log.d("Hello", locationList.get(l).toString().split("Location_Name=")[1].split(",")[0].replace("}",""));
                    townName[l] = locationList.get(l).toString().split("Location_Name=")[1].split(",")[0].replace("}","");
                }

                for (int i = 0; i < townID.length; i++) 
                {
                    ts = new TownSelector(townID[i], townName[i]);
                    // Binds all strings into an array
                    arraylist.add(ts);                      
                }

                // Pass results to ListViewAdapter Class
                townAdapter = new ListViewAdapter(this, arraylist);  <--Create new instant of adapter here
                // Binds the Adapter to the ListView
                townList.setAdapter(townAdapter); //This is the line that causes a crash

            }
        }); 
    }    
}
townts;
列表视图城市列表;
ListViewAdapter-townAdapter;价值
位置标签放置(标签盖,id);
locationsListMap.put(标签名称、位置名称);
//将哈希列表添加到ArrayList
locationList.add(locationsListMap);
}
}                   
}捕获(JSONException e){
e、 printStackTrace();
}
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
//获取所有产品后关闭对话框
pDialog.disclose();
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
/**
*将解析的JSON数据更新为映射标记
* */
//TODO设置映射标记
townID=新字符串[locationList.size()];
townName=新字符串[locationList.size()];

对于(int l=0;l您的asyncTask
townAdapter=new ListViewAdapter中的此部分)(此,arraylist);
不正确。您的编辑器应该显示错误。您需要传递
YourActivityName。此
只是将
LoadAllInfo
作为上下文发送,这是错误的异步任务中的此部分
townAdapter=new ListViewAdapter(此,arraylist);
不正确。您的编辑器应该显示错误。您需要传递
YourActivityName。这
只是将
LoadAllInfo
作为上下文发送,这是错误的

您如何初始化适配器请在mainActivity@NavinRaj我在创建适配器变量的代码中编写了Pandey,该变量名为
townAdapter
以及在何处初始化和使用我的意思是,您如何将活动或片段中的上下文传递到此适配器我相信我可能遗漏了这一点?抱歉,我对listadapter的内容不太熟悉。您能否指导我如何初始化此内容?我的主要活动是正常活动,而不是frgament。我是否在som中导入它e方式?您如何初始化适配器请在显示代码mainActivity@NavinRajPandey我在代码中写过,我在代码中创建了名为
townAdapter
的适配器变量,然后在其中初始化并使用它。我的意思是,您如何将活动或片段中的上下文传递给此适配器?我相信我可能遗漏了它?Apo逻辑我不熟悉listadapter。你能指导我如何初始化它吗?我的主要活动是正常活动而不是frgament。我是否以某种方式导入它?
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<TownSelector> locationlist = null;
private ArrayList<TownSelector> arraylist;

public ListViewAdapter(Runnable runnable, List<TownSelector> locationlist) {
    mContext = (Context) runnable;
    this.locationlist = locationlist;
    inflater = LayoutInflater.from(mContext);
    this.arraylist = new ArrayList<TownSelector>();
    this.arraylist.addAll(locationlist);
}
public ListViewAdapter(Context context,Runnable runnable, List<TownSelector> locationlist) {
    mContext = context;
    this.runnable =  runnable;
    this.locationlist = locationlist;
    inflater = LayoutInflater.from(mContext);
    this.arraylist = new ArrayList<TownSelector>();
    this.arraylist.addAll(locationlist);
}
    09-22 03:58:03.467: E/AndroidRuntime(15586): FATAL EXCEPTION: main
    09-22 03:58:03.467: E/AndroidRuntime(15586): Process: biz.nickbullcomputing.bevnav, PID: 15586
    09-22 03:58:03.467: E/AndroidRuntime(15586): java.lang.ClassCastException: biz.nickbullcomputing.bevnav.MainActivity$LoadAllInfo$1 cannot be cast to android.content.Context
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at biz.nickbullcomputing.bevnav.ListViewAdapter.<init>(ListViewAdapter.java:25)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at biz.nickbullcomputing.bevnav.MainActivity$LoadAllInfo$1.run(MainActivity.java:409)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at android.app.Activity.runOnUiThread(Activity.java:4794)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at biz.nickbullcomputing.bevnav.MainActivity$LoadAllInfo.onPostExecute(MainActivity.java:376)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at biz.nickbullcomputing.bevnav.MainActivity$LoadAllInfo.onPostExecute(MainActivity.java:1)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at android.os.AsyncTask.finish(AsyncTask.java:632)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at android.os.Handler.dispatchMessage(Handler.java:102)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at android.os.Looper.loop(Looper.java:157)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at android.app.ActivityThread.main(ActivityThread.java:5867)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at java.lang.reflect.Method.invokeNative(Native Method)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at java.lang.reflect.Method.invoke(Method.java:515)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
    09-22 03:58:03.467: E/AndroidRuntime(15586):    at dalvik.system.NativeStart.main(Native Method)