简体   繁体   中英

Why cant i set the onTouchListener for my RelativeLayout?

I am trying to make a basic app in which you touch the screen and it changes the background color of the screen. However I can't set the OnTouchListener for the RelativeLayout that is containing my app essentials. The app throws an error upon building and then crashes, not sure whats going on here =/ If anyone could help that would be great =)

Java Code:

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 Code:

<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>

I have a RelativeLayout nested inside of another RelativeLayout because i thought the error was because it was the root view originally. Apparently not considering its still throwing the error.

Set first the setContentView before do something with it:

For example:

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

Your activity looks now like this:

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));
}

Set the content view before accessing the layout

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;
            }
        }
);

If you don't do this first, your activity has no idea where to find the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM