0

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)
atom2ueki
  • 837
  • 4
  • 15
  • 32

2 Answers2

0

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();

Community
  • 1
  • 1
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • so can i said what you mean is simple adapter is not suitable for my hashmap array? so i would better use a custom adapter? – atom2ueki Aug 24 '13 at 17:09
0

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 });
user
  • 86,916
  • 18
  • 197
  • 190
  • actually, i don't want use a new layout like simple_list_item_1 to open a new page, what i want is show them just on main_layout which id is listView_wifi, as you said adapter can't bind data to a ListView from the row directly, so what is the solution? dose 'ArrayAdapter' can solve this problem? – atom2ueki Aug 24 '13 at 18:17
  • i tried your method...it solved my problem, but i don't understand your comment for " passing as the ListView's row the layout that you used for the activity content(the one containing the wifi ListView)",my R.id.listView_wifi is under R.layout.activity_main, is there any diff from android.R.layout.simple_list_item_1 and android.R.id.text1? can you explain it more detail...sry abt that – atom2ueki Aug 24 '13 at 18:25
  • @atom2ueki *what i want is show them just on main_layout which id is listView_wifi* - and show them how? What are you using the `R.id.listView_wifi` `ListView` for then? – user Aug 24 '13 at 18:29
  • then how can i use adapter update view on R.id.listView_wifi...not on android.R.id.text1 – atom2ueki Aug 24 '13 at 18:38
  • @atom2ueki You don't seem to know how a `ListView` works. A `ListView` has rows as its content and that content is made from a row layout(used in each row) that is passed to the adapter. You update the ListView by inserting data in the list/array/map that is backing the adapter and calling notifyDataSetChanged(). Use the code I've written in my answer. – user Aug 24 '13 at 18:42