This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
So I have This Code, now after i added a smiple action to the button add(setOnClickListener) when I run the app it immidiatley crashes...
any idea why?
public class MainActivity extends ActionBarActivity {
int counter;
Button add,sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button)findViewById(R.id.bAdd);
sub = (Button)findViewById(R.id.bSub);
display = (TextView)findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your Total is " + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
It's most likely that your line
add = (Button)findViewById(R.id.bAdd);
is returning null. Try verifying this by printing
Log.d("TEST", "is null: " + (add == null));
The next step is to ensure sure that Button with id "bAdd" is actually defined in your activity_main XML. Edit your question to add your XML file activity_main.
Related
I'm working on the android self study Sunshine app and I'm having trouble getting extra menu entries in the action bar.
The most likely reason I've found, is that the OnCreate method of my frame class is not being called and thus the menu is not being inflated.
Here's my MainActivity code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity","OnCreate Started 1");
if (savedInstanceState == null) {
Log.d("MainActivity","OnCreate Started 2");
ForecastFragment MyFragment = new ForecastFragment();
if (MyFragment == null){
Log.d("MainActivity","OnCreate Started 3");
}
else{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, MyFragment)
.commit();}
}
}
Here is now the code of my ForecastFragment class:
public class ForecastFragment extends Fragment {
public ForecastFragment() {
}
//#Override
protected void OnCreate(Bundle savedInstanceState){
Log.d("Forecastfragment","OnCreate Started");
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.forecastfragment, menu);
}
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_refresh) {
FetchWeatherTask WeatherTask = new FetchWeatherTask();
WeatherTask.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
When I run the app, I don't see the logging that the OnCreate method of the ForecastFragment class is being called.
Any ideas?
This is not the exact inherited onCreate method of Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate()");
}
it should be onCreate not OnCreate
When I try to run my app on the emulator I have a tab message saying
"unfortunately,GeoQuiz has stopped working" .......
GeoQuiz is my app's name .......
here is my activity class in the QuizActivity.java file
public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
mTrueButton=(Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
}
});
mFalseButton=(Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
#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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have no images at all in my app
and the min sdk version for this app is API 16:android 4.1 (jelly bean)
and the targeted version is API 21:android 5.0 (Lollipop)
you dont call setContentView at the right place:
you call it at the end of the onCreate method! You have to move these two lines to the beginning of the onCreate Method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
because of that you get a NullPointerException here:
mTrueButton=(Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() { // NPE
Hello I recently updated may ADT and and now I am developing new app using eclipse. so i want to know where to write setOnclickListner event. I want to open another activity on click button. I tried various combinations but it's gives me error every time. my eclipse Version: 8.1.2.201302132326 please tell me where i can write code??????
after adding setContentView(R.layout.activity_main); it gives an Error and application will unfortunately stooped.
I also remove some code and run it but it does not worked.
eg.
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
and
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
this code remove from coding but it's not worked.
and in that when i am creating new blank activity then there is fragment activity.xml will created automatically and I want make GUI changes in fragment activity.xml file. If I made changes in activity.xml then it gives error.
Please help me.
Thank you in advance..
here is may code which is automatically generated...
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
you should put the button fragment container,
so,you need to access your button you should specify like this because your placehold fragment inflate the layout of fragment_container.xml
Button b1=(Button)rootView.findViewById(R.id.button1);
b1.setOnclickListener(this);
in this case implements the onClickLiistener your class,Automatically onclick will override in your class
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final Activity contect=getActivity();
Button b1=(Button)rootView.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=new Intent(contect,YourActivity.class);
startActivity(in);
}
}) ;
return rootView;
}
}
}
put the button in fragment_main,move to the some other activity
Otherwise remove the fragment and extends the activity also remove the appcompat_v7 libraries from the code and project.
Remove the libraries Properties-->Android--> from below remove it
I know for a fact I'm missing something extremely small but i can not seem to get my code to compile without errors. I am attempting to get the text field to take in a decimal number and OnClick, it will spit out the numbers binary equivalent.
It keeps telling me to initialize Binary but if i set it to 0, that makes Binary '0' instead of converting the decimal number.
Any help will would great! This is is my MainActivity.java file:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
final EditText editDecimal = (EditText) findViewById(R.id.editDecimal);
final EditText editBinary = (EditText) findViewById(R.id.editBinary);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int decimal = Integer.valueOf(editDecimal.getText().toString());
int binary;
editBinary.setText(Integer.toBinaryString(binary));
}
});
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
You don't need the variable binary. Instead you just need to do this:
editBinary.setText(Integer.toBinaryString(decimal));
When trying to make an android app, I encountered the error:
No enclosing instance of the type MainActivity is accessible in scope on the line
Toast.makeText(MainActivity.this, "Swag", Toast.LENGTH_SHORT).show();
This is my MainActivity.java:
public class MainActivity extends ActionBarActivity {
static Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements View.OnClickListener {
public PlaceholderFragment() {
}
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Swag", Toast.LENGTH_SHORT).show();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
btn=(Button) rootView.findViewById(R.id.test_button);
btn.setOnClickListener(this);
return rootView;
}
}
}
You must use getActivity() inside the fragment.
Be careful, if you are doing some background job, or any other async task when it return, getActivity() may be null. So every time you use it check for null.
Further information on fragments, you can use this.