我正在开发一个应用程序,用户必须将一个对象(例如塑料瓶,罐头,易碎包装)拖到正确的垃圾箱(垃圾箱,回收箱)中。我正在尝试这样做,以便一旦用户将对象拖到正确的容器中,下一个对象就会在同一图像视图中弹出,并告诉用户是否选择了正确的容器。
在我看来,我已经正确地完成了所有操作,但是只要将对象拖到其中一个垃圾箱,该应用程序就会崩溃。我在做什么错了?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageView question, option1, option2, option3;
private Map<Integer, Integer> drawableValues;
private int currentIndex = 0;
private int[] textureArrayWin = {
R.drawable.image_0,
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3,
R.drawable.image_4,
R.drawable.image_5,
R.drawable.image_6,
R.drawable.image_7,
R.drawable.image_8,
R.drawable.image_9,
R.drawable.image_10,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawableValues = new HashMap<>();
drawableValues.put(R.drawable.image_0, 1);
drawableValues.put(R.drawable.image_1, 2);
drawableValues.put(R.drawable.image_2, 3);
drawableValues.put(R.drawable.image_3, 2);
drawableValues.put(R.drawable.image_4, 3);
drawableValues.put(R.drawable.image_5, 3);
drawableValues.put(R.drawable.image_6, 1);
drawableValues.put(R.drawable.image_7, 1);
drawableValues.put(R.drawable.image_8, 1);
drawableValues.put(R.drawable.image_9, 1);
drawableValues.put(R.drawable.image_10, 1);
question = (ImageView) findViewById(R.id.qImageView);
question.setImageDrawable(getDrawable(textureArrayWin[currentIndex]));
option1 = (ImageView) findViewById(R.id.gWBinImageView);
option2 = (ImageView) findViewById(R.id.rBImageView);
option3 = (ImageView) findViewById(R.id.oWImageView);
option1.setOnDragListener(dragListener);
option2.setOnDragListener(dragListener);
option3.setOnDragListener(dragListener);
question.setOnLongClickListener(longClickListener);
}
View.OnLongClickListener longClickListener = new View.OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder myShadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, myShadowBuilder, v, 0);
return true;
}
};
View.OnDragListener dragListener = new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
int dragEvent = event.getAction();
switch (dragEvent){
case DragEvent.ACTION_DROP:
final View view = (View) event.getLocalState();
//view.setVisibility(View.INVISIBLE);
ImageView dropTarget = (ImageView) v;
ImageView dropped = (ImageView) view;
int answer = drawableValues.get(((ImageView) dropped).getDrawable());
int tagDropTarget = Integer.parseInt((String)dropTarget.getTag());
if (answer == tagDropTarget ){
Toast.makeText(MainActivity.this,R.string.correctMessage, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this,R.string.incorrectMessage, Toast.LENGTH_SHORT).show();
}
currentIndex = (currentIndex + 1);
Drawable imageQuestion = getResources().getDrawable(textureArrayWin[currentIndex]);
question.setImageDrawable(imageQuestion);
break;
}
return true;
}
};
}
activity_main.xml(相关部分)
<ImageView
android:tag="1"
android:id="@+id/gWBinImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/general_waste" />
<ImageView
android:tag="2"
android:id="@+id/rBImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/recycle_bin" />
<ImageView
android:tag="3"
android:id="@+id/oWImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/food_waste" />
</LinearLayout>
错误日志
2019-06-12 23:26:14.261 12092-12092/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 12092
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at com.example.myapplication.MainActivity$2.onDrag(MainActivity.java:93)
at android.view.View.callDragEventHandler(View.java:26134)
at android.view.View.dispatchDragEvent(View.java:26125)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1784)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1784)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1784)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1784)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1784)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1784)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1784)
at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:6856)
at android.view.ViewRootImpl.access$1200(ViewRootImpl.java:146)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4719)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7116)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)