i am wondering how can i init Objects in my Activity correctly without getting NPE?
Normally I am initialising my Objects in the onCreate() method but when I do so I can't access them later in for example in an onClick() method.
What is the right way to do it?
Thanks Jérôme
public class MainActivity extends ActionBarActivity {
Button sendButton;
EditText textMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendButton = (Button) findViewById(R.id.button);
EditText textMessage = (EditText) findViewById(R.id.message);
}
public void sendMessage() {
setContentView(R.layout.activity_main);
String actualMessage = textMessage.getText().toString();
System.out.println(actualMessage);
}
Simply remove the types Button and EditText when you initialize them in onCreate. Having the type when you set it equal to a view declares a new variable, instead of referencing the class variables.
sendButton = (Button) findViewById(R.id.button);
textMessage = (EditText) findViewById(R.id.message);
Now you can use this control anywhere in this activity
Button sendButton;
EditText textMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendButton = (Button) findViewById(R.id.button);
textMessage = (EditText) findViewById(R.id.message);
}
Related
This is the simple code I applied.Please explain why such behavior is taking place.Oncreate is not updating then why on click of button it can be called again and again
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gaggan();
}
public void gaggan(){
Button bt =(Button) findViewById(R.id.button4);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText e2= (EditText) findViewById(R.id.editText);
EditText e3= (EditText) findViewById(R.id.editText3);
int num1=Integer.parseInt(e2.getText().toString());
int num2=Integer.parseInt(e3.getText().toString());
int sum=num1+num2;
Toast.makeText(MainActivity.this,Integer.toString(sum), Toast.LENGTH_SHORT).show();
}
bt.setOnClickListener will register your click-listener (new View.OnClickListener() {..} on the button and thus, whenever the button is clicked, the onClick() method of your click-listener is executed.
The Android docs have a more detailed explanation
I have been coding a simple android Java program, so far I included an ImageView, button and an EditText in the activity_main.xml. However, when I run it, with this MainActivity java class:
public class MainActivity extends Activity {
private Button start = (Button) findViewById(R.id.start);
private EditText nameField = (EditText) findViewById(R.id.nameField);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = nameField.getText().toString();
}
});
}
is not working and it returns a fatal error I cannot resolve. It says:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo, java.lang.NullPointerException
How can I fix it? Thanks a lot!
You should move these initializations to onCreate :
private Button start = (Button) findViewById(R.id.start);
private EditText nameField = (EditText) findViewById(R.id.nameField);
i.e :
private Button start;
private EditText nameField;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
nameField = (EditText) findViewById(R.id.nameField);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = nameField.getText().toString();
}
});
}
When you call findViewById(R.id.start) in the member initialization expression, the Activity instance is not initialized yet, which causes the NullPointerException.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
button = (Button) findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView3);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
input1 = editTextView1.getText().toString();
input2 = editTextView2.getText().toString();
First initialize the editText just like you initialize the Button and TextView then you are able to get the text from editText
you must have initialize.
(EditText, input1 and input2).
in Activity :
EditText editTextView1;
EditText editTextView2;
String input1;
String input2;
in onCreate :
editTextView1 = (EditText) findViewById(yourEditText1_Id);
editTextView2 = (EditText) findViewById(yourEditText2_Id);
Here is a little code that might help point you in the right direction. Post your code and I'll make the code fit your needs.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
TextView textView = (TextView)findViewById(R.id.textView3);
EditText editText1 = (EditText) findViewById(R.id.editTextView1);
EditText editText2 = (EditText) findViewById(R.id.editTextView2);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
input1 = editText1.getText().toString();
input2 = editText2.getText().toString();
break;
}
}
Just going to make some of the answers above a bit clearer. An Activity is like a Class in a regular Java program. That means that it has fields (variables) and methods (functions). You can't access a field from within a method unless you declare in within the body of the class, like this:
public class MainActivity extends Activity implements OnClickListener{
EditText input1;
protected void onCreate(Bundle savedInstanceState){
// normal onCreate stuff
input1 = (EditText) findViewById(R.id.input1);
// this should be the id in your layout.xml file
}
public String getText(){
String s = input1.getText().toString().trim();
return s;
}
}
Note the use of the trim() method, which will remove any extra whitespace the user may have added at the end of their input.
Displaying two random numbers and counting scores for odd and even turns. When one of it reaches to 100, the application stops.
This is my java file of application
public class Board_Play1 extends Activity {
int d=0,a=0,b=0,turn=2;
Random random = new Random();
EditText diceno = (EditText) findViewById(R.id.editText1);
EditText p1 = (EditText) findViewById(R.id.editText2);
EditText p2 = (EditText) findViewById(R.id.editText3);
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.board_play1);
diceno.setText(String.valueOf(d));
p1.setText(String.valueOf(a));
p2.setText(String.valueOf(b));
while(a!=100 && b!=100)
{
if(turn%2==0)
{
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
d=random.nextInt(6)+1;
EditText diceno = (EditText) findViewById(R.id.editText1);
diceno.setText(String.valueOf(d));
}
});
}
else
{
d=random.nextInt(6)+1;
diceno.setText(String.valueOf(d));
}
if(turn%2==0)
a+=d;
else
b+=d;
if(a>100)
a-=d;
if(b>100)
b-=d;
p1.setText(String.valueOf(a));
p2.setText(String.valueOf(b));
turn++;
}
a=0;b=0;
}
}
It doesn't open and give an error saying Unfortunately your app has stopped. Why is this happening? What can I change?
Move all this inside onCreate after setContentView
EditText diceno;
EditText p1;
EditText p2 ;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.board_play1);
diceno = (EditText) findViewById(R.id.editText1);
p1 = (EditText) findViewById(R.id.editText2);
p2 = (EditText) findViewById(R.id.editText3);
findViewById looks for a view in the current view hierarchy. You ned to set the content of the layout to the activity first and then initialize vies by using findViewById.
Also there is no need to re- initialize editText in button onClick. Get rid of the below in the same
EditText diceno = (EditText) findViewById(R.id.editText1);
Also move this
Button button = (Button) findViewById(R.id.button1);
before the while loop. no need to initialize everytime
I have 2 methods which use same 2 buttons + 1 TextView. Something like this:
public void startChange(View v) {
final Button start = (Button) findViewById(R.id.start);
final Button restart = (Button) findViewById(R.id.restart);
final TextView check = (TextView) findViewById(R.id.check);
//TO-DO part - Sets check to something based on buttons TagID
}
public void restartChange (View v) {
final Button start = (Button) findViewById(R.id.start);
final Button restart = (Button) findViewById(R.id.restart);
final TextView check = (TextView) findViewById(R.id.check);
//TO-DO part - Restars everything to basic position
}
everything worked fine.. But as soon as I made this buttons and textview global variable I got java.lang.NullPointerException.
Any idea how to fix it?
Edit:
Problem solved.
Only think you have to do is to define buttons and textview in onCreate method not in global definition..
Example:
public class Example extends Activity {
Button start;
Button restart;
TextView t;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
start = (Button) findViewById(R.id.start);
restart = (Button) findViewById(R.id.restart);
check = (TextView) findViewById(R.id.check);
}
}
You must add setContentView(R.layout.main);
public class Example extends Activity
{
Button start;
Button restart;
TextView t;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button) findViewById(R.id.start);
restart = (Button) findViewById(R.id.restart);
check = (TextView) findViewById(R.id.check);
}
}
Problem solved.
Only think you have to do is to define buttons and textview in onCreate method not in global definition..
Example:
public class Example extends Activity {
Button start;
Button restart;
TextView t;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
start = (Button) findViewById(R.id.start);
restart = (Button) findViewById(R.id.restart);
check = (TextView) findViewById(R.id.check);
}
}