【问题标题】:webview and listview in a same activitywebview 和 listview 在同一个活动中
【发布时间】:2012-09-13 11:23:00
【问题描述】:

我正在尝试在同一个活动中创建 webview 和 listview。我有一个 ID 为 list_xc 的列表视图,但在我的 content must have a ListView whose id attribute is 'android.R.id.list 的 logcat 显示中。 这是我的布局文件:

<com.markupartist.android.widget.ActionBar
    android:id="@+id/actionbar"
    style="@style/ActionBar" />

<TextView
    android:id="@+id/tv_xc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="3dp"
    android:text="Cross Country (XC)"
    android:textColor="#372c24"
    android:textSize="20dp"
    android:textStyle="bold" />

<WebView
    android:id="@+id/webview_xc"
    android:layout_width="match_parent"
    android:layout_height="166dp" />

<ListView
    android:id="@+id/list_xc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

这是我的行布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<TextView
    android:id="@+id/tv_test"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#424545" >
</TextView>

这是我的活动:

public class Information_XC_Activity extends ListActivity {
WebView mWebView;
TextView tv;
private Adapter_DetailPolXC adapterDetailPolXC;

private ListView mainListView;

// private ArrayAdapter<String> listAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info_xc);

    final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
    actionBar.setTitle(getString(R.string.app_name));

    int index = getIntent().getIntExtra("text", 0);

    mWebView = (WebView) findViewById(R.id.webview_xc);

    String temp = "<html><body>" + "<p align=\"justify\">"
            + getString(R.string.xc_info + index) + "</p> "
            + "</body></html>";
    mWebView.loadData(temp, "text/html", "utf-8");

    ArrayList<Entity_Detail_Product> arraylist = new ArrayList<Entity_Detail_Product>();

    adapterDetailPolXC = new Adapter_DetailPolXC(this, R.layout.info_row,
            arraylist);

    mainListView = (ListView) findViewById(R.id.list_xc);
    mainListView.setAdapter(adapterDetailPolXC);

    new info_xc_dao(response).view_pol_xc();
}

pol_xc_Result response = new pol_xc_Result() {
    @Override
    public void gotResult(Object obj, String message, int action) {
        // TODO Auto-generated method stub
        // actionBar.setProgressBarVisibility(View.INVISIBLE);

        @SuppressWarnings("unchecked")
        ArrayList<Entity_Detail_Product> arrayList = (ArrayList<Entity_Detail_Product>) obj;
        AlertDialog.Builder builder = new AlertDialog.Builder(
                Information_XC_Activity.this);

        if (arrayList == null) {
            builder.setIcon(R.drawable.alert_warning);
            builder.setTitle("Error");
            builder.setMessage("Data Not Found !");
            builder.setNegativeButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

        } else {
            for (Entity_Detail_Product entity_Detail_Product : arrayList) {
                adapterDetailPolXC.add(entity_Detail_Product);
            }
            adapterDetailPolXC.notifyDataSetChanged();
        }

    }

};

private class Adapter_DetailPolXC extends
        ArrayAdapter<Entity_Detail_Product> {
    private ArrayList<Entity_Detail_Product> items;

    public Adapter_DetailPolXC(Context context, int textViewResourceId,
            ArrayList<Entity_Detail_Product> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;
        if (v == null) {
            LayoutInflater layoutInflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = (View) layoutInflater.inflate(R.layout.info_xc, null);
        }
        Entity_Detail_Product adapter_Detail_Product = items.get(position);
        if (adapter_Detail_Product != null) {
            TextView textView = (TextView) v.findViewById(R.id.tv_test);
            if (textView != null)
                textView.setText(adapter_Detail_Product.getModel_name());
        }
        return v;
    }

}

这是日志:

09-21 03:22:49.029:E/AndroidRuntime(22732):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.wilis.hellotabwidget/com.wilis.hellotabwidget.Information_XC_Activity}:java.lang。 RuntimeException:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.os.Handler.dispatchMessage(Handler.java:99) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.os.Looper.loop(Looper.java:130) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.app.ActivityThread.main(ActivityThread.java:3687) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 java.lang.reflect.Method.invoke(Method.java:507) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 09-21 03:22:49.029: E/AndroidRuntime(22732): at dalvik.system.NativeStart.main(Native Method) 09-21 03:22:49.029: E/AndroidRuntime(22732): Caused by: java.lang.RuntimeException: 你的内容必须有一个 ID 属性为 'android.R.id.list' 的 ListView 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.app.ListActivity.onContentChanged(ListActivity.java:243) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:212) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.app.Activity.setContentView(Activity.java:1657) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 com.wilis.hellotabwidget.Information_XC_Activity.onCreate(Information_XC_Activity.java:35) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-21 03:22:49.029: E/AndroidRuntime(22732): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

非常感谢,我真的需要帮助。

【问题讨论】:

    标签: android listview webview


    【解决方案1】:

    因为您的Information_XC_Activity 扩展了ListActivity,所以它必须有一个具有错误消息中指定的 id 的 ListView。改变这一行

    android:id="@+id/list_xc"
    

    到这里:

    android:id="@android:id/list"
    

    【讨论】:

      【解决方案2】:

      如果您只想显示一个 ListView ListActivity 默认包含一个 ListView,则 ListActivity 不需要您通过 setContentView() 方法为其分配布局。

      如果您需要在 ListActivity 中包含比 ListView 更多的 View,您仍然可以为您的 Activity 分配布局。在这种情况下,您的 布局必须包含一个 ListView,其中 android:id 属性设置为 @android:id/list。所以修改代码到这个。

      <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
      </ListView> 
      

      如果您在布局中使用带有@android:id/empty ID 的 View,如果 ListView 为空,ListActivity 将自动显示此 View,否则将其隐藏。

      【讨论】:

        【解决方案3】:

        如果你扩展 ListActivity,你的 android:id="@+id/list_xc" 必须是 android:id="@android:id/list"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-16
          • 2016-08-30
          相关资源
          最近更新 更多