gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:22:27 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com