简体   繁体   English

片段上findViewById的NullPointerException

[英]NullPointerException on findViewById from Fragment

I have been dealing with this problem way too much and even after trying to implement other ideas from already asked questions, I could not get it worked. 我对这个问题的处理方式太多了,即使尝试从已经提出的问题中实施其他想法后,我也无法使它起作用。

I am working on fragments. 我正在研究片段。 I am trying to find a view from fragment of my choice and use it in a method which is called in onStart() . 我试图从我选择的片段中找到一个视图,并在onStart()onStart()的方法中使用它。 This is how it looks: 它是这样的:

@Override
protected void onStart() {
    super.onStart();
    Log.e(TAG, "On start");
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    }
    else {
        if (mCommandService == null) 
            setupCommand();
    }
}
private void setupCommand() {
    Log.d(TAG, "setupChat()");

    mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
    mConversationView = (ListView) findViewById(R.id.in);
    mConversationView.setAdapter(mConversationArrayAdapter);

    mOutEditText = (EditText) findViewById(R.id.edit_text_out);
    mOutEditText.setOnEditorActionListener(mWriteListener);

    mSendButton = (Button) findViewById(R.id.action_send);
    mSendButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Send a message using content of the edit text widget
            TextView view = (TextView) findViewById(R.id.edit_text_out);
            String message = view.getText().toString();
            sendMessage(message);
        }
    });
    mCommandService = new CommandService(this, mHandler);
    mOutStringBuffer = new StringBuffer("");
}

@SuppressLint("ValidFragment")
public class SendSectionFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.send, container, false);

            rootView.findViewById(R.id.action_send)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });

            return rootView;
        }
    }

The SendSectionFragment fragment is using its own .xml . SendSectionFragment片段使用自己的.xml What I am trying to do is to refer to ListView from SendSectionFragment.xml . 我想做的是从SendSectionFragment.xml引用ListView What is causing trouble: 造成麻烦的原因:

01-06 18:05:21.259: E/AndroidRuntime(17868): Caused by: java.lang.NullPointerException
01-06 18:05:21.259: E/AndroidRuntime(17868):    at com.receive.bluereceive.Main.setupCommand(Main.java:244)
01-06 18:05:21.259: E/AndroidRuntime(17868):    at com.receive.bluereceive.Main.onStart(Main.java:217)

where 217 is setupCommand(); 其中217是setupCommand(); and 244 is mConversationView.setAdapter(mConversationArrayAdapter); 244是mConversationView.setAdapter(mConversationArrayAdapter);

How can I deal with that? 我该如何处理? The SendSectionFragment is not the main fragment of my application. SendSectionFragment不是我的应用程序的主要片段。

Remove this line from setupCommand() - 从setupCommand()删除此行-

mConversationView = (ListView) findViewById(R.id.in);

and it in onCreateView(), like this- 然后放在onCreateView()中,就像这样-

mConversationView = (ListView) rootView.findViewById(R.id.in);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM