java - Fragment 中的 findViewById 出现 NullPointerException

标签 java android android-fragments nullpointerexception

我已经太多地处理这个问题了,即使在尝试从已经提出的问题中实现其他想法之后,我也无法让它发挥作用。

我正在研究 fragment 。我试图从我选择的 fragment 中找到一个 View ,并在 onStart() 中调用的方法中使用它。它看起来是这样的:

@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;
        }
    }

SendSectionFragment fragment 使用其自己的 .xml。我想做的是引用 SendSectionFragment.xml 中的 ListView 。造成问题的原因:

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)

其中 217 是 setupCommand();,244 是 mConversationView.setAdapter(mConversationArrayAdapter);

我该如何处理这个问题? SendSectionFragment 不是我的应用程序的主要 fragment 。

最佳答案

从 setupCommand() 中删除此行 -

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

在 onCreateView() 中,像这样 -

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

关于java - Fragment 中的 findViewById 出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20955526/

相关文章:

java - 我需要使用数据库中的值创建 XYLineChart (Java(Mysql)

java - java解密期间出现BadPaddingException

android - R8 说类型被多次定义 : in build\. 转换和在 build\tmp\kotlin-classes

android - getLoaderManager().initLoader() 不接受 'this' 作为参数,尽管类 (ListFragment) 没有支持库

java - JUnit : when testing method with annotation, 测试忽略注释

java - 基于鼠标移动旋转四元数(OpenGL 和 Java)

android - 计算纬度/经度列表之间的距离

无法重新分配 Android val

android - 从 fragment 中处理相机应用程序而不会崩溃的正确方法

android - 使用 Robolectric 更改配置