java - 如何在涉及 Fragment 的 Android 代码中找到空指针错误

标签 java android pointers android-fragments nullpointerexception

我已经尝试了 2 个小时来查找代码中的空指针,但没有成功。我希望你们中的一位比我能想象的更聪明。首先,我在这里遵循教程:http://youtu.be/Y2liEQV3tbQ?list=PLonJJ3BVjZW4lMlpHgL7UNQSGMERcDzHo作者:slideNerd。

这就是令我困惑的地方:在水平模式下,代码执行完美,并且显示内容更新为正确的信息。这意味着 if 语句的水平部分正在起作用。然而,当它处于纵向状态并且我单击 ListView 图标时,它会立即强制关闭并使用空指针。该错误来自以下行:

f2b.changeTextView(index);

如果我删除该行,第二个 Activity 将毫无问题地打开。我只是不知道是什么给我带来了这么多麻烦。

这是我的类(class):

主要 Activity :

public class MainActivity extends Activity implements  FragmentA.Communicator{

    private FragmentA f1a;
    private FragmentB f2b;
    private FragmentManager manager;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        manager=getFragmentManager();

        f1a = (FragmentA) manager.findFragmentById(R.id.fragment1a);

        f1a.setCommunicator(this); //the respond method that is implemented from Fragment a

        f2b = (FragmentB) manager.findFragmentById(R.id.fragment2b);

    }

    @Override
    public void respond(int index) {

        //Check if portrait mode
        if (f2b != null && f2b.isVisible()){ //Horizontal

            f2b.changeTextView(index);
        } else { //Vertical

            Intent intent = new Intent(this, Activity2.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }
}

fragment A:

public class FragmentA extends Fragment implements AdapterView.OnItemClickListener {

    ListView listView;

    Communicator comm;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_a, container, false);
        return view;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        listView = (ListView) getActivity().findViewById(R.id.fragment_a_list_view);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.chapters, android.R.layout.simple_list_item_1);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        comm.respond(position);
    }

    public void setCommunicator(Communicator communicator){
        this.comm = communicator;
    }

    //Interface to allow access between fragments
    public interface Communicator{
        public void respond(int index);
    }
}

fragment B:

public class FragmentB extends Fragment {

    TextView textView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_b, container, false);

        return view;

    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        textView = (TextView) getActivity().findViewById(R.id.fragment_b_text_view);


    }

    public void changeTextView(int position){

        String [] descriptions = getResources().getStringArray(R.array.descriptions);

        textView.setText(descriptions[position]);
    }
}

Activity 2:

public class Activity2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);

        Intent intent = getIntent();

        int index = intent.getIntExtra("index", 0);

        //reference the fragment
        FragmentB f2b = (FragmentB) getFragmentManager().findFragmentById(R.id.fragment2b);
        if (f2b != null){

            f2b.changeTextView(index);

        }

    }
}

这是 logcat 中的错误代码:

Caused by: java.lang.NullPointerException at com.pgmacdesign.fragmentexamples3_flexibleui.FragmentB.changeTextView(FragmentB.java:42) at com.pgmacdesign.fragmentexamples3_flexibleui.Activity2.onCreate(Activity2.java:25)

我已经确认索引/位置变量传递正确。如果有人对如何查找空指针错误有任何建议,我会很乐意提供帮助。

最佳答案

在您的 fragment B 中,

尝试将 textView = (TextView) getActivity().findViewById(R.id.fragment_b_text_view) 替换为 textView = (TextView) view.findViewById(R.id.fragment_b_text_view) 并将其从 onActivityCreated() 移至 onCreateView()。

希望这有帮助。

关于java - 如何在涉及 Fragment 的 Android 代码中找到空指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28231524/

相关文章:

java - 如何从多项式字符串中获取系数和指数?

android - 找不到 'kotlinPlugin' 和 'darkThemeColors'

pointers - clojure中的指针循环

android - 无法在 android 中使用 PowerManager 的 goToSleep API

java - `onPageFinished` 是在 `onReceivedError` 之后调用的吗?

c++ - 为什么以及何时通过指针在 C++ 中传递类类型?

c++ - 删除指针的段错误

java - 按值获取枚举常量

java - 使用 itext java 使用自定义字体编写特殊字符

java - 如何在 Java-8 中仅并行运行某些中间操作?