0

我有一個ListView是動態填充的,我想用ViewFlipper切換到一個新的屏幕,當點擊第一個列表中的一個項目時按鈕。Android ViewFlipper和ListView

我已經添加了一個OnItemClickListener,以便當我單擊列表中的項目時,它將顯示一條消息。

問題是,當我在onItemClick()方法中添加showNext()方法時,應用程序只會與NullPointerException一起崩潰。

我只是在尋找鰭狀肢從列表屏幕到按鈕屏幕的過渡。

這是我使用的代碼片段。

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

這裏是我使用

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

任何幫助的XML將大大讚賞。

謝謝:)

更新:

以下是錯誤日誌中我得到:

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) 

這裏是我的主要活動類的代碼

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

請發佈您的錯誤日誌..... –

回答

0

你不打電話setContentView(R.layout.yourlayoutname);這意味着您的佈局不被放在scre上恩。結果是findViewById()將返回null給你。由於您正在嘗試撥打null.showNext();,您將在該行上收到nullPointerException。

+0

我打電話給setContentView(R.layout.main);在另一個類和它的作品,當我刪除「flippy.showNext();」 – mcbain

+0

發佈其他班級代碼。我仍然認爲這是錯誤。如果您有兩個獨立的活動,則setContentView()調用不會從一個轉移到另一個。 – FoamyGuy

+0

如果我有兩項活動,我該如何實施? – mcbain