在 Android 片段中获取 Java.Lang.NullPointerException

Posted

技术标签:

【中文标题】在 Android 片段中获取 Java.Lang.NullPointerException【英文标题】:Getting a Java.Lang.NullPointerException in Android Fragment 【发布时间】:2013-10-25 17:27:09 【问题描述】:

您好,我正在创建一个包含列表视图的片段。每当我运行代码时,我都会在我的Fragment 中得到一个java.lang.nullpointerexception。错误日志显示问题出在第 1 行。 24

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;
    


为什么会这样?提前致谢。

【问题讨论】:

先将视图(布局)设置为fragment,然后使用findview... 【参考方案1】:

试试这个

 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

【讨论】:

容器应该为空 @blackbelt 大概可以使用getView() 来初始化onActivityCreated 中的listview。 @blackbelt 和 Raghunandan,谢谢你们。成功了【参考方案2】:

在 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;

【讨论】:

【参考方案3】:

我也遇到了同样的问题,我通过在宿主视图中保持引用对象来解决它:

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);
    

【讨论】:

以上是关于在 Android 片段中获取 Java.Lang.NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.database.sqlite异常(代码片段

java.lang.NullPointerException:Android 尝试调用虚拟方法错误

Android Studio“尚未附加片段”

在android中获取java.lang.NullPointerException

在android和java.lang.IllegalStateException中获取联系电话

在android中的一个Activity中添加两个片段