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)
4

2 回答 2

0

来自 SimpleAdapter 文档

一个实现 Checkable 的视图(例如 CheckBox)。预期的绑定值是一个布尔值。

文本视图。预期的绑定值是一个字符串,并且调用了 setViewText(TextView, String)。

图像视图。预期的绑定值是资源 id 或字符串,并且调用了 setViewImage(ImageView, int) 或 setViewImage(ImageView, String)。

将对象作为映射对象传递似乎与 SimpleAdapter 不兼容。更改对象类型,或创建自定义适配器(我推荐后者)

也试试这个链接

Android,ListView IllegalStateException:“适配器的内容已更改但ListView没有收到通知”

列表视图 java.lang.IllegalStateException 即使在调用 notifyDataSetCanged();

于 2013-08-24T16:57:57.427 回答
0

您将ListView用于活动内容的布局(包含 wifi 的布局)作为 ' 行传递ListView,并在to数组中传递. 这将失败,因为适配器无法直接将数据绑定到行中的 a 并且您尝试做的事情无论如何都是不正确的。R.id.listView_wifiListViewListView

您想要制作不同的布局以SimpleAdapter用于绑定数据。当您计划将 String 绑定到行时,一个简单的解决方案是使用以下方法:

adapter = new SimpleAdapter(
                this, 
                arraylist, 
                android.R.layout.simple_list_item_1,
                new String[] { ITEM_KEY },
                new int[] { android.R.id.text1 });
于 2013-08-24T17:35:58.367 回答