Lisek
Lisek

Reputation: 783

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(). 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. What I am trying to do is to refer to ListView from SendSectionFragment.xml. 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(); and 244 is mConversationView.setAdapter(mConversationArrayAdapter);

How can I deal with that? The SendSectionFragment is not the main fragment of my application.

Upvotes: 0

Views: 2335

Answers (1)

amit singh
amit singh

Reputation: 1422

Remove this line from setupCommand() -

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

and it in onCreateView(), like this-

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

Upvotes: 1

Related Questions