Question

Getting a IllegalStateException SimpleAdapter.java:195 and ...when list view showing something from simple adapter you can get my source code here https://github.com/Wifin/WifinApp

Code snippet 1

public class MainActivity extends Activity 
{

    TextView txtlocation;
    TextView txtstatus;
    TextView txtinstant;
    Button btn_loc;
    Button btn_ser;
    Button bnt_aploc;
    ListView wifi_lv;
    myLocation mlocal;
    myWifi mwifi;
    WifiReceiver mreceiver;
    SimpleAdapter adapter;
    ArrayList<HashMap<String, String>> arraylist;
    String ITEM_KEY;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        arraylist = new ArrayList<HashMap<String, String>>();
        ITEM_KEY = "key";

        adapter = new SimpleAdapter(
                this, 
                arraylist, 
                R.layout.activity_main,
                new String[] { ITEM_KEY },
                new int[] { R.id.listView_wifi });

        txtlocation= (TextView) findViewById(R.id.locView);
        btn_loc  = (Button) this.findViewById(R.id.button_location);
        btn_ser = (Button) this.findViewById(R.id.button_services);
        bnt_aploc = (Button) this.findViewById(R.id.button_aploc);
        wifi_lv =(ListView)findViewById(R.id.listView_wifi);
        mlocal = new myLocation();
        mwifi =new myWifi();
        mreceiver =new WifiReceiver();

        mlocal.checkGoogleplay(this);       
        wifi_lv.setAdapter(adapter);

        //mwifi.scanWifi(this.getApplicationContext());

        mwifi.wifi= (WifiManager) getSystemService(Context.WIFI_SERVICE);


            btn_loc.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                    // TODO Auto-generated method stub
                arraylist.clear();
                    mlocal.getlocation();
                    mwifi.scanWifi();
                    mreceiver.getwifilist(MainActivity.this);   
                adapter.notifyDataSetChanged();
            }
      });

Code snippet 2

package com.example.Wifin;

import java.util.HashMap;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;

public class WifiReceiver extends BroadcastReceiver
{

    List<ScanResult> wifilist;
    int size=0;
    double ap_rssi;
    double ap_feq;
    double distance;
    String ssid;


    @Override
    public void onReceive(Context c, Intent i) {
        // TODO Auto-generated method stub  
        System.out.println("111");
        if (i.getAction().equals(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifilist = ((WifiManager) c.getSystemService(Context.WIFI_SERVICE)).getScanResults();

    }

    public void getwifilist (MainActivity ma)
    {
        if (wifilist!=null)
        {   

        try {
            size =wifilist.size()-1;
            while (size >= 0) 
            {                   
                ap_rssi = Double.valueOf(wifilist.get(size).level);
                ap_feq = Double.valueOf(wifilist.get(size).frequency);
                ssid = String.valueOf(wifilist.get(size).SSID);

                distance=Math.pow(10, (Math.abs(ap_rssi)-20*Math.log10(ap_feq)-32.44)/20)*1000;

                System.out.println("distance:"+(Math.round(distance*100))/100.0);

                HashMap<String, String> item = new HashMap<String, String>();
                item.put(ma.ITEM_KEY, ssid +""+String.valueOf((Math.round(distance*100))/100.0));
                ma.arraylist.add(item);
                size--; 
              }
            }
            catch (Exception e) {

            }
        }
    }

}

catlog

08-25 01:25:55.427: E/AndroidRuntime(5009): FATAL EXCEPTION: main
08-25 01:25:55.427: E/AndroidRuntime(5009): java.lang.IllegalStateException: android.widget.ListView is not a  view that can be bounds by this SimpleAdapter
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:195)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.ListView.onMeasure(ListView.java:1159)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.View.measure(View.java:15848)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.View.measure(View.java:15848)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.View.measure(View.java:15848)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.View.measure(View.java:15848)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.View.measure(View.java:15848)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.Choreographer.doFrame(Choreographer.java:532)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.os.Handler.handleCallback(Handler.java:730)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.os.Looper.loop(Looper.java:137)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at java.lang.reflect.Method.invokeNative(Native Method)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at java.lang.reflect.Method.invoke(Method.java:525)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-25 01:25:55.427: E/AndroidRuntime(5009):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

You're passing as the ListView's row the layout that you used for the activity content(the one containing the wifi ListView) and also passing in the to array the id(R.id.listView_wifi) of the ListView. This will fail because the adapter can't bind data to a ListView from the row directly and what you're trying to do is incorrect anyway.

You'd want to make a different layout to use in the SimpleAdapter to which to bind the data. One simple solution, as you're planing to bind a String to the row, is to use this:

adapter = new SimpleAdapter(
                this, 
                arraylist, 
                android.R.layout.simple_list_item_1,
                new String[] { ITEM_KEY },
                new int[] { android.R.id.text1 });

OTHER TIPS

From the SimpleAdapter documentation

A view that implements Checkable (e.g. CheckBox). The expected bind value is a boolean.

TextView. The expected bind value is a string and setViewText(TextView, String) is invoked.

ImageView. The expected bind value is a resource id or a string and setViewImage(ImageView, int) or setViewImage(ImageView, String) is invoked.

It would appear that passing an Object as the mapped object is not compatible with SimpleAdapter. Change the object type, or create a custom adapter (I recommend the later)

And also try this links

Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"

list view java.lang.IllegalStateException even after calling notifyDataSetCanged();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top