【问题标题】:ListView cannot be displayedListView 无法显示
【发布时间】:2012-09-24 19:04:13
【问题描述】:

这是我第一次使用 ListView 并且已经尝试了许多方法来使其可行。首先,我在 xml 文件中使用字符串数组指定“android:entries”,它显示但我无法激活 onClickListener。另一种方法与我的方法相似,有些是我使用“扩展 Acitvity”的地方。提前感谢您帮助我。

基本上,我想为列表中的每个项目使用自定义的 textView 行,并为每个可点击的行显示一个 toast。我的问题是,当我尝试显示此活动时,应用程序会崩溃。我不确定问题出在哪里,因为没有任何语法错误。错误消息是(对不起,我会放一张图片,但我不能 - 新用户):

  • 致命异常:主要
  • java.lang.RuntimeException: 无法启动活动 ComponentInfo(edu.....)
  • 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
  • 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
  • 在 android.app.ActivityThread.access$600(ActivityTHread.java:139)
  • 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
  • 在 android.os.Handler.dispatchMessage(Handler.java:99)
  • 在 android.os.Looper.loop(Loooper.java:154)
  • 在 android.app.ActivityThread.main(ActivityThread.java:4974)
  • 在 java.lang.reflect.Method.invokeNative(Native Method)
  • 在 java.lang.reflect.Method.invoke(方法 java:511)
  • 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  • 在 com.android.internal.os.ZygoteInit.main(ZygotaInit.java:551)
  • 在 dalvik.system.NativeStart.main(Native Method)
  • 原因:java.lang.UnsupportedOperationException: addView(View, LayoutParams)...
  • 在 android.widget.AdapterView.addView(AdapterView.java:471)
  • 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:743)
  • 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
  • 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:489)
  • 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:396)
  • 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:352)
  • 在 com.android.internal.policy.impl.PhoneWindows.setContentView(PhoneWindow.java ...)
  • 在 android.app.Activity.setContentView(Activity.java:1892)
  • 在 edu.nyp.wonderful.Menu.onCreate(Menu.java:14)
  • 在 android.app.Activity.performCreate(Activity.java:4538)
  • 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
  • 在 android.app.ActivityThread.performLaunchActivity(ActivityTHread.java:2158)
  • ...还有 11 行(我无法输入更多内容)

Menu.java v

package edu.nyp.wonderful;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class Menu extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        Log.d("Menu:", "after set content view");

        String[] items = { "Set Pins", "View Pins", "List of Pins", "Email Summary", "Edit Session's Info" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.menu_row, R.id.menu_name, items);
        setListAdapter(adapter);
        Log.d("Menu:", "after setting adapter");
    }

    protected void onListItemClick(ListView l, View v, int position, long id){
        super.onListItemClick(l, v, position, id);
        Toast.makeText(getApplicationContext(), "Clicked position: " + position, Toast.LENGTH_SHORT).show();
    }
}

menu.xml v

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="10dp" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="54dp"
            android:layout_height="60dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/logo" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Menu"
            android:textColor="#DC1700"
            android:textStyle="bold"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="30dp"
            android:textSize="25sp"
            android:typeface="serif" />
    </LinearLayout>

    <View android:id="@+id/separator1" 
         android:background="#000000" 
         android:layout_width = "fill_parent"
         android:layout_height="1dp"
         android:layout_marginBottom="10dp" />


    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#000000"
        android:dividerHeight="1dp" >

        <TextView android:id="@+id/android:empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="nothing" />

    </ListView>
</LinearLayout>

*menu_row.xml v*

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="23dp"
    android:shadowRadius="5"
    android:shadowColor="#000000"
    android:shadowDy="3"
    android:shadowDx="3"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp" />

【问题讨论】:

  • 请添加完整的 logcat 错误。
  • 感谢添加更多错误行
  • @user1661881 你是从其他活动中调用菜单(ListActivity)吗?比如使用按钮单击或其他什么?
  • 是的,我认为这也是以前的活动,所以我将它链接到另一个活动,静态显示文本视图并且它有效。 =)
  • @user1661881,您还没有发布相关的 logcat 详细信息。在崩溃的堆栈跟踪附近,您应该看到包含您的包名称的行。请张贴。

标签: android listview


【解决方案1】:

是的,这就是问题所在。

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

应该是

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

修复并检查,如果问题仍然存在,请报告。

更新:我不敢相信我之前没有看到它:P

Listview 中有一个 TextView。这是不可能的。如果您打算使用“空视图”,请将它们都放在 LinearLayout/FrameLayout 中。但是 Listview 不能包含 TextView,它们应该彼此相邻。然后,确保在本例中将“ListView”中的“Empty View”属性设置为 Textview 的 id @android:id/empty(请同时更正)

【讨论】:

  • 谢谢,我尝试更改它,但没有任何区别。我的参考来自 android application development for dummies pdf 第 215 页,他们在其中写下了 'android:id="@+id/android:list"'
  • @user1661881,我可以向你保证,这本书完全错了。这是我的source 也请发布完整的logcat。我知道你添加了更多的行,但我们需要更多,应该有更多的红色行,请发布。
  • 是的,我已经根据你的修改了,我只是交叉引用,谢谢 =) 我已经编辑并发布了所有红线,我无法访问最后 11 行,抱歉
  • @user1661881,没问题。很高兴我能帮上忙。 :)
【解决方案2】:

尝试像这样更改您的代码

public class Menu extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        Log.d("Menu:", "after set content view");

        String[] items = { "Set Pins", "View Pins", "List of Pins", "Email Summary", "Edit Session's Info" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.menu_row, R.id.menu_name, items);

        ListView lv  = getListView();
        lv.setAdapter(adapter);
        Log.d("Menu:", "after setting adapter");
    }

    protected void onListItemClick(ListView l, View v, int position, long id){
        super.onListItemClick(l, v, position, id);
        Toast.makeText(getApplicationContext(), "Clicked position: " + position, Toast.LENGTH_SHORT).show();
    }
}

告诉我它是否有效......

【讨论】:

  • 谢谢,我试过了,但遗憾的是活动无法再次开始。只是要清楚它 - 'ListView lv = getListView();'我进入的。 =)
  • 是的,对不起,我添加了两个等号..还有一件事..您是否将 xml 中的列表视图名称更改为另一个答案中所说的内容。我假设你已经这样做了。并且设置适配器也发生了变化......你看到了吗??
  • 是的,我都做到了。但它仍然无法运行。
猜你喜欢
  • 1970-01-01
  • 2021-01-21
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2021-01-30
  • 2013-06-22
相关资源
最近更新 更多