所有代碼做工精細: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);
發表您的'activity_contacts_detail_row_layout' – iTurki
XML @iturki它的存在 – user170317