相关疑难解决方法(0)

NullPointerException:FragmentManager.beginTransaction()

尝试从第一个片段启动片段时出现此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentManager.beginTransaction()' on a null object reference
Run Code Online (Sandbox Code Playgroud)

这是我收到错误的方法:

    @Override
    public void onClick(View v) {
        Fragment fragment = new PropertyFragment();
        if (fragment != null) {
            FragmentManager fragmentManager = fragment.getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            fragmentTransaction.replace(R.id.rent_viewer, fragment);  
            fragmentTransaction.addToBackStack(null);

            // Commit the transaction
            fragmentTransaction.commit();  
        }
    }
Run Code Online (Sandbox Code Playgroud)

确切地说,以下代码指令导致错误:

fragmentManager.beginTransaction();
Run Code Online (Sandbox Code Playgroud)

以下是类和嵌套类的外观:

public class …
Run Code Online (Sandbox Code Playgroud)

android nullpointerexception fragment fragmenttransaction

10
推荐指数
1
解决办法
3万
查看次数

检查显示空指针异常,而我正在检查空概率

我的项目中有这样的代码:

public class MyExpAdapter extends BaseExpandableListAdapter
{

    @Override
    public int getChildrenCount(final int groupPosition)
    {
        return this.getCountry(groupPosition) == null ? 0 :
                this.getCountry(groupPosition).getCityList() == null ? 0 :
                        this.getCountry(groupPosition).getCityList().size();
    }
.
.
.
}
Run Code Online (Sandbox Code Playgroud)

虽然我正在检查空概率,但检查员仍然突出显示最后两行,如下图所示.任何的想法?谢谢.

在此输入图像描述

更新

我甚至改变了我的方法,不使用三元运算符,但它仍然很糟糕:( 在此输入图像描述

java android nullpointerexception inspector android-studio

4
推荐指数
1
解决办法
128
查看次数