2012-09-24 58 views
0

这是我第一次使用ListView,并尝试了很多方法使其可行。首先,我在xml文件下使用了一个字符串数组来指定“android:entries”,但是我无法激活onClickListener。另一种方法与我所用的方法相似,其中一些方法是我使用“扩展Acitvity”的方式。预先感谢帮助我。ListView无法显示

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

  • 致命异常:主要
  • 了java.lang.RuntimeException:无法启动活动ComponentInfo(EDU .. ...)
  • 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
  • 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
  • 在android.app.ActivityThread。访问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(本机方法)
  • 在java.lang.reflect.Method中.invoke(Method java:511)
  • at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
  • 在com.android.internal.os.ZygoteInit.main(ZygotaInit.java:551)
  • 在dalvik.system.NativeStart.main(本机方法)
  • 造成的:java.lang.UnsupportedOperationException:addView(查看,的LayoutParams)...
  • 在android.widget.AdapterView.addView(AdapterView.java:471)
  • 在android.view.LayoutInflater.rInflate(LayoutInflater.java:743)
  • 在android.view。 LayoutInflater.rInflate(LayoutInflater.java:742)
  • at 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)
  • at android.app.Activity.performCreate(Activity.java:4538)
  • at android.app.Instrumentation.callActivityOnCreate(Instr umentation。Java的:1071)
  • 在android.app.ActivityThread.performLaunchActivity(ActivityTHread.java:2158)
  • ... 11个多行(我不能表现出更多的输入输出)

菜单。 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" /> 
+1

请添加完整的logcat错误。 –

+0

增加了更多的错误行谢谢 – user1661881

+0

@ user1661881你是否从其他活动调用Menu(ListActivity)..就像使用按钮单击或某件事情? –

回答

0

是啊,这就是问题所在。

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

应该

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

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

更新:我不能相信我没有看到它的前面:P

你有TextViewListview内。这不可能。如果您打算使用“空视图”,请将它们都放在LinearLayout/FrameLayout中。但是Listview不能包含TextView,它们应该彼此相邻。然后,请确保您设置的“ListView控件”中的“空视图”属性设置TextView的在这种情况下@android:id/empty的ID(请纠正这个问题,以及)

+0

谢谢,我试着改变它,但没有区别。我的参考文献是从android应用程序开发的傻瓜pdf第215页,他们在这里写下'android:id =“@ + id/android:list”' – user1661881

+0

@ user1661881,我可以向你保证这本书是错误的。这里是我的[源](http://developer.android.com/reference/android/app/ListActivity.html)也请发布完整的logcat。我知道你增加了更多的线条,但是我们需要更多,红线中应该有更多的线条,请张贴。 – st0le

+0

是的,我已经改变它根据你的,我只是交叉引用,谢谢=) 我编辑它,并张贴所有的红线,我不能访问最后11行,对不起 – user1661881

0

尝试改变这样

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(); 
    } 
} 
代码

告诉我,如果它的工作...

+0

谢谢,我试过了,但可悲的是活动无法再开始。只是为了清楚它 - 'ListView lv = getListView();'我输入的。 =) – user1661881

+0

雅,对不起,我添加了两个等号。还有一件事..你是否改变了xml中的listview名称到其他答案中的内容。我假设你已经做到了。并且在设置适配器中还有一个变化...您看到了? – Renjith

+0

是的,我做到了。但它仍然无法运行。 – user1661881