app closes during setText - android

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

Related

Unable to get the reference of CheckBox in android

I am using a CheckBox in my activity layout. But I don't know why I am not getting the refrence of the CheckBox. It shows NullPointerException everytime. I have used another checkbox in my dialog fragment but it was working fine. I don't know what is the cause of NullPointerException.
Here is xml code
<CheckBox
android:id="#+id/ccb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:text="Add Your Previous Amount\n with Current Amount "
android:textColor="#color/batteryChargedBlue"
android:textSize="13dp"
android:fontFamily="#font/caviardreams_bolditalic"
/>
Here is the java code where I am using the checkbox and getting NullPointerException.
public class StartActivity extends AppCompatActivity {
//Declaring the CheckBox variable globally
CheckBox scb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
//Initializing in the onCreate() method
scb = (CheckBox)findViewById(R.id.ccb);
...
}
//using it in the button clicked method to check whether checkbox is checked or not
public void setInitialMonthlyAmount(View view) {
if(scb.isChecked()) { //Getting The NullPointerException Here Don't know why ??
System.out.println("Checkbox is checked");
//Code
}else {
//Code
}
}
}
I think everything is correct. and I am using the same way to get the references of other views like TextViews, EditText etc and they are working fine. I don't what is wrong with this checkbox. Please Help!!!
Are you sure you coded Activity like this:?
public class StartActivity extends AppCompatActivity {
CheckBox scb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
scb = (CheckBox)findViewById(R.id.ccb);
setInitialMonthlyAmount(view)
}
public void setInitialMonthlyAmount(View view) {
if(scb.isChecked()) {
System.out.println("Checkbox is checked");
//Code
}else {
//Code
}
}
Make sure you are calling setContentView(R.layout.your_layout_with_checkbox); in your activitie's onCreate !

Refresh layout in android

I try to do some changes(change text, change positon ...) but it doesn't work.
For example in this function i'll change text of button correctly(i checked with getText function) but in view doesn't change anything.
When a button pressed, this function calls and change view. After this function, i want set view back.
setButton.setText("CHANGE RANGE");
setContentView(R.layout.main_layout);
I also have another layout with imageView in this main layout. I also want change that image's position.
Try this :
public class TestActivity extends Activity
{
Button setButton;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
setButton = (Button) findViewById(R.id.testButton);
setButton.setText("CHANGE RANGE");
}
}
setButton.setText("CHANGE RANGE"); should be called after setContentView(R.layout.main_layout);
Hope this helps.

Textview.text change example

I have 2 layouts. First layout has 2 textviews. And second layout has 1 button. I want to do when i pres buton textviews.text change. But i get null error about textviews.
textviews on musicactivity, button on homeactivity.
my first activity class --> homeactivity,secon activity class --> Musicactivity,textview-->ad1,textview-->ad2,textviews on musiclayouts,Button on homeactivity.
public class HomeActivity extends Activity {
Button btn;
TextView ad1;
TextView ad2;
private MusicActivity music;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
btn=(Button)findViewById(R.id.button1);
ad1=(TextView)findViewById(R.id.textView1);
ad2=(TextView)findViewById(R.id.textView2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
ad1.setText(null);
ad2.setText(null);
}
}
});
}
The error message you posted is for line 40, which is doing a close of v1. I don't know what a veritabani is, but it appears that error is happening there. I suspect the rest of the error trace might say more.
You are doing it wrong. You can't get the textViews if they are not in the layout. You say yourself they are not in the layout. You have to pass the values you want to set to the new activity (musicActivity) via an Intent. Then in musicActivity's you can get the textViews, you have the values, you set them.

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);
}
}

setContentView and Listeners

I'm changing my ContentView of the Activity at some point. (to View2).
After changing it back to View1, the listeners are not more working.
I already tried putting the Listener in the onResume() method.
Is it common anyway to use setContentView() to display e.g. a Progress screen/please wait,...(while an asyncTask is running).
Or should you only have ONE mainView for each Activity? (and replacing the content dynamically).
//EDIT: To be more specific:
I am looking for something like
LinearLayout item = (LinearLayout) findViewById(R.id.mainView);
View child = getLayoutInflater().inflate(R.layout.progress, null);
item.addView(child);
but instead of adding the "progress.xml", it should remove the current layout and ONLY show "progress.xml".
Do I need an "container" and show/hide mainView/progress?
But that doesn't seem very proper to me...
See also code below (stripped)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
}
});
}
setContentView(R.layout.view2);
[...]
setContentView(R.layout.view1);
//Listener not more working
Thank you all, for your reply. You made me realize, the onClickListeners are getting lost when I remove or replace (using setContentView()) the main view. I ended up this way now:
onCreate:
setContentView(R.layout.parse);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(getLayoutInflater().inflate(R.layout.dialog, null));
container.addView(getLayoutInflater().inflate(R.layout.progress, null));
onStartDoingSomething:
findViewById(R.id.dialog).setVisibility(View.INVISIBLE);
findViewById(R.id.progress).setVisibility(View.VISIBLE);
onEndDoingSomehting:
findViewById(R.id.dialog).setVisibility(View.VISIBLE);
findViewById(R.id.progress).setVisibility(View.INVISIBLE);
I might change View.INVISIBLE to View.GONE, like nmr said, but since I have never used View.GONE, I have to check the Android doku first ;)
Assuming you use 'findViewById' to initialize 'button', you would need to do that every time that you do setContentView(R.layout.view1);
You can only have one UI for each Activity, you should create another activity and in its onCreate method set the content view
Steps:
Create Activity
Use the following code to set the content View:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view2);
}
Define the Activity in the applications manifest as such :
Create an intent in the first Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view1);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
intent();
}
});
}
void intent(){
Intent intent = new Intent();intent.setClass(Activity1.this, Activity2.class);
startActivity(intent);
}
And there you go c:

Categories

Resources