尝试调用虚拟方法(片段中的问题视图)

时间:2018-09-23 05:55:34

标签: java android android-fragments nullpointerexception

ChatFragment.java:

raise AssertionError(whatever after comma)

错误:

public class ChatFragment extends Fragment {
private FirebaseListAdapter<ChatMessage> adapter;
View RootView;
EmojiconEditText emojiconEditText;
ImageView emojiButton,submitButton;
EmojIconActions emojIconActions;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View RootView = inflater.inflate(R.layout.fragment_chat,container,false);
    this.RootView = RootView;
    return RootView;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//:55
    emojiButton = (ImageView)this.RootView.findViewById(R.id.emoji_button);
    submitButton = (ImageView)this.RootView.findViewById(R.id.submit_button);
    emojiconEditText = (EmojiconEditText)this.RootView.findViewById(R.id.emojicon_edit_text);
    emojIconActions = new EmojIconActions(getActivity().getApplicationContext(),this.RootView,emojiButton,emojiconEditText);
    emojIconActions.ShowEmojicon();

我尝试了另一种方式(ChatFragment.java):

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    at dev.edmt.chatapp.ChatFragment.onCreate(ChatFragment.java:55)

错误:

public class ChatFragment extends Fragment {
private FirebaseListAdapter<ChatMessage> adapter;
View RootView;
EmojiconEditText emojiconEditText;
ImageView emojiButton,submitButton;
EmojIconActions emojIconActions;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View RootView = inflater.inflate(R.layout.fragment_chat,container,false);
    this.RootView = RootView;
    this.emojiButton = (ImageView)RootView.findViewById(R.id.emoji_button);
    this.submitButton = (ImageView)RootView.findViewById(R.id.submit_button);
    this.emojiconEditText = (EmojiconEditText)RootView.findViewById(R.id.emojicon_edit_text);
    return RootView;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//:60
    emojIconActions = new EmojIconActions(getActivity().getApplicationContext(),this.RootView,this.emojiButton,this.emojiconEditText);
    emojIconActions.ShowEmojicon();

我是新手,不理解片段。............................................... ................................................... ......................................

2 个答案:

答案 0 :(得分:0)

移动此代码:

 emojIconActions = new EmojIconActions(getActivity().getApplicationContext(),this.RootView,this.emojiButton,this.emojiconEditText);
    emojIconActions.ShowEmojicon();

onCreateView之前的return RootView;方法

答案 1 :(得分:0)

在片段中引用您的观点的正确形式是在您的onCreateView中:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View RootView = inflater.inflate(R.layout.fragment_chat,container,false);
    this.RootView = RootView;
    this.emojiButton = (ImageView)RootView.findViewById(R.id.emoji_button);
    this.submitButton = (ImageView)RootView.findViewById(R.id.submit_button);
    this.emojiconEditText = (EmojiconEditText)RootView.findViewById(R.id.emojicon_edit_text);

    // Your view objects are holding a reference
    emojIconActions = new EmojIconActions(getActivity().getApplicationContext(),this.RootView,this.emojiButton,this.emojiconEditText);
    emojIconActions.ShowEmojicon();
}

您可以在onCreate下面的图像中看到,在onCreateView之前调用了该方法。

Fragment life cycle