java - 在 onCreate() 方法运行之前命名 View 时出错

标签 java android layout-inflater oncreate

运行此代码时,应用程序立即崩溃。

Error: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{(...).MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

通过研究,我发现崩溃的原因很可能是我在onCreate()之前命名了两个 View 。方法已运行。我可以在 onCreate() 中命名这些 View 方法和以下监听器,但 View 不能公开,因此它们将彼此独立,这将阻止我的应用程序按预期工作。 有人知道如何在不使这些 View 彼此独立的情况下防止此问题吗?

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    public ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_layout0);
    LayoutInflater inflater = getLayoutInflater();
    public View rectimage3 = inflater.inflate(R.layout.my_rectview, constraintLayout, false);

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

        final ImageView rectimage = (ImageView) findViewById(R.id.rectimage);

        //set Listeners
        rectimage.setOnTouchListener(new MySecOnTouchListener());
        rectimage3.setOnTouchListener(new MyOnTouchListener());
        constraintLayout.setOnDragListener(new MyDragListener());
    }

    //OnTouchListener of the first Rect
    private final class MySecOnTouchListener implements View.OnTouchListener {
        public boolean onTouch(View sview, MotionEvent motionEvent){
            int action = motionEvent.getAction();
            if(action == MotionEvent.ACTION_DOWN);
                rectimage3.setVisibility(View.VISIBLE);
                constraintLayout.addView(rectimage3);

                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(rectimage3);
                rectimage3.startDrag(data, shadowBuilder, rectimage3, 0);
                rectimage3.setVisibility(View.INVISIBLE);
                return true;
            } else{
                return false;
            }
        }
    }

    //OnTouchListener of the movable Rects
    private final class MyOnTouchListener implements View.OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent){
            int action = motionEvent.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            } else {
                return false;
            }
        }
    }

    //OnDragListener of the Layout
    class MyDragListener implements View.OnDragListener{
        @Override
        public boolean onDrag(View v, DragEvent event) {
            View view = (View) event.getLocalState();

            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    view.setVisibility(View.VISIBLE);
                    break;
                case DragEvent.ACTION_DRAG_LOCATION:
                    view.setX(event.getX()-(view.getWidth()/2));
                    view.setY(event.getY()-(view.getHeight()/2));
                    break;
                case DragEvent.ACTION_DROP:
                    break;
            }
            return true;
        }
    }
}

最佳答案

您必须仅在 main 方法内部或 main 方法之后绑定(bind) View 和访问方法。

public class MainActivity extends AppCompatActivity {
    public ConstraintLayout constraintLayout;
    LayoutInflater inflater ;
    public View rectimage3 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_layout0);
    inflater = getLayoutInflater();
    rectimage3 = inflater.inflate(R.layout.my_rectview, constraintLayout, false);
    final ImageView rectimage = (ImageView) findViewById(R.id.rectimage);

    //set Listeners
    rectimage.setOnTouchListener(new MySecOnTouchListener());
    rectimage3.setOnTouchListener(new MyOnTouchListener());
    constraintLayout.setOnDragListener(new MyDragListener());
}

//OnTouchListener of the first Rect
private final class MySecOnTouchListener implements View.OnTouchListener {
    public boolean onTouch(View sview, MotionEvent motionEvent){
        int action = motionEvent.getAction();
        if(action == MotionEvent.ACTION_DOWN);
            rectimage3.setVisibility(View.VISIBLE);
            constraintLayout.addView(rectimage3);

            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(rectimage3);
            rectimage3.startDrag(data, shadowBuilder, rectimage3, 0);
            rectimage3.setVisibility(View.INVISIBLE);
            return true;
        } else{
            return false;
        }
    }
}

//OnTouchListener of the movable Rects
private final class MyOnTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent){
        int action = motionEvent.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
}

//OnDragListener of the Layout
class MyDragListener implements View.OnDragListener{
    @Override
    public boolean onDrag(View v, DragEvent event) {
        View view = (View) event.getLocalState();

        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                view.setVisibility(View.VISIBLE);
                break;
            case DragEvent.ACTION_DRAG_LOCATION:
                view.setX(event.getX()-(view.getWidth()/2));
                view.setY(event.getY()-(view.getHeight()/2));
                break;
            case DragEvent.ACTION_DROP:
                break;
        }
        return true;
    }
}

}

关于java - 在 onCreate() 方法运行之前命名 View 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48301086/

相关文章:

java - 在 Blackberry Java 中剥离一串 html 标签

java - 在执行测试之前验证外部系统是否可用的最佳位置是什么?

java - JVM + Linux + Intel 的超线程 =

android - 如何更新具有从 Sqlite 填充的 Gridview 的 fragment

android - Ionic 2 安卓性能

android - PropertyChangedEventHandler 导致内存泄漏

Android:带有自定义微调器的自定义适配器下拉 xml 布局给出错误

java - fragment 不显示在框架布局中

java - oData4j 示例 eclipse 项目

android - 一个应用中实例化了多少个LayoutInflater?