MikeC
MikeC

Reputation: 735

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. Im trying to work with a listview object inflated from my xml im not sure how to do this though.

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

heres my 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:

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

Upvotes: 0

Views: 365

Answers (1)

hungr
hungr

Reputation: 2096

LayoutInflater is used to inflate a layout , so you need to do the following to get the correct result:

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);

Upvotes: 2

Related Questions