Android片段 - null对象引用

时间:2017-02-26 16:45:24

标签: java android android-fragments nullpointerexception

我正在尝试使用主要活动中的片段更改某些图像上的alpha。但是,我尝试的所有内容都会出错:

  

java.lang.NullPointerException:尝试调用虚方法   'android.view.View android.view.View.findViewById(int)'上的null   对象参考

我从我的主要活动中调用公共函数,例如: 主要活动:

public void decadeDotsFragment(){

                DecadeDotsFragment fragment = new DecadeDotsFragment();
                FragmentManager manager = getSupportFragmentManager();
                manager.beginTransaction()
                        .replace(R.id.dots_fragment, fragment)
                        .addToBackStack(null)
                        .commit();

        fragment.selectDot(0);

    }

我做错了什么?

片段:

public class DecadeDotsFragment extends Fragment {

    ImageView dot_0, dot_1, dot_2, dot_3;
    View rootView;

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

        rootView = inflater.inflate(R.layout.decade_dots_indicator_fragment, container, false);

        return rootView;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){

    }

    public void selectDot(int position){

        dot_0 = (ImageView) rootView.findViewById(R.id.dot_0);
        dot_1 = (ImageView) rootView.findViewById(R.id.dot_1);
        dot_2 = (ImageView) rootView.findViewById(R.id.dot_2);
        dot_3 = (ImageView) rootView.findViewById(R.id.dot_3);

        switch (position){
            case 0:
                dot_0.setAlpha((float) 1);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
                break;
            case 1:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 1);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
                break;
            case 2:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 1);
                dot_3.setAlpha((float) 0.5);
                break;
            case 3:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 1);
                break;
            default:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
        }

    }


}

3 个答案:

答案 0 :(得分:1)

这是片段的基本违规。您正在直接与活动本身的片段进行交互。

fragment.selectDot(0);

不应该是你怎么称呼这种方法。如果你想调用片段中的任何方法总是创建一个接口并使用它进行交互,否则它可能会引起像你这样的奇怪问题。

教程: - Android Activity to Fragment Communication

答案 1 :(得分:1)

以这样的方式做正确的

public void decadeDotsFragment(){

    DecadeDotsFragment fragment = new DecadeDotsFragment();
    fragment.selectDot(0);//here should pass required position
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .replace(R.id.dots_fragment, fragment)
            .addToBackStack(null)
            .commit();
}
  

并调用您的方法" selectDot()"来自" onCreateView()"像这样

public class DecadeDotsFragment extends Fragment {
    ImageView dot_0, dot_1, dot_2, dot_3;
    View rootView;
    private int mPosition;

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

        rootView = inflater.inflate(R.layout.decade_dots_indicator_fragment, container, false);

        return rootView;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
       setData();
    }

    public void selectDot(int position){
        mPosition=position;
    }

    private void setData() {
        dot_0 = (ImageView) rootView.findViewById(R.id.dot_0);
        dot_1 = (ImageView) rootView.findViewById(R.id.dot_1);
        dot_2 = (ImageView) rootView.findViewById(R.id.dot_2);
        dot_3 = (ImageView) rootView.findViewById(R.id.dot_3);

        switch (mPosition){
            case 0:
                dot_0.setAlpha((float) 1);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
                break;
            case 1:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 1);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
                break;
            case 2:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 1);
                dot_3.setAlpha((float) 0.5);
                break;
            case 3:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 1);
                break;
            default:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
        }

    }


}

答案 2 :(得分:0)

将您的排名从Activity转移到fragment

DecadeDotsFragment fragment = new DecadeDotsFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("value", your_position);  //pass your position
    fragment.setArguments(bundle);    //send data
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .replace(R.id.dots_fragment, fragment)
            .addToBackStack(null)
            .commit();

onCreateView()

中添加imageview参考

现在在你的片段中使用:

int position;   //Declare

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

    rootView = inflater.inflate(R.layout.decade_dots_indicator_fragment, container, false);
     dot_0 = (ImageView) rootView.findViewById(R.id.dot_0);
    dot_1 = (ImageView) rootView.findViewById(R.id.dot_1);
    dot_2 = (ImageView) rootView.findViewById(R.id.dot_2);
    dot_3 = (ImageView) rootView.findViewById(R.id.dot_3);
    position = getArguments().getInt("value");  //get the position
    selectDot(position);   //call here
    return rootView;

}

Android框架希望您的Fragment在onCreateView()方法中创建其视图。在框架调用此方法后,View变为可用。