简体   繁体   中英

TextView onClickListener not working properly

I am trying to call another layout when clicking a TextView on my MainActivity layout. I tried using the onClick() on the XML file but, due to the course purposes, it requires that everything is handled from the Java file. Following is the sample code given by the instructor that, in theory, should work, but it doesn't, followed by my very own code. When I click on the TextView to call on the other layout the program crashes.

My MainActivity.java

package com.wanli.jorgemorales.additionalviews;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        TextView numbers = (TextView) findViewById(R.id.numbers);
        TextView family = (TextView) findViewById(R.id.family);
        TextView colors = (TextView) findViewById(R.id.colors);
        TextView phrases = (TextView) findViewById(R.id.phrases);

        numbers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Numbers.class);
                startActivity(intent);
            }
        });


    }
}

You can set the click handler in xml with this attribute:

android:clickable="true"

Don't forget the clickable attribute, without it, the click handler isn't called.

main.xml

...

<TextView 
   android:id="@+id/numbers"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"               
   android:text="Click Me"
   android:textSize="55sp"                
   android:clickable="true"/> <!--Do not forget this attribute-->
...

MyActivity.java

// Find the View that shows the numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);

// Set a click listener on that View
numbers.setOnClickListener(new View.OnClickListener() {
   // The code in this method will be executed when the numbers View is clicked on.
   @Override
   public void onClick(View view) {
       Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
       startActivity(numbersIntent);
   }
});

Easiest way to set a onClickListener is with a Button .Buttons can also hold text. Change in your XML file the TextView numbers to a Button , and in the java code.

Example:

    Button numbers = (Button) findViewById(R.id.numbers); //Remember to change type in XML
    TextView family = (TextView) findViewById(R.id.family);
    TextView colors = (TextView) findViewById(R.id.colors);
    TextView phrases = (TextView) findViewById(R.id.phrases);

    numbers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Numbers.class);
            startActivity(intent);
        }
    });

Example of XML:

...
<Button
    android:id="@+id/numbers"
    ...
    android:text="Text"

/>
...

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