NullPointerException at OnClickListener in Eclipse - android

package com.example.geoquiz;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends ActionBarActivity {
Button mTrueButton;
Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button)findViewById(R.id.button1);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
});
mFalseButton = (Button)findViewById(R.id.button2);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Please help guys. Getting a NullPointerException at mTrueButton.setOnClickListener(new View.OnClickListener().
I know other ways to set the OnClickListener but just wanted to know what's wrong with this code. Thanks in advance

You need to initialize your mTrueButton. Likely with a call to findViewById() after setContentView(), like you're initializing mFalseButton.

You forgot to initialize mTrueButton buttons:
mTrueButton= (Button)findViewById(R.id.buttonId);

You need to initialize mTrueButton like you did with mFalseButton
For eg.
mTrueButton = (Button)findViewById(R.id.button1); \\ button1 is whatever id you have given to mTrueButton
Until you do that mTrueButton is null and trying to call any method on it will result in a NullPointerException

Just initialize your mTrueButton like this:
mTrueButton = (Button)findViewById(R.id.button1);
before setting onClickListener

You forgot to initialize the buttons:
Button mTrueButton;
Button mFalseButton;
just add the findViewById() method to each button so you can access to them.
mTrueButton = (Button)findViewById(R.id.button1);

Related

How do I switch the if and if else when switching the text between buttons in android studio?

I am trying to switch the text between buttons like a puzzle game using the grid layout. I tried using an if statement but I feel I am doing it wrong. can any one give suggestions? Here is my java code
package com.example.kellito13.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.btn);
final Button btnOne=(Button)findViewById(R.id.btnOne);
final Button btnTwo=(Button)findViewById(R.id.btnTwo);
final Button btnFive=(Button)findViewById(R.id.btnFive);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(v == btnOne){
Button btn = (Button) v;
btn.setText("B");
btnOne.setText("A");
}
else if (v == btnFive){
btn.setText("E");
btnFive.setText("A");
}
/* Button btn = (Button) v;
btn.setText("B");
btnOne.setText("A");*/
}
});
btnOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Button btnOne = (Button) v;
btn.setText("A");
btnOne.setText("B");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Try this way
#Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btnOne:
btn.setText("B");
btnOne.setText("A");
break;
case R.id.btnFive:
btn.setText("E");
btnFive.setText("A");
break;
default :
}
}
package com.example.kellito13.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity Implement View.OnclickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.btn);
final Button btnOne=(Button)findViewById(R.id.btnOne);
final Button btnTwo=(Button)findViewById(R.id.btnTwo);
final Button btnFive=(Button)findViewById(R.id.btnFive);
//In oncreate method register the listener for all button
btn.setOnClickListener(this);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
btnFive.setOnClickListener(this);
}
//the following will be your original code....
#Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btn:
//do stuff
break;
case R.id.btnOne:
//do stuff
break;
case R.id.btnTwo:
//do stuff
break;
case R.id.btnFive:
//do stuff
break;
default :
//do stuff
}
}
}

Implementing GoogleAnalytics in Android App

I am implementing Google Analytics in Android App using eclipse. After lot of research i implemented it. Here is my Code:
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.StandardExceptionParser;
public class MainActivity extends Activity {
private EasyTracker easyTracker = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
easyTracker = EasyTracker.getInstance(MainActivity.this);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
easyTracker.send(MapBuilder.createEvent("your_action",
"envet_name", "button_name/id", null).build());
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
try {
int a[] = new int[2];
int num = a[4];
} catch (ArrayIndexOutOfBoundsException e) {
easyTracker.send(MapBuilder.createException(
new StandardExceptionParser(MainActivity.this, null)
.getDescription(Thread.currentThread().getName(), e), false).build());
}
}
});
}
#Override
public void onStart() {
super.onStart();
EasyTracker.getInstance(this).activityStart(this);
}
#Override
public void onStop() {
super.onStop();
EasyTracker.getInstance(this).activityStop(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The code compiled successfully but the problem which i am facing is that it is not sending any information in the GoogleAnalytics account.I have not received any part of information in google Analytics.

App crashes when Launching "Unfortunately, has stopped"

I have a program and when I try to start it, it crashes. I really don't understand why, even Eclipse show's that there are no errors.
I can show you the code of a page on which I think is the problem.
package ctect.android.maxipro;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class BasicScreenActivity extends Activity {
private Button butonul1;
private Button butonul2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_screen);
butonul1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
// TODO Auto-generated method stub
butonul2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(currentView.getContext(), NeedForSpeedActivity.class);
startActivityForResult(myIntent, 0);
Intent myIntent2 = new Intent(currentView.getContext(), Fifa2012Activity.class);
startActivityForResult(myIntent2, 0);
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.basic_screen, menu);
return true;
}
}
if somebody understands, please help me.
You forgot to assign an object to butonul1 before using it. You need to add this line before butonul1.setOnClickListener:
butonul1= (Button) findViewById(R.id.butonul1);
This is assuming that you gave it the id butonul1 in your layout file.

Syntax Error Insert ; and } to complete statement?

I have just started to learn how to program android apps, And im a COMPLETE NOOB.
I cant figure out how to fix this! please help! The format may look butchered... It's cause i have no clue what im doing!
package com.smiggle.bmxhandbook;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button button;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick1(View arg0) {
Intent myIntent = new Intent(MainActivity.this, Trick.class
MainActivity.this.startActivity(myIntent);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub; }
}
It keeps on giving me a syntax error!
Try this code instead:
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this, Trick.class);
MainActivity.this.startActivity(myIntent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Your class had a ridiculously high amount of syntax and logical errors:
Arbitrary onCreate1() method, which will never me called.
Arbitrary onClick() method, even though your Activity never implemented the interface
Your anonymous inner class' onClick() method was named onClick1()
The statement with the Intent was missing a ); at the end.
The inner class body was missing a closing });
The addListenerOnButton() method was missing a closing }
Your entire class was missing a closing }
I sincerely recommend you spend a few months (or even a year) learning Java before coming to Android.
You have this wide open
Intent myIntent = new Intent(MainActivity.this, Trick.class
You need to close that with a right parenthesis and and a semicolon.
Also that onClick should not have a 1 in it:
public void onClick1(View arg0) {

Seek bar progress when button pressed

I have made a simple android project with a single activity containing three buttons and a seek bar,Now i want is that when i press 1st button the seek bar should directly progressed 33%(without showing progress),when 2nd button pressed the seek bar should progressed again by 33% second time and when third button pressed the seek bar should full progressed..my code is as below:
Main.java
package com.example.seekabardemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends Activity {
Button btn,btn1,btn2;
SeekBar sk;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
btn1=(Button)findViewById(R.id.button2);
btn2=(Button)findViewById(R.id.button3);
sk=(SeekBar)findViewById(R.id.seekBar1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Thank you....Please help me..!
Please help me ..thank you..!
write this three statements in click events of each button respectively.
sk.setProgress(sk.getMax()/3);
sk.setProgress(sk.getMax()*2/3);
sk.setProgress(sk.getMax());
To set a seekBar progress just :
sk.setProgress(ProgressToSet);
to get the maximum possible progress just:
sk.getMax();
to get a porcentage of a seekbar just:
sk.setProgress(sk.getMax()/100*YourPercentage);

Categories

Resources