0

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
4
  • r u loading the correct layout in the activity? Commented Mar 16, 2012 at 2:30
  • Can you show the code where you get your inflater? Commented Mar 16, 2012 at 2:34
  • So you are getting an error when you inflating the listview? Commented Mar 16, 2012 at 2:41
  • my errors a nullpointer on listview and i added the inflater code. also incase this makes any difference im doing this inside of my single class and i have already inflated the xml singleplayer in my activity once. Commented Mar 16, 2012 at 2:43

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

6 Comments

thank you now im getting a different error though ill post in my original post
For your 2nd issue, as you can see from the stacktrace that issue probably is caused by that custom view, you may need to post some of your code in that class in order to tackle the issue ;)
its over 2000 lines the thing is though that it inflates without error when i do it in my activity
Please have a look into the codes around line 1922 in single.java. From the stacktrace: "Can't create handler inside thread that has not called Looper.prepare()". Error Probably caused by this, hopes this help you
what is a invocation targetexception?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.