为什么我不能为RelativeLayout设置onTouchListener?

时间:2015-12-22 18:22:25

标签: android android-layout android-studio

我正在尝试创建一个基本的应用程序,您可以在其中触摸屏幕并更改屏幕的背景颜色。但是我无法为包含我的app essentials的RelativeLayout设置OnTouchListener。应用程序在构建时抛出错误然后崩溃,不知道这里发生了什么= /如果有人可以提供帮助那就太好了=)

Java代码:

public class ColorCrazeActivity extends AppCompatActivity{

int pointCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout1);
    myLayout.setOnTouchListener(
            new RelativeLayout.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent m) {
                    if (m.getActionMasked() == MotionEvent.ACTION_DOWN) {
                        changeColor();
                    }

                    return true;
                }
            }
    );

    setContentView(myLayout);
}

public void changeColor(){
    int myNum1 = (int)Math.floor(Math.random() * (255));
    int myNum2 = (int)Math.floor(Math.random() * (255));
    int myNum3 = (int)Math.floor(Math.random() * (255));

    TextView myTextView = (TextView)findViewById(R.id.myTextView);

    RelativeLayout myRelativeLayout = (RelativeLayout)findViewById(R.id.myRelativeLayout1);

    myTextView.setText("RGB(" + Integer.toString(myNum1) + ", " + Integer.toString(myNum2) + ", " + Integer.toString(myNum3) + ")");
    myRelativeLayout.setBackgroundColor(Color.argb(255, myNum1, myNum2, myNum3));
    addPoints();
}

public void addPoints(){
    pointCount += 1;

    TextView myTextView1 = (TextView)findViewById(R.id.myTextView1);
    myTextView1.setText(Integer.toString(pointCount));
}

}

XML代码:

<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/myRelativeLayout"
tools:context="com.toymakersdev.colorcraze.ColorCrazeActivity"
android:focusableInTouchMode="true"
android:background="#000c65">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/myRelativeLayout1"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myTextView1"
        android:text="Taps: 0"
        android:textSize="40dp"
        android:textColor="#ffffff"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myTextView"
        android:text="Tap here"
        android:textSize="50dp"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

我有一个RelativeLayout嵌套在另一个RelativeLayout中,因为我认为错误是因为它原来是根视图。显然没有考虑到它仍在抛出错误。

2 个答案:

答案 0 :(得分:2)

首先设置setContentView,然后再对其执行操作:

例如:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.id.myLayout); //name of the layout file
}

您的活动现在看起来像这样:

public class ColorCrazeActivity extends AppCompatActivity{

int pointCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.id.myLayout); //name of the layout file

    RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout1);
   myLayout.setOnTouchListener(
        new RelativeLayout.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent m) {
                if (m.getActionMasked() == MotionEvent.ACTION_DOWN) {
                    changeColor();
                }

                return true;
            }
        }
);


   }

public void changeColor(){
int myNum1 = (int)Math.floor(Math.random() * (255));
int myNum2 = (int)Math.floor(Math.random() * (255));
int myNum3 = (int)Math.floor(Math.random() * (255));

TextView myTextView = (TextView)findViewById(R.id.myTextView);

RelativeLayout myRelativeLayout = (RelativeLayout)findViewById(R.id.myRelativeLayout1);

myTextView.setText("RGB(" + Integer.toString(myNum1) + ", " + Integer.toString(myNum2) + ", " + Integer.toString(myNum3) + ")");
myRelativeLayout.setBackgroundColor(Color.argb(255, myNum1, myNum2, myNum3));
addPoints();
   }

public void addPoints(){
    pointCount += 1;

    TextView myTextView1 = (TextView)findViewById(R.id.myTextView1);
    myTextView1.setText(Integer.toString(pointCount));
}

答案 1 :(得分:2)

在访问布局

之前设置内容视图
setContentView(R.layout.activity_layout);//adjust this xml layout

RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout1);
myLayout.setOnTouchListener(
        new RelativeLayout.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent m) {
                if (m.getActionMasked() == MotionEvent.ACTION_DOWN) {
                    changeColor();
                }

                return true;
            }
        }
);

如果您不先执行此操作,您的活动就不知道在哪里可以找到该视图。