2011-12-31 59 views
0

幫助請!我一直試圖讓我的列表視圖儘可能高效地工作,但它保持與NullPointerException的強制關閉。這是我的LogCat。我真的不知道。我的列表視圖中的空指針異常

12-31 14:32:35.552: D/AndroidRuntime(240): Shutting down VM 
12-31 14:32:35.552: W/dalvikvm(240): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
12-31 14:32:35.552: E/AndroidRuntime(240): Uncaught handler: thread main exiting due to uncaught exception 
12-31 14:32:35.602: E/AndroidRuntime(240): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.FirstLoginActivity}: java.lang.NullPointerException 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.os.Looper.loop(Looper.java:123) 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.app.ActivityThread.main(ActivityThread.java:4363) 
12-31 14:32:35.602: E/AndroidRuntime(240): at java.lang.reflect.Method.invokeNative(Native Method) 
12-31 14:32:35.602: E/AndroidRuntime(240): at java.lang.reflect.Method.invoke(Method.java:521) 
12-31 14:32:35.602: E/AndroidRuntime(240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
12-31 14:32:35.602: E/AndroidRuntime(240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
12-31 14:32:35.602: E/AndroidRuntime(240): at dalvik.system.NativeStart.main(Native Method) 
12-31 14:32:35.602: E/AndroidRuntime(240): Caused by: java.lang.NullPointerException 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.content.ContextWrapper.getResources(ContextWrapper.java:80) 
12-31 14:32:35.602: E/AndroidRuntime(240): at com.cerealBarApps.FirstLoginActivity.<init>(FirstLoginActivity.java:30) 
12-31 14:32:35.602: E/AndroidRuntime(240): at java.lang.Class.newInstanceImpl(Native Method) 
12-31 14:32:35.602: E/AndroidRuntime(240): at java.lang.Class.newInstance(Class.java:1479) 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
12-31 14:32:35.602: E/AndroidRuntime(240): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409) 
12-31 14:32:35.602: E/AndroidRuntime(240): ... 11 more 
12-31 14:32:36.962: I/dalvikvm(240): threadid=7: reacting to signal 3 
12-31 14:32:37.192: I/dalvikvm(240): Wrote stack trace to '/data/anr/traces.txt' 
12-31 14:32:40.592: I/Process(240): Sending signal. PID: 240 SIG: 9 

這是我的列表視圖代碼

public class FirstLoginActivity extends ListActivity { 
    Context mContext; 
    List mList; 
    String[] testcontacts; 

    MessageView aa = null; 

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

     testcontacts = getResources() 
       .getStringArray(R.array.testcontacts_array); 

     aa = new MessageView(); 

     ListView lv = getListView(); 
     lv.setAdapter(aa); 
     lv.setTextFilterEnabled(true); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // When clicked, show a toast with the TextView text 
       Toast.makeText(getApplicationContext(), 
         ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
class MessageView extends ArrayAdapter<String> { 
     MessageView() { 
      super(FirstLoginActivity.this, android.R.layout.activity_list_item, 
        testcontacts); 
      // TODO Auto-generated constructor stub 
     } 

     public View getView(int position, View convertview, ViewGroup parent) { 
      Log.d("Ebz", "inside getView method"); 
      ViewHolder holder; 
      View v = convertview; 
      if (v == null) { 
       Log.d("Ebz", "if v == null"); 
       LayoutInflater inflater = getLayoutInflater(); 
       v = inflater.inflate(R.layout.list_items, null); 
       holder = new ViewHolder(); 
       holder.firstLine = (TextView) v.findViewById(R.id.firstLine); 
       holder.secondLine = (TextView) v.findViewById(R.id.secondLine); 
       holder.icon1 = (ImageView) v.findViewById(R.id.icon1); 
       holder.icon2 = (ImageView) v.findViewById(R.id.icon2); 
       v.setTag(holder); 
      } else { 
       holder = (ViewHolder) v.getTag(); 
      } 
      holder.firstLine.setText(testcontacts[position]); 
      holder.secondLine.setText(testcontacts[position]); 
      holder.icon1.setImageBitmap(null); 
      holder.icon2.setImageBitmap(null); 
      // call the images directly? 
      return v; 
     } 

     class ViewHolder { 
      TextView firstLine; 
      TextView secondLine; 
      ImageView icon1; 
      ImageView icon2; 

     } 
    } 
} 

我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="?android:attr/listPreferredItemHeight" 
     android:padding="6dip" > 

     <ImageView 
      android:id="@+id/icon1" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="6dip" 
      android:src="@drawable/ic_launcher" /> 

     <TextView 
      android:id="@+id/secondLine" 
      android:layout_width="fill_parent" 
      android:layout_height="26dip" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:layout_toRightOf="@id/icon1" 
      android:ellipsize="marquee" 
      android:singleLine="true" 
      android:text="Some more information" /> 

     <TextView 
      android:id="@+id/firstLine" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_above="@id/secondLine" 
      android:layout_alignParentTop="true" 
      android:layout_alignWithParentIfMissing="true" 
      android:layout_toRightOf="@id/icon1" 
      android:gravity="center_vertical" 
      android:text="Some Information" /> 

     <ImageView 
      android:id="@+id/icon2" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="6dip" 
      android:src="@drawable/ic_launcher" /> 

    </RelativeLayout> 

回答

3

你的代碼是在實例初始化結束了FirstLoginActivity對象。

最有可能的罪魁禍首是這一行:

String[] testcontacts = getResources().getStringArray(
     R.array.testcontacts_array); 

移動初始化到onCreate處理器,getResources在這一點上可能是返回一個null - 它是在初始化序列太早所有活動佈線到位。

String[] testcontacts; 

... 

    // in onCreate 
    testcontacts = getResources().getStringArray(R.array.testcontacts_array); 

你應該打第二個NullPointerException異常,當你達到:

ListView lv = null; 
    lv.setAdapter(aa); 

在其他的答案中指出 - 你錯過了一個電話findViewById

+0

謝謝,實際上是我的問題,但是我可以設置listView lv = getListView(); 我的ListView沒有顯示我如何在XML中定製它,我會繼續努力尋找它,試圖找出原因,但如果你能告訴我爲什麼會爲我節省很多時間。先謝謝了! 我發佈了我的XML和更新的代碼 – 2011-12-31 20:56:27

+1

@EbenezerAckon:如果您對XML或顯示有問題,請發佈一個單獨的問題 - 這與您的原始問題無關。 – Mat 2011-12-31 20:58:45

+0

好的,謝謝。我想通了我的問題 – 2011-12-31 21:04:21

1

在代碼中,你初始化期間,在下面的代碼行設置的ListView對象爲null:

ListView lv = null; 

即罰款初始化。但是,你應該做這樣的事情,實際使用LV對象

lv = findViewById(R.layout.<yourlistviewid>); 

一旦你得到一個參照列表視圖對象之前,您可以執行如設置adapter..etc所有其他操作。在此之前,你會得到一個空指針異常。

1

你忘了在這裏初始化ListView

ListView lv = null; 
lv.setAdapter(aa); 
lv.setTextFilterEnabled(true); 

將其初始化如下。

ListView lv = (ListView) v.findViewById(R.id.ListViewID_From_XML); 
lv.setAdapter(aa); 
lv.setTextFilterEnabled(true);