在 onCreate() 方法之前命名视图时出错 运行
Error when Views are named before the onCreate() method has run
运行使用此代码时,应用程序会立即崩溃。
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()
方法有 运行 之前命名了两个视图。我可以在 onCreate()
方法和以下侦听器中命名这些视图,但视图不能是 public,因此它们将彼此独立,这将阻止我的应用程序按预期工作。
有人知道如何在不使这些视图彼此独立的情况下防止这个问题吗?
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 方法之后绑定视图和访问方法。
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;
}
}
}
试试这个
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;
}
}
}
试试这个
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)
;
运行使用此代码时,应用程序会立即崩溃。
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()
方法有 运行 之前命名了两个视图。我可以在 onCreate()
方法和以下侦听器中命名这些视图,但视图不能是 public,因此它们将彼此独立,这将阻止我的应用程序按预期工作。
有人知道如何在不使这些视图彼此独立的情况下防止这个问题吗?
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 方法之后绑定视图和访问方法。
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;
}
}
}
试试这个
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;
}
}
}
试试这个
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)
;