3

嗨我創建了一個帶有listview的片段。每當我運行代碼時,我在我的Fragment中收到java.lang.nullpointerexception。錯誤日誌表示問題出現在第no行。 24在Android片段中獲取Java.Lang.NullPointerException

Line No. 24: ListView messsagesListView = (ListView) getView.findViewById(R.id.messages_list)

這裏是我的代碼:

我的片段類:

public class MessageFragment extends Fragment { 

private List<MessageItem> messagesList = new ArrayList<MessageItem>(); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    ListView messagesListView = (ListView) getView() 
      .findViewById(R.id.messages_list); 

    populateMessagesList(); 

    ArrayAdapter<MessageItem> messageListAdapter = new MessageListAdapter(); 
    messagesListView.setAdapter(messageListAdapter); 

    return inflater.inflate(R.layout.messages_fragment, container); 
} 

public void populateMessagesList() { 

    messagesList.add(new MessageItem("Gary Walts", 
      "10/13/2013, 5:31:55 PM", "22")); 
    messagesList.add(new MessageItem("Martha Flames", 
      "10/13/2013, 6:32:35 PM", "41")); 
    messagesList.add(new MessageItem("Jack Bones", 
      "10/12/2013, 9:45:32 AM", "165")); 
    messagesList.add(new MessageItem("Chitambaram Patel", 
      "10/07/2013, 5:31:55 PM", "21")); 
    messagesList.add(new MessageItem("Sally Walts", 
      "10/02/2013, 5:31:55 PM", "323")); 
    messagesList.add(new MessageItem("Setina Gabriole", 
      "09/24/2013, 5:31:55 PM", "257")); 
    messagesList.add(new MessageItem("Carl Fosters", 
      "09/15/2013, 5:31:55 PM", "22")); 
} 

class MessageListAdapter extends ArrayAdapter<MessageItem> { 

    public MessageListAdapter() { 
     super(getActivity(), R.layout.messages_list_item, messagesList); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     View itemView = convertView; 

     if (itemView == null) { 
      itemView = getActivity().getLayoutInflater().inflate(
        R.layout.messages_list_item, parent, false); 
     } 

     MessageItem messageItem = messagesList.get(position); 

     TextView friendName = (TextView) itemView 
       .findViewById(R.id.friend_name); 
     friendName.setText(messageItem.getFriendName()); 

     TextView dateAndTime = (TextView) itemView 
       .findViewById(R.id.date_time); 
     dateAndTime.setText(messageItem.getDateAndTime()); 

     TextView numberOfMessages = (TextView) itemView 
       .findViewById(R.id.number_messages); 
     numberOfMessages.setText(messageItem.getNumberOfMessages()); 

     return itemView; 
    } 
} 
} 

這究竟是爲什麼?提前致謝。

+0

首先設置視圖(佈局)進行拆分,然後用findview ... –

回答

7

試試這個

View v = inflater.inflate(R.layout.messages_fragment, null); 
ListView messagesListView = (ListView) v 
     .findViewById(R.id.messages_list); 
return v; 

或getView返回

return inflater.inflate(R.layout.messages_fragment, null); 

然後

public void onActivityCreated(Bundle savedInstanceState) { 
    View v= getView(); 
    ListView messagesListView = (ListView) v.findViewById(R.id.messages_list); 
    ... // rest of the code 
} 
+1

容器應該是null – Blackbelt

+0

@blackbelt你是對的 – Raghunandan

+0

@blackbelt可能可以使用'getView()'初始化'onActivityCreated'中的listview。 – Raghunandan

3

在onCreateView()getview(),因爲你沒有任何誇大視圖返回null ..像這樣改變你的代碼..

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    View view=inflater.inflate(R.layout.messages_fragment, null); 
    ListView messagesListView = (ListView) view 
      .findViewById(R.id.messages_list); 

    populateMessagesList(); 

    ArrayAdapter<MessageItem> messageListAdapter = new MessageListAdapter(); 
    messagesListView.setAdapter(messageListAdapter); 

    return view; 
} 
0

我有同樣的問題也和我解決它通過一個參考對象緊跟的託管視圖:

public class MyFragment extends Fragment { 
    private View mContentView = null; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     mContentView = inflater.inflate(R.layout.dialog_disclaimer, null); 
     builder.setView(mContentView); 

     return builder.create(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
     ListView messagesListView = (ListView) mContentView.findViewById(R.id.messages_list); 
    } 
} 
相關問題