2014-06-15 131 views
0

我創建CreateEvent活動的Android - findViewById(INT ID)始終返回null

 @Override 
     public void onClick(View v) { 
      Intent in = new Intent(UserActivity.this, CreateEvent.class); 
      startActivity(in); 
     } 
    }); 

在CreateEvent活動,我想創建ListView和這裏是我爲它的代碼:

@Override 
public void friendsDownlaoded(ArrayList<User> friends) { 
    this.friends = friends; 
    this.adapter = new InviteFriendAdapter(getLayoutInflater()); 
    this.friendListview.setAdapter(adapter); 
    removeRoation();  
    setUpContent(); 
} 

和這裏是InviteFriendAdapter的代碼,它是內部類,它可以看到ArrayList的朋友:

private class InviteFriendAdapter extends BaseAdapter { 
    private LayoutInflater inflater; 

    public InviteFriendAdapter(LayoutInflater inflater) { 
     this.inflater = inflater; 
    } 

    @Override 
    public int getCount() { 
     return friends.size(); 
    } 

    @Override 
    public Object getItem(int i) { 
     return friends.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     return i; 
    } 

    @Override 
    public View getView(int position, View myView, ViewGroup vg) { 
     FriendHolder frndHolder; 
     User user = friends.get(position); 


     if (myView == null) { 
      myView = this.inflater.inflate(R.layout.invite_friends_event_item, null,false); 
      ImageView img= (ImageView) findViewById(R.id.inviteCheckBox); 
      frndHolder = new FriendHolder(); 
      frndHolder.hisImage = (ImageView) findViewById(R.id.inviteCheckBox); 
      frndHolder.hisNumber = (TextView) findViewById(R.id.inviteHisNumber); 
      frndHolder.hisName = (TextView) findViewById(R.id.inviteHisName); 

      myView.setTag(frndHolder); 
     } else { 
      frndHolder = (FriendHolder) myView.getTag(); 
     } 


     frndHolder.hisName.setText("aa"); 
     frndHolder.hisNumber.setText("uUU"); 

     return myView; 
    } 

} 

private static class FriendHolder { 
    TextView hisName; 
    TextView hisNumber; 
    ImageView hisImage; 
} 

我已經ch ecked ArrayList的朋友,它不是null; 該程序在適配器getView()拋出NullPointException,原因是當myView == null - (ImageView)findViewById(R.id.inviteCheckBox);返回null

下面是項目的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/possibleFriendInviteHolder" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/inviteHisName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_margin="@dimen/possible_his_number_margin" 
     android:textSize="@dimen/possible_his_name_text" /> 

    <TextView 
     android:id="@+id/inviteHisNumber" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/inviteHisName" 
     android:layout_margin="@dimen/possible_his_number_margin" 
     android:textSize="@dimen/possible_his_number_text" /> 

    <ImageView 
     android:id="@+id/inviteCheckBox" 
     android:layout_width="@dimen/smile_pic_width" 
     android:layout_height="@dimen/smile_pic_height" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_margin="@dimen/possible_check_box_margin" 
     android:contentDescription="@string/image" 
     android:scaleType="fitXY" /> 

</RelativeLayout> 

我無法找到網絡解決方案。我創建了適配器幾次,它運行良好,但現在.. 任何人都可以告訴我爲什麼findViewById()返回null? 預先感謝您

+0

nullPointexception在這裏:frndHolder.hisName.setText(「aa」); becasue frndHolder.hisName爲空 –

回答

1

在一個適配器getView(),呼籲視圖findViewById()你膨脹,不是活動視圖層次結構(如果適配器處於活性的非靜態內部類)。

在您的代碼中,將findViewById()替換爲myView.findViewById()

+0

謝謝。我試圖找到一個小時的問題。再次感謝 –