【问题标题】:Error when Views are named before the onCreate() method has run [duplicate]在 onCreate() 方法运行之前命名视图时出错 [重复]
【发布时间】:2018-01-17 12:23:50
【问题描述】:

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

错误:java.lang.RuntimeException:无法实例化活动 ComponentInfo{(...).MainActivity}: java.lang.NullPointerException: 尝试调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()' 在空对象引用上

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

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

【问题讨论】:

  • 你应该在布局膨胀后进行初始化。
  • 1) 如果您在 onCreate 中创建它们,则没有理由不能将它们公开。 2)无论如何,您都不应该拥有 Activity 类的任何公共成员。如果您需要访问其他类中活动的视图,则应传入它们。您不应传递对 Activity 的引用来访问其数据成员。
  • @GabeSechan 1) 此处不允许使用修饰符 'public' 2) 你如何将它们传入?
  • 您似乎将创建与声明混淆了。您可以声明一个变量,但还不能创建它的值。至于数字 2- 作为构造函数或实际需要它们的函数的参数
  • @GabeSechan 啊,非常感谢!

标签: java android layout-inflater oncreate


【解决方案1】:

您只能在 main 方法内或 main 方法之后绑定视图和访问方法。

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

}

【讨论】:

    【解决方案2】:

    试试这个

     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)
    

    ;

    【讨论】:

      【解决方案3】:

      试试这个

      public class MainActivity extends AppCompatActivity {
          public ConstraintLayout constraintLayout ;
          LayoutInflater inflater = getLayoutInflater();
          public View rectimage3;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              final ImageView rectimage = (ImageView) findViewById(R.id.rectimage);
              inflater = getLayoutInflater();
              rectimage3 = inflater.inflate(R.layout.my_rectview, 
              constraintLayout, 
             false)
             ; //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;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        • 1970-01-01
        • 2018-12-12
        • 1970-01-01
        相关资源
        最近更新 更多