开发者

android newbie question null pointer on ArrayAdapter.setAdapter

I have been slowing learning and building my first android app. I'm VERY new to java but have already done a couple of projects in C#, VB.NET (back in the day), Objective-C (have 6 apps in the store) and Fortran (waaaaaaaaaaaaaaaaaaaay back in the day ;)

So I just received from overseas a htc legend (I'm not in the US), which i bought in order to have a decent mid-level device for development (it's running non-rooted adnroid 2.1)

The application I have been developing is target level 4 (android 1.6). It uses a 5 Mb sqlite3 database with a .mp3 extension to avoid compression within the apk and proper copying from assets to system folder.

It all works fine on the emulator, and on the device I see that the file size of开发者_开发问答 the app after copying the database matches exactly what I see on the emulator.

now, on my main activity with a list view and a spinner, I bind some data through two array adapters. when running on the device all does smoothly. but when trying to run on the device this part of the code:

public class mainAct extends Activity implements OnItemSelectedListener, TextWatcher, OnItemClickListener
{   
        /** members */
        //private EditText searchtext;
        private ListView designations;
        private ArrayAdapter<String> adapterShapes;
        private ArrayAdapter<String> adapterTypes;
        private Spinner types;
      .  
      .  
      .    
    public void onCreate(Bundle savedInstanceState)   
    {  
      .  
      .  
      .    
        // DESIGNATIONS  
        // 
        adapterShapes = new ArrayAdapter<String>(this,R.layout.list_item,shapes);       // custom TextView Adapter  
        designations=(ListView)findViewById(R.id.designations);
        Log.e("MAIN.ACCT", "ok to 172");
        designations.setAdapter(adapterShapes);
        Log.e("MAIN.ACCT", "ok to 174");
        designations.setOnItemClickListener(this);

        // TYPES
        //
        adapterTypes=new ArrayAdapter<String>(this,R.layout.spinner_item,DT.get().typesInLibrary);
        types=(Spinner)findViewById(R.id.types);
        types.setAdapter(adapterTypes);
        types.setOnItemSelectedListener(this);
      .  
      .  
      .  
    }
}

Both designations.setAdapter(adapterShapes);

& types.setAdapter(adapterTypes);

give me a Null Pointer exception.

I'm using eclipse under mac, the LogCat window throws:

06-25 18:41:37.842: ERROR/AndroidRuntime(9523): Uncaught handler: thread main exiting due to uncaught exception

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.davidhomes.steel/com.davidhomes.steel.mainAct}: java.lang.NullPointerException

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread.access$2200(ActivityThread.java:126)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.os.Handler.dispatchMessage(Handler.java:99)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.os.Looper.loop(Looper.java:123)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread.main(ActivityThread.java:4595)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at java.lang.reflect.Method.invokeNative(Native Method)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at java.lang.reflect.Method.invoke(Method.java:521)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at dalvik.system.NativeStart.main(Native Method)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): Caused by: java.lang.NullPointerException

-------------------------------------------------------------- 06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at com.davidhomes.steel.mainAct.onCreate(mainAct.java:183)

--------------------------------------------------------------

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): ... 11 more

06-25 18:46:38.252: ERROR/ActivityManager(99): fail to set top app changed!

Line 183 is the first setAdapter call (designations.setAdapter(adapterShapes);), when I comment it out the second setAdapter is the one breaking code

I'm a little lost here, the adapters show the proper number of items on the log window when running from the simulator and the device.

I admit to being a noob to both java and android, so any help is highly appreciated.

regards

david


Well, as you've probably figured out yourself, some variable is null. Unfortunately, there is no obvious source of the NullPointerException in your code.

Therefore, you should first try to identify what variable is null, and hence causing the exception.

For example, findViewById returns null if it cannot find the view, so you may want to double check your ListView and Spinner are being initialised properly.

Of course, the problem may be with your ArrayAdapters, so you should also check them, but from your question it sounds as if you've already done that.

Once you (and we) know exactly where the NullPointerException is occurring, it will be easier to give more specific advice


Oops! it was my fault all along, somehow I had TWO layouts (one for different resolutions), the one being used on my device did not have the proper ListView and Spinner ID. That alternative layout was collapse into a folder and I just forgot about it (I had paused development of the app for about 2 month until I actually got a device to test on).

the one working fine was installing on the simulator but not on the device and vice-versa, weird as the good one matches the resolution of my device (I'm pretty sure I'm also missing something there but that's not important right now)

Still, thanks Chris for pointing me to looking at the Null return value on findViewById. Being new to android I was lost as to were to begin

Best regards david


I know this is a long closed subject. However I just came across the same problem and this post did not answer it.

What i did, was very silly. I forgot to add a reference to the xml. ie. setContentView(R.layout.settings);

it actually looks like the original post might have also forgotten this so i thought I would just post it. Cheers.


I had the same problem. I put Log.d messages in printing out every object. It turned out that my ListView object was "null". The reason for this was I had the wrong XML activity listed in my SetContentView. I think the easiest way to debug this problem is keep putting in Log.d messages until you find the object/variable that is "null".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜