android - SimpleAdapter 中的 WebView

标签 android simpleadapter

我用 json 创建 Rss,当我从后台获取 jsonObject 时,在 postExecute 中我用对象结果创建 Map,然后出现错误 LogCat 导致 SimpleAdapter 中的 WebView 出错,为什么没人能帮忙?

LogCat

    12-05 17:55:35.597: E/AndroidRuntime(22543): FATAL EXCEPTION: main
    12-05 17:55:35.597: E/AndroidRuntime(22543): java.lang.IllegalStateException: android.webkit.WebView is not a  view that can be bounds by this SimpleAdapter
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:195)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.AbsListView.obtainView(AbsListView.java:2161)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.ListView.fillDown(ListView.java:674)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.ListView.fillFromTop(ListView.java:735)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.ListView.layoutChildren(ListView.java:1652)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.AbsListView.onLayout(AbsListView.java:1996)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.View.layout(View.java:14003)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.View.layout(View.java:14003)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.View.layout(View.java:14003)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.View.layout(View.java:14003)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.View.layout(View.java:14003)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.Choreographer.doFrame(Choreographer.java:532)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.os.Handler.handleCallback(Handler.java:725)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.os.Handler.dispatchMessage(Handler.java:92)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.os.Looper.loop(Looper.java:137)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at android.app.ActivityThread.main(ActivityThread.java:5238)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at java.lang.reflect.Method.invoke(Method.java:511)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    12-05 17:55:35.597: E/AndroidRuntime(22543):    at dalvik.system.NativeStart.main(Native Method)

源代码

protected void onPostExecute(JSONObject result) {
      super.onPostExecute(result);
      if(result != null){ 

      JSONObject jsn = result;
      try {
        JSONArray jarray = jsn.getJSONArray("item");
        for (int i = 0; i < jarray.length(); i++){
            JSONObject jsno = jarray.getJSONObject(i);
            Log.d("Log", jsno.getString("title"));
             rssMap = new HashMap<String, Object>();
             rssMap.put(TITLE, jsno.getString("title"));
             rssMap.put(CATEGORY, jsno.getString("category"));
             rssMap.put(DATE, jsno.getString("pubDate"));
             rssMap.put(DESCRIPTION, jsno.getString("description"));// This for webView, it's  html code!
             rssView.add(rssMap);
        }
        SimpleAdapter adapter = new SimpleAdapter(
                getApplicationContext(), rssView, R.layout.item, 
                new String[] {TITLE, CATEGORY, DATE, DESCRIPTION}, 
                new int[] {R.id.title, R.id.cat, R.id.date, R.id.desc}
                );
        list.setAdapter(adapter);
      }
      catch (JSONException e) {     
            e.printStackTrace();}
      }
      else{
          /*
           * Тут мне нужен AlertDialog с оповещанием что нету подключения к сети!
           * */
          showDialog(errorShow);      
      }

    }

}

item.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Заголовок"
        android:textColor="#33b5e5"
        android:textSize="23sp" />

    <TextView
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="05.12.2012"
        android:textColor="#969696"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/cat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Категория"
        android:textColor="#969696"
        android:textSize="14sp" />

    <WebView
        android:id="@+id/desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

最佳答案

SimpleAdapter 的 Android 文档指出:

Binding data to views occurs in two phases. First, if a SimpleAdapter.ViewBinder is available, setViewValue(android.view.View, Object, String) is invoked. If the returned value is true, binding has occurred. If the returned value is false, the following views are then tried in order:

  • 实现 Checkable 的 View (例如 CheckBox)。预期的绑定(bind)值是一个 bool 值。
  • TextView 。预期的绑定(bind)值是一个字符串,并且调用了 setViewText(TextView, String)
  • ImageView。预期的绑定(bind)值是资源 ID 或字符串,并调用 setViewImage(ImageView, int)setViewImage(ImageView, String)

If no appropriate binding can be found, an IllegalStateException is thrown.

适配器无法自行将您的 jsno.getString("description")(它是一个 String)转换为 URL。您可以尝试转换字符串,然后将其作为 URL 对象放入 rssMap 中,但是,我建议您实现自己的自定义 Adapter 并覆盖 getView() 方法在绑定(bind)期间设置 WebView 的 URL。

关于android - SimpleAdapter 中的 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13724317/

相关文章:

带有 scala setAdapter InvocationTargetException 的 Android

Android:ListView 单击事件无法使用 SimpleAdapter

java - 当我循环遍历所有行时更新列

java - 循环帮助!向 map API v2 添加多个标记

android - 使用 NDK 在 Android 中进行实时图像处理

android - 当我为警告对话框中的编辑文本执行 getText() 时获取空指针 (Android)

android - 像微调器一样的 iOS

java - 系统服务不可用异常SimpleAdapter Android