Android 使用setOnTouchListener在ViewGroup上添加imageView

Android 使用setOnTouchListener在ViewGroup上添加imageView,android,imageview,ontouchlistener,Android,Imageview,Ontouchlistener,编辑:此问题已解决 我面临着一个,希望是,简单的问题。我需要的是,当用户单击图像/区域时,在该位置上显示/添加图像。 这是布局图: activity_test_diff.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" a

编辑:此问题已解决

我面临着一个,希望是,简单的问题。我需要的是,当用户单击图像/区域时,在该位置上显示/添加图像。 这是布局图:

activity_test_diff.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestDiff"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <ImageView
        android:id="@+id/img_rectangle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:visibility="invisible"/>

    <ImageView
        android:id="@+id/img_test_diff1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter" />

    <ImageView
        android:id="@+id/click_check_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/check_green_48dp"/>

    <ImageView
        android:id="@+id/click_check_ko"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:src="@drawable/close_red_48dp"/>

</RelativeLayout>

<ImageView
    android:id="@+id/img_test_diff2"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"
    android:scaleType="fitCenter"/>
</LinearLayout>
这就是即将出现的错误:

Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
意味着有人能知道发生了什么!
感谢您提供的xml布局:

<RelativeLayout
    android:id="@+id/relative_layout"

RelativeLayout
layout\u relative
不在层次结构中。如果这是您的活动,请确保布局中存在
layout\u relative
。编辑问题,这只是我的错误,在发布时键入error这不是我要求的。我无法判断您发布的布局是否为实际的
activity\u test\u diff
或其他布局,1。因为你还没有把名字贴在上面,2。你说RelativeLayout是LinearLayout的孩子,但我没有看到任何LinearLayout围绕着
layout\u relative
。请参阅。@Abbas,问题已更新@justo此代码是否存在问题?
<RelativeLayout
    android:id="@+id/relative_layout"