Android: app is crashing - android

This is my code :
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView editTextDay = (TextView)findViewById(R.id.editTextDay);
editTextDay.setVisibility(8); //GONE
setContentView(R.layout.main);
}
without the line " editTextDay.setVisibility(8); " the app doesn't crash.
any idea on what's wrong ?

you should set the content layout before any call of the method : findViewById() ;that's why it return null . So your code will work like this :
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView editTextDay = (TextView)findViewById(R.id.editTextDay);
editTextDay.setVisibility(8); //GONE
}

Move this line setContentView(R.layout.main); after super.onCreate(savedInstanceState);
.Currently your textview is null so it is throwing exception. You first have to set view and then use findViewById method.

Put setContentView right under the super call.
You can't access views before setting the content view:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView editTextDay = (TextView)findViewById(R.id.editTextDay);
editTextDay.setVisibility(View.GONE); //GONE
}

Related

How should we set widgets values in Android?

I was looking at my code and I realized that there are at least 3 ways of getting widget's reference in code:
First one (before onCreate):
private TextView textView= (TextView) findViewById(R.id.textView);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
Second one (in onCreate):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
final TextView textView= (TextView) findViewById(R.id.textView);
}
Third one (creating out and setting in onCreate):
private TextView textView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
textView= (TextView) findViewById(R.id.textView);
}
What is the difference between this 3 methods? When should I use them?
You must call setContentView() prior calling findViewById(), therefore on 1st approach will give you null all the times. 2nd and 3rd are the same with the exception for final keyword, but this is Java feature, not Android's.
The first does not guarantee that your widget is actually instantiated, it is not within the onCreate.
Second will be instantiated, but its value can not be changed because it becomes a constant to be the final.
Third, it is a global variable that will be instantiated in onCreate and you can use it in any other part of your code.
If you need to call findViewById() then the call should be anywhere after setContentView. Not before that like in your first option. Your third option creates an instance variable, use it only if the textview will be accessed a lot throughout the class, otherwise just call findViewById wherever you need it.

Nullpointerexception when trying to read textfield - Android

I have a button with a method that is invoked upon clicking.
The method:
public void addToList(View view) {
System.out.println(1);
String str = "";
try{
str = edit.getText().toString();}
catch (Exception ex){
System.out.println( ex );
}
System.out.println(2);
new QueryInList( ).execute(helper, str);
System.out.println(3);
edit.setText(null);
System.out.println(4);
//adapter.notifyDataSetChanged();
}
Well, I always get the exception, it is a Nullpointerexception.
This quite baffles me, because edit IS initalized:
It is declared in the class:
private EditText edit;
and besides, it is initialized in onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
edit = (EditText)findViewById(R.id.textfield);
setContentView(R.layout.activity_view);
......}
So I wonder why I always get a Nullpointer?
Set the content view, before looking for the items. You dont have a view to find the items in until you set the content view.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
edit = (EditText)findViewById(R.id.textfield);
......}
Move edit = (EditText)findViewById(R.id.textfield); after your setContentView statement.
Here is a nice explanation from user #Squonk from another question:
setContentView(...) perfoms something called 'layout inflation'. What that means is it parses the XML in the relevant file (main.xml in your case) and creates instances of all the UI elements within it. It then attaches that view to the Activity. When you call findViewById(...) it doesn't reference your main.xml directly - instead it references the content view attached to the Activity, in other words the one inflated by setContentView(...)

app closes during setText

I have a checkedbox that when, onClick (checked/unchecked), would setText to an activity.
When I run the application, it stopped and will return to the previous page.
What is wrong with my code?
My OrderActivity.java has:
public class OrderActivity extends ActionBarActivity {
CheckBox OrderMenuBiggDeal, OrderMenuCrispyChicken, OrderMenuExtremeBurger, OrderMenuTenderloinTips;
TextView ReceiptTextMenuBiggDeal, ReceiptTextMenuCrispyChicken, ReceiptTextMenuExtremeBurger, ReceiptTextMenuTenderloinTips;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
OrderMenuBiggDeal = (CheckBox) findViewById(R.id.checkBoxMenu1);
ReceiptTextMenuBiggDeal = (TextView) findViewById(R.id.textViewReceiptMenuPrice1);
and
public void onClickBiggDeal(View view){
if(OrderMenuBiggDeal.isChecked()){
ReceiptTextMenuBiggDeal.setText("" + "hello");
}
else{
ReceiptTextMenuBiggDeal.setText(R.string.default_value);
}
the application closes on the ReceiptTextMenuBiggDeal.setText("" + "hello"); line.
The setContentView is activity_order.xml.
The location of the TextView that I want to setText (ReceiptTextMenuBiggDeal) is on a different xml file, the activity_receipt.xml
You should change this
setContentView(R.layout.activity_order);
to
setContentView(R.layout.activity_receipt);
It's because your TextView with id textViewReceiptMenuPrice1 belong to activity_receipt layout and you trying to find it on activity_order layout.
You cannot set text on textview that is in other xml, just in one that is set in setcontentview

Draw 50 button dynamically after full activity loaded

This is on create of my activity and after setting content view , I am calling LoadButton().
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Loadbuttons();
}
public void LoadButtonss()
{
Here I am fetching data from database ,and plotting 50 buttons dynamically
}
So My Problem is : It is taking time in loading activity.
Any Idea how to LoadButtons() after Loading full activity. It should not wait for the ButtonLoad() function.
Any Idea how to do that?
paste this code in your activity class block:
#Override
protected void onStart() {
// TODO Auto-generated method stub
LoadButtons();
super.onStart();
}
According to the activity life cycle, you must call LoadButtons in onStart overrided method. Because activity will be visible in this state.
You could set the buttons to invisible and if you fetch the data you can update the UI through an AsyncThread. It is not the nicest way, but it works. Otherwise use instead of Buttons a CustomListView.
I would try this: Load buttons in onCreate and set View invisible. in onResume make it visible again.
View v;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v= findViewById(R.layout.main);
v.setVisibility(View.INVISIBLE);
Loadbuttons();
}
#Override
protected void onResume() {
v.setVisibility(View.VISIBLE);
super.onResume();
}
public void LoadButtons()
{
FrameLayout layout = (FrameLayout) findViewById(R.id.MarkerLinearlayout);
//Here I am fetching data from database ,and plotting 50 buttons dynamically
}

Android: How can I reference layout items outside the Main Activity?

My onCreate method sets the content view and sets up an image button:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton mGetClickTime = (ImageButton) findViewById(R.id.clicker);
}
But I want to create a method that changes the background of the ImageButton:
public void mUpdateBackground() {
if (backgroundPic) {
randomImageId = R.drawable.bg1;
}
else {
randomImageId = R.drawable.bg0;
}
mGetClickTime.setImageResource(randomImageId);
}
The problem is, the mUpdateBackground method doesn't know about the layout. If I declare the layout in the method it resets all the changes made programatically in the MainActivity.
Any ideas how I can solve this problem?
the mUpdateBackground method doesn't know about the layout
I am going to translate that to mean:
the mUpdateBackground method doesn't know about the ImageButton
If that is correct, make ImageButton mGetClickTime be a data member of your activity, instead of a local variable within onCreate(), and have mUpdateBackground() be a method on that same activity.
You need to move the ImageButton declaration to your Activity scope instead of your onCreate() method scope. Try like this:
public class YourActivity extends Activity{
ImageButton mGetClickTime;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGetClickTime = (ImageButton) findViewById(R.id.clicker);
}
public void mUpdateBackground() {
if (backgroundPic) {
randomImageId = R.drawable.bg1;
}
else {
randomImageId = R.drawable.bg0;
}
mGetClickTime.setImageResource(randomImageId);
}
}

Categories

Resources