2012-10-29 75 views
1

所有代碼做工精細:setImageResource定製ArrayAdapter內外因顯示java.lang.NullPointerException

public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View row = inflater.inflate(R.layout.activity_contacts_detail_row_layout, parent, false); 
    ImageView smsLogo = (ImageView) row.findViewById(R.id.sms_logo); 
    smsLogo.setClickable(true); 
    smsLogo.setTag(position); 
    if (contactsRow.isSMSApproved == true) smsLogo.setImageResource(R.drawable.messages_green); 
    else smsLogo.setImageResource(R.drawable.messages_red); 

,直到該行:

smsLogo.setImageResource(....) 

的logcat:

10-29 22:46 :34.006:E/AndroidRuntime(935): java.lang.NullPointerException 10-29 22:46:34.006: E/AndroidRunti我(935):在 com.callsfreecalls.android.ContactsDetailArrayAdapter.getView(ContactsDetailArrayAdapter.java:107) 10-29 22:46:34.006:E/AndroidRuntime(935):at android.widget.AbsListView.obtainView 10/18 22:46:34.006:E/AndroidRuntime(935):at android.widget.ListView.measureHeightOfChildren(ListView.java:1216) 10-29 22:46:34.006:E/AndroidRuntime(935):at android.widget.ListView.onMeasure(ListView.java:1127)10-29 22:46:34.006:E/AndroidRuntime(935):at android.view.View.measure(View .java:8313)10-29 22:46:34.006: E/AndroidRuntime(935):at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566) 10-29 22:46:34.006:E/AndroidRuntime(935):at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)10-29 22:46:34.006:E/AndroidRuntime(935):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 10-29 22:46:34.006:E/AndroidRuntime(935):at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)10-29 22:46:34.006:E/AndroidRuntime(935):at android.view.View.measure(View.java:8313)10-29 22:46:34.006: E/AndroidRuntime(935):at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 10-29 22:46:34.006:E/AndroidRuntime(935):at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)10-29 22:46:34.006:E/AndroidRuntime(935): android.view.View.get.View.measure(View.java:8313)10-29 22:46:34.006: E/AndroidRuntime(935):at android.view.ViewRoot.performTraversals(ViewRoot.java:839)10 -29 22:46:34.006:E/AndroidRuntime(935):at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)10-29 22:46:34.006:E/AndroidRuntime(935):at android.os.Handler.dispatchMessage(Handler.java:99)10-29 22:46:34.006:E/AndroidRuntime(935):at android.os.Looper.loop(Looper.java:130)10- 29 22:46:34.006: E/AndroidRuntime(935):at android.app.ActivityThread.main(ActivityThread.java:3683)10-29 22:46:34.006:E/AndroidRuntime(935):at java.lang.reflect.Method .invokeNative(Native Method)10-29 22:46:34.006:E/AndroidRuntime(935):at java.lang.reflect.Method.invoke(Method.java:507)10-29 22:46:34.006: E/AndroidRuntime(935):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 10-29 22:46:34.006:E/AndroidRuntime(935):at com .android.internal.os.ZygoteInit.main(ZygoteInit。的java:597)

row_layout:

<ImageView 
    android:id="@+id/sms_logo" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_marginRight="6dip" 
    android:src="@drawable/messages_green" /> 

<ImageView 
    android:id="@+id/callLogo" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_marginLeft="60dp" 
    android:layout_marginTop="-60dp" 
    android:src="@drawable/callbuttonred" /> 

<TextView 
    android:id="@+id/contactsDetailData" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="120dp" 
    android:layout_marginTop="-43dp" 
    android:gravity="center" 
    android:shadowColor="#FFFFFFFF" 
    android:shadowDx="1" 
    android:shadowDy="1" 
    android:shadowRadius="1" 
    android:text="+380674878717" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textColor="#FF64396F" /> 

這是編碼生成:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_contacts_detail); 
    bar = ((ProgressBar) findViewById(R.id.progressContactDetailsUpdate)); 
    bar.setVisibility(View.VISIBLE); 

    contactsDetailsForUsing = this; 
    adapterDetails = new ContactsDetailArrayAdapter(getApplicationContext(), R.layout.activity_contacts_detail_row_layout); 
    lvContactDetails = (ListView) findViewById(R.id.listContactsDetailView); 
    lvContactDetails.setAdapter(adapterDetails); 
+1

發表您的'activity_contacts_detail_row_layout' – iTurki

+0

XML @iturki它的存在 – user170317

回答

1

我懷疑問題是在膨脹的佈局:

View row = inflater.inflate(R.layout.activity_contacts_detail_row_layout, parent, false); 

我不確定,但parent可能爲空。嘗試使用這樣的:

View row = inflater.inflate(R.layout.activity_contacts_detail_row_layout, null); 

UPDATE:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
    <!-- your three views: --> 
    <ImageView .... /> 
    <ImageView .... /> 
    <TextView .... /> 
</LinearLayout> 
+0

不幸的是,相同的結果爲\t \t \t \t查看排= inflater.inflate(R.layout .activity_contacts_detail_row_layout,null); – user170317

+0

請問您的XML有一個根佈局?我認爲它應該。如我錯了請糾正我。 – iTurki

+0

我正在使用示例來編程它。你可以發佈一些代碼給我看,我可以如何將行佈局添加到根佈局? – user170317

0

contactsRow.isSMSApproved - isSMSApproved爲空。對我來說是驚喜,是必須的原語在Java中初始化...

+0

這很奇怪!無論如何,很高興你知道了。 – iTurki

+0

@iturki第一個問題是固定的,但內存問題開始。你能提出一些建議嗎? – user170317

+0

我建議你開始一個新的問題,而不是改變這個範圍。它會更清晰。 – iTurki

相關問題