Thomas Kim
Thomas Kim

Reputation: 513

android studio, button object not recognized,

After importing View, and Button. I have created an object for Button, in this case "thomasButton".

But error stated the field 'thomasButton" is not used even though I have called it at the next line.

After awhile, I found that the field can be recognized if I put it in another scope. like this, but still the program wont run (crash when started). Do you guys know what is the correct way to setOnLongClickListener for a button?

package com.example.thoma.event;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void displayMessage(View v){
    TextView thomasText = (TextView)findViewById(R.id.txt_View);
    thomasText.setText(R.string.rsc_Text2);x
}

Button thomasButton = (Button)findViewById(R.id.btn_Change);
// weird but I can only use Button object within an inner scope
{
    thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            TextView thomasText = (TextView) findViewById(R.id.txt_View);
            thomasText.setText("Artificial");
            // it will return false if long click wasnt long enough
            // and normal click will be called
            return true;
        }
    });
}
}

Crash: Unfortunately, App has stopped

Upvotes: 1

Views: 1922

Answers (3)

Omkar
Omkar

Reputation: 3100

button initialize in onCreate() check below code. findViewById is costly so it will be better if you find those ids as minimum as possible

package com.example.thoma.event;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     Button thomasButton = (Button)findViewById(R.id.btn_Change);

     TextView thomasText = (TextView) findViewById(R.id.txt_View);

     thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            thomasText.setText("Artificial");

            // it will return false if long click wasnt long enough
            // and normal click will be called

            return true;
        }
    });

}

public void displayMessage(View v){
    TextView thomasText = (TextView)findViewById(R.id.txt_View);
    thomasText.setText(R.string.rsc_Text2);
}


}

Upvotes: 2

Saurav Prakash
Saurav Prakash

Reputation: 654

package com.example.thoma.event;

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

public class MainActivity extends AppCompatActivity {
TextView thomasText;
Button thomasButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    thomasText = (TextView)findViewById(R.id.txt_View);
      thomasButton = (Button)findViewById(R.id.btn_Change);
      thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
             thomasText.setText("Artificial");
            // it will return false if long click wasnt long enough
            // and normal click will be called
            return true;
        }
    });

}

public void displayMessage(View v){
    thomasText.setText(R.string.rsc_Text2);
}



}

Upvotes: 1

Ramesh sambu
Ramesh sambu

Reputation: 3539

Try this

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

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         Button thomasButton = (Button)findViewById(R.id.btn_Change);
         thomasButton.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    TextView thomasText = (TextView) findViewById(R.id.txt_View);
                    thomasText.setText("Artificial");
                    return false;
                }
            });
    }


    }

Upvotes: 0

Related Questions