2012-09-11 29 views
-1

我的代碼中有listview,我想對其設置adapter即使在初始化後,該視圖也爲空

但問題是,即使在初始化listview之後,它仍然是null導致nullPointerException。 (我通過日誌記錄和調試進行了檢查)

我無法從該XML訪問任何視圖。

我錯過了什麼?任何幫助讚賞。

XML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:scrollbars="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <ListView 
      android:id="@+id/lvToday" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:divider="#FFF" 
      android:dividerHeight="2dp" /> 

     <ListView 
      android:id="@+id/lvTomorrow" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lvToday" 
      android:divider="#FFF" 
      android:dividerHeight="2dp" /> 
    </RelativeLayout> 

</ScrollView> 

活動文件

public class DashboardActivity extends Activity { 

ListView lvToday, lvTomorrow; 
TextView lblCall; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dashboard); 
    init(); 
} 

private void init() { 
    lvToday = (ListView) findViewById(R.id.lvToday); 
    lvTomorrow = (ListView) findViewById(R.id.lvTomorrow); 

    TodayAppAdapter adapter = new TodayAppAdapter(DashboardActivity.this, 
      DashboardActivity.this); 
       // This line is giving NULL 
    lvToday.setAdapter(adapter); 

    TomorrowAppAdapter adapter1 = new TomorrowAppAdapter(
      DashboardActivity.this, DashboardActivity.this); 
       // This line is giving NULL 
    lvTomorrow.setAdapter(adapter1); 
} 
} 
+0

檢查XML名稱是否是「儀表板」 – Chet

回答

1

1)試食菜單:項目 - >清除...

2)如果你有多個版本爲XML佈局(例如layout-large,layout-xlarge,...),檢查它們是否都有你的視圖。

+0

它工作。 'layout' nad'layout-land'中的xml副本是不同的。這就是爲什麼這個問題。 – GAMA

0

你缺少你的佈局

+0

它在那裏。這是我的錯誤,我只複製粘貼選定的代碼。檢查我更新的問題。 – GAMA

0

一個TextView把一個String [] 阿龍摹與您的適配器的名字在你的

OnDraw() 

方法

相關問題