Being new to Android, I am attempting to identify which button is being pressed by outputting "Hello" on that button, but I'm experiencing an error when executing the code.
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1 :
button1.setText("Hello");
break;
case R.id.button2 :
button2.setText("Hello1");
break;
default:
throw new RuntimeException("Unknown button ID");
}
}
}
The above code returns the following error:
java.lang.RuntimeException: Unable to instantiate activity ...
The problem are your class attributes:
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
You can't call findViewById before calling setContentView in onCreate method. Change your code:
Button button1;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
Make findViewById inside onCreate:
Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
you have to instanciate the button inside onCreate methode like that :
Button button1 ;
Button button2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
the problem was on binding button with it's proper viewId , so these viewIds are inside activity_main layout , so findViewById will search inside that layout for these viewIds and all this will be done inside onCreate method
Related
IM GETTING THIS ERROR ON THIS LINE
Button button1 = (Button) findViewById(R.id.alarm);
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.user.appidea/com.example.user.appidea.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
public class MainActivity extends AppCompatActivity {
private Button button;
**Button button1 = (Button) findViewById(R.id.alarm);**
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
INSERT CODE HERE
}
});
}
}
try this working example
final MediaPlayer mpRingtone = MediaPlayer.create(this, R.raw.musicfile);
mpRingtone.start();
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Like in subject. I got null pointer, how it is possible ?
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
You just forgot to do findViewById to your Controls in OnCreate()
button2 =(Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.btnID);
textView2 =(textView) findViewById(R.id.textView2);
Sample code
#Override
protected void onCreate( Bundle savedInstanceState ){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.mybtnID);
button2 = (Button) findViewById(R.id.button2);
textView2 = (textView) findViewById(R.id.textView2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick( View view ){
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick( View view ){
size = list.size();
textView2.setText(size);
}
});
}
You are missing
findViewById for all your widget
i.e.
Button button, button2;
TextView textView, textView2;
button = findViewById(R.id.id_of_button_1); // same for button2,textview,textview2
You can initialize with findById(R.id.myBtn) like everyone is saying. Or programatically like button = new Button(this);
Try this
You forgot to add findViewById
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button2=(Button)findViewById(R.id.button2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
Try this are missing findViewById for all your widget
button = (Button) findViewById(R.id.btnID);
button2 =(Button) findViewById(R.id.button2);
textView = =(textView) findViewById(R.id.textView);
textView2 =(textView) findViewById(R.id.textView2);
Sample Code
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btnID);
button2 =(Button) findViewById(R.id.button2);
textView = =(textView) findViewById(R.id.textView);
textView2 =(textView) findViewById(R.id.textView2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
I want to create a simple calculator in Android, in my way I get an error on calling a fuction that i have already declared. Any Android Expert here that can help me?
public class CalculatorActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
}
//Start my method
public void calculator(View v) {
Button btn0 = (Button) findViewById(R.id.btn0);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
Button btn5 = (Button) findViewById(R.id.btn5);
Button btn6 = (Button) findViewById(R.id.btn6);
Button btn7 = (Button) findViewById(R.id.btn7);
TextView textViewResult = (TextView) findViewById(R.id.textView);
if (btn0.isPressed() == true) {
textViewResult.setText(textViewResult + "0");
} else if (btn1.isPressed() == true) {
textViewResult.setText(textViewResult + "1");
}
}
//End my method
//I want to call that fucntion, and i get an error
calculator();
}
You should call that method inside OnCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
calculator();
}
Use Below Code:
public class CalculatorActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
//I want to call that fucntion, and i get an error
calculator();
}
//Start my method
public void calculator() {
Button btn0 = (Button) findViewById(R.id.btn0);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
Button btn5 = (Button) findViewById(R.id.btn5);
Button btn6 = (Button) findViewById(R.id.btn6);
Button btn7 = (Button) findViewById(R.id.btn7);
TextView textViewResult = (TextView) findViewById(R.id.textView);
if (btn0.isPressed() == true) {
textViewResult.setText(textViewResult + "0");
} else if (btn1.isPressed() == true) {
textViewResult.setText(textViewResult + "1");
}
}
//End my method
}
call calculator() method inside your onCreate() method
Try this
public class CalculatorActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
calculator();
}
//Start my method
public void calculator() {
Button btn0 = (Button) findViewById(R.id.btn0);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
Button btn5 = (Button) findViewById(R.id.btn5);
Button btn6 = (Button) findViewById(R.id.btn6);
Button btn7 = (Button) findViewById(R.id.btn7);
TextView textViewResult = (TextView) findViewById(R.id.textView);
if (btn0.isPressed() == true) {
textViewResult.setText(textViewResult + "0");
} else if (btn1.isPressed() == true) {
textViewResult.setText(textViewResult + "1");
}
}
//End my method
}
Call calculator(); inside your onCreate()
public class CalculatorActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
calculator();
}
//Start my method
public void calculator(View v) {
Button btn0 = (Button) findViewById(R.id.btn0);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
Button btn5 = (Button) findViewById(R.id.btn5);
Button btn6 = (Button) findViewById(R.id.btn6);
Button btn7 = (Button) findViewById(R.id.btn7);
TextView textViewResult = (TextView) findViewById(R.id.textView);
if (btn0.isPressed() == true) {
textViewResult.setText(textViewResult + "0");
} else if (btn1.isPressed() == true) {
textViewResult.setText(textViewResult + "1");
}
}
}
Now it's going to working charm
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText textmsg;
static final int READ_BLOCK_SIZE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textmsg = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
noteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Notes.class);
}
});
resuBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Results.class);
}
});
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
}
});
When I run the application the buttons don't work. If I set the buttons as so..
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
Button resuBtn= (Button) findViewById(R.id.resuBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
If I use this code above, the code works fine and the buttons work correctly. But other functionality with different classes/activities won't run. Could someone please show me a solution or explain how to solve this issue.
You should place:
this.noteBtn = (Button) findViewById(R.id.noteBtn);
this.notBtn.setOnClickListener(this);
this.resuBtn = (Button) findViewById(R.id.resuBtn);
this.resuBtn.setOnClickListener(this);
this.timeBtn = (Button) findViewById(R.id.timeBtn);
this.timeBtn.setOnClickListener(this);
in onCreate() instead of onClick(). Make the buttons belong to the class, so you can reference them in onClick(). You should not be setting new onClickListeners in onClick(), but should rather have a switch statement based on the clicked view's id to determine which button was pressed.
this code
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button timeBtn = (Button) findViewById(R.id.timeBtn);
...
}
is not working because it's never going to get call, since none of your buttons is implementing it, worse yet they have not even been initialized because the onClick has not and will never be called.
the correct way to do it is like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but1 = (Button) findViewById(R.id.but1);
Button resBtn = (Button) findViewById(R.id.resBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agendaBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View viewTimeTable) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
because you are first getting a reference to your buttons and assigning them the onClickListener to each one of it.
I will suggest reading more about the Android Activity life cycle you can find it
here
I want to disable button B when making a treatement in button A.
public class GoJump extends Activity{
Button answerA, answerB;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jump);
answerA = (Button) findViewById(R.id.button_A);
answerB = (Button) findViewById(R.id.button_B);
answerA.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// Do something
// Disable button B
}
});
The problem is button B is not visible inside button A treatement. I have to declare it another time to disable it.
Is there any other method to do? Make a variable visible in all class.
Thank you.
Initialize your variable before onCreate().
// Before onCreate
private Button b, b2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
b2.setEnabled(false);
}
});
{
You just need to setEnabled to false. Read this javadoc.
public void onClick(View v)
{
answerB.setEnabled(false);
}