简体   繁体   English

我如何创建一个listview对象Android

[英]How do i create a listview object Android

Hi so what im trying to do is when the player loses the game a highscore list comes up taking up half the screen in the form of a listView. 嗨,那么我想要做的就是当玩家输掉游戏时,一个高分榜上出现了以listView形式占据屏幕的一半。 Im trying to work with a listview object inflated from my xml im not sure how to do this though. 我试图使用从我的xml膨胀的listview对象我不知道如何做到这一点。

I have tried: 我努力了:

LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

ListView lv = (ListView) inflater.inflate(R.id.highscores,null);

but it keeps telling me lv is null 但它一直告诉我lv是null

heres my xml: 继承我的xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<com.SpaceShot.single android:id="@+id/single1"
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">

 </com.SpaceShot.single>

<ListView android:id="@+id/highScores"
 android:layout_width="wrap_content"
 android:layout_height="200dp"
 android:padding="15dp"
 android:visibility= "invisible"
 >
</ListView>
</FrameLayout>

EDIT: 编辑:

Now inflating the ListView works but i get this error: 现在膨胀ListView工作,但我收到此错误:

03-15 23:05:01.351: ERROR/AndroidRuntime(2555): Uncaught handler: thread Thread-11 
exiting due to uncaught exception
03-15 23:05:01.371: ERROR/AndroidRuntime(2555): android.view.InflateException: Binary
 XML file line #9: Error inflating class java.lang.reflect.Constructor
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at 
android.view.LayoutInflater.createView(LayoutInflater.java:512)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at 
android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at 
android.view.LayoutInflater.inflate(LayoutInflater.java:407)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at 
android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at
android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at 
com.SpaceShot.single$singleThread.doDraw(single.java:503)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at 
com.SpaceShot.single$singleThread.run(single.java:1754)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555): 
Caused by: 
java.lang.reflect.InvocationTargetException
03-15 23:05:01.371: ERROR/AndroidRuntime(2555): at com.SpaceShot.single.<init>
(single.java:1922)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):  at 
java.lang.reflect.Constructor.constructNative(Native Method)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at     
java.lang.reflect.Constructor.newInstance(Constructor.java:446)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at 
android.view.LayoutInflater.createView(LayoutInflater.java:499)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     ... 7 more
03-15 23:05:01.371: ERROR/AndroidRuntime(2555): 
Caused by: java.lang.RuntimeException:
Can't create handler inside thread that has not called Looper.prepare()
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at android.os.Handler.<init>
(Handler.java:121)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at android.view.SurfaceView$1.
<init>(SurfaceView.java:107)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     at android.view.SurfaceView.<init>
(SurfaceView.java:105)
03-15 23:05:01.371: ERROR/AndroidRuntime(2555):     ... 11 more

LayoutInflater is used to inflate a layout , so you need to do the following to get the correct result: LayoutInflater用于扩展布局,因此您需要执行以下操作以获得正确的结果:

LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = (View)inflater.inflate(R.layout.your_layout,null);

ListView v = (ListView)layout.findViewById(R.id.highscores);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM