0

I have a ListView that is being populated dynamically and I want to use ViewFlipper to switch to a new screen with a button when an item in the first list is clicked.

I have already added an OnItemClickListener so that when I click the item in the list it will display a message.

The problem is that when I add the showNext() method in the onItemClick() method the application just crashes with NullPointerException.

I'm just looking for the flipper to make a transition from the screen with the list to the screen with the button.

Here is a snippet of the code I'm using.

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    deviceListView = (ListView) findViewById(R.id.deviceList);
    listAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
    setListAdapter(listAdapter);
    deviceListView = getListView();


    flippy = (ViewFlipper) findViewById(R.id.flipper);
    //flippy.setOnClickListener((OnClickListener) this);

    //getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()

    deviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    {   
        public void onItemClick(AdapterView <?> parent, View v, int position, long id)
        {
            System.out.println("Before Flipper");
            flippy.showNext();
            System.out.println("After Flipper");
        }
    });


    getApplicationContext().bindService(
            new Intent(this, BrowserUpnpService.class),
            serviceConnection,
            Context.BIND_AUTO_CREATE
    );
}

Here is the XML I am using

    <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" >
            <ViewFlipper
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/flipper"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <ListView
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/deviceList"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
                </ListView>
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" >
                </Button>
            </ViewFlipper>
        </FrameLayout>

Any help would greatly be appreciated.

Thanks :)

Update:

Here is the error log I am getting:

07-01 12:31:35.548: E/AndroidRuntime(482): FATAL EXCEPTION: main  
07-01 12:31:35.548: E/AndroidRuntime(482): java.lang.NullPointerException  
07-01 12:31:35.548: E/AndroidRuntime(482):  at com.app.browser.BrowseActivity$3.onItemClick(BrowseActivity.java:156)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at android.widget.ListView.performItemClick(ListView.java:3382)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at android.os.Handler.handleCallback(Handler.java:587)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at android.os.Handler.dispatchMessage(Handler.java:92)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at android.os.Looper.loop(Looper.java:123)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at android.app.ActivityThread.main(ActivityThread.java:4627)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at java.lang.reflect.Method.invokeNative(Native Method)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at java.lang.reflect.Method.invoke(Method.java:521)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)  
07-01 12:31:35.548: E/AndroidRuntime(482):  at dalvik.system.NativeStart.main(Native Method)

Here is the code from my main activity class

   setContentView(R.layout.main);

    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, BrowseActivity.class);
    spec = tabHost.newTabSpec("device")
            .setIndicator("Devices", getResources().getDrawable(R.drawable.ic_launcher))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, MediaPlayer.class);
    spec = tabHost.newTabSpec("media")
            .setIndicator("Media Player", getResources().getDrawable(R.drawable.ic_launcher))
            .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
mcbain
  • 1
  • 2

1 Answers1

0

You aren't calling setContentView(R.layout.yourlayoutname); which means that your layout is not being put onto the screen. The result is that findViewById() is going to return null to you. Since you are trying to call null.showNext(); you are getting a nullPointerException on that line.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • I am calling setContentView(R.layout.main); in another class and it works when I remove "flippy.showNext();" – mcbain Jul 01 '12 at 12:30
  • Post the other class code. I still think that this is the error. If you have two seperate Activities the setContentView() call is not going to carry over from one to the other. – FoamyGuy Jul 01 '12 at 15:42
  • What should I be implementing if I do have two Activities? – mcbain Jul 01 '12 at 16:20