So I have a separate layout file for a navigation, and I want to edit it on a button click. But when I do so, I get the error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
The code that I use to define it is:
private TextView tvData;
private TextView gettingPrices;
private TextView navTime;
private TextView title;
navTime = (TextView) findViewById(R.id.refreshPricesNav);
tvData = (TextView) findViewById(R.id.test);
gettingPrices = (TextView) findViewById(R.id.gettingPrices);
title = (TextView) findViewById(R.id.navtitle);
and the code that executes is here:
title.setText("Last Checked" + Datetime());
Fixed! Added navTime = (TextView) findViewById(R.id.refreshPricesNav); to onPostExecute
your R.id.navtitle TextView is not available in current layout file.
Create method in class where this TextView is available
make TextView static so that you can access it from other class also.
You should move all the below code
navTime = (TextView) findViewById(R.id.refreshPricesNav);
tvData = (TextView) findViewById(R.id.test);
gettingPrices = (TextView) findViewById(R.id.gettingPrices);
title = (TextView) findViewById(R.id.navtitle);
to onCreate method of that activity. Don't remember to set the view before pasting the above code. You should set the content view as
setContentView(R.layout.filename);
Replace the filename based on your layout name.
Basically what happens where you are calling this line of code
title = (TextView) findViewById(R.id.navtitle);
here, the method findViewById() returns null, so the title is pointing to no where, means title has no object reference, this happens because you have nor declared TextView in your current layout file by the id of navtitle, so you must have declared this in your current layout or you can use the include http://developer.android.com/training/improving-layouts/reusing-layouts.html tag if you are declaring in some other layout.
Related
How can I find those TextView's another function on same class?
I will use setText(), serBackgroundColor() after create.
This code part is on CreateDesign() and this func calling onCreate():
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private TextView textView;
public void CreateDesign(){
linearLayout = (LinearLayout) findById(R.id.linearLayout);
for(int i = 1; i <= 5; i++){
textView = new TextView(this);
textView.setId(i);
textView.setText(i + ". TextView");
linearLayout.addView(textView);
}
}
Well you don't necessarily need to use id here, There are several ways to achieve this:
1.
TextView textView = (TextView) linearLayout.findViewById(i);
i is what you set before from 1 to 5.
2.
TextView textView = (TextView) linearLayout.getChildAt(i);
i here is the number of set item, for instance i=0 is the first textView you added using addView() method.
Either you create a member variable of this TextView which you can then use inside this class or you can use findViewById() on your LinearLayout.
Use the normal findViewById() method. You're giving the TextViews unique IDs from 1 to 5, so you can find those TextViews by supplying 1-5 to findViewById().
However, you probably shouldn't be doing it this way, and you shouldn't have a global textView variable (it'll only hold a reference to the last-created TextView).
Instead, try using an ArrayList and adding all of your TextViews to it. Then you won't need to give them IDs that don't follow the standards.
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private ArrayList<TextView> textViews = new ArrayList<>();
public void CreateDesign(){
linearLayout = (LinearLayout) findById(R.id.linearLayout);
for(int i = 1; i <= 5; i++) {
TextView textView = new TextView(this);
textView.setText(i + ". TextView");
linearLayout.addView(textView);
textViews.add(textView); //add it to the ArrayList
}
}
}
I have several strings of text on a screen that are set to invisible when the application starts. When a button is clicked on another screen, I want a specific string to become visible. Ultimately I want to have a few strings, of the several, become visible as a result of clicking this button.
public void buttona0Click(View view){
setContentView(R.layout.report_screen);
buttonClicked2 = 1;
if(buttonClicked1==1){
setVisibility(R.id.textView2.VISIBLE);
}
}
I am primarily looking for guidance on this line
setVisibility(R.id.textView2.VISIBLE);
I am new to programming in general, so I don't know if what I've said makes sense to most of you. Is .setText an alternative?
Write your own algo:
Use one boolean
view VISIBLE and GONE based own above boolean variable
You need to instantiate your TextView first. So the easiest way to begin is to declare them before onCreate() and outside of any other method so they are member variables
public class MyActivity extends Activity
{
TextView tv1, tv2, etc...;
public void onCreate(...)
{
super.onCreate(...);
setContentView(R.layout.my_layout);
tv1 = (TextView) findViewById(R.id.textView1);
...
}
Then in your onClick() change the Visibility which takes an int value
public void buttona0Click(View view){
buttonClicked2 = 1;
if(buttonClicked1==1){
tv1.setVisibility(View.VISIBLE);
}
}
Note: please don't include the "..." above in your code. That is just omitted code that I assume you know how to handle already. Also, I took out setContentView() from the onClick() method because typically this should only be done once in onCreate().
I'm not sure about the logic inside there because I don't know what the buttonClicked1 variables are for but that is how to do the Visibility.
setVisibility() Docs
Is .setText an alternative?
This will simply set the text so if you have it already set in your xml then you don't need to...you can just change the Visibility as you are trying to do. If you haven't set it then you will need to with something like
tv1.setText("Hello World"); // input your own String or String resource
Make sure you call setVisibility(View.GONE) for all your TextViews in onCreate() after setContentView(). Then in your if clause set the visibility of selected textview to View.VISIBLE - textview.setVisibility(View.VISIBLE)
First findViewById of the TextView and in onClick function of your button set visibility of that textView as VISIBLE
TextView textView = (TextView) findViewById(R.id.textView1);
public void buttona0Click(View view){
buttonClicked2 = 1;
if(buttonClicked1==1){
textView.setVisibility(view.VISIBLE);
}
}
I am a new android dev. I want to use a same TextView in all activity. But, i don't know how to declare TextView as global Variable & How can i use this to show text in activity. Please help me. The code is simple.
Thank for every one.
Write your XML code of text view with id as:
<TextText
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Than in your activity declare it before onCreate() method as:
public static TextView textview = (TextView) findViewByID(Your ID);
Than this will be accessible to all calsses.
You can use this everywhere:
TextView textview = (TextView) findViewByID(Your ID);
textview = (TextView) findViewByID(Your ID);
Make it a singleton. Or just keep one public static reference.
public class MyReference {
public static TextView myTextView = new TextView();
}
and then you can use it anywhere by calling MyReference.myTextView
I think this Link this is helpfull to
http://alinberce.wordpress.com/2012/02/20/android-edittext-with-custom-font-and-clear-button/
you have to create Editbox just One time & use anywhere in Application
weather use in Layout file or use Dynamically in java Class
I get a null pointer exception at line five/seven of my code:
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.footer_row, (ViewGroup) findViewById(R.id.header_layout_root));
TextView tv_footer = (TextView) findViewById(R.id.tv_footertext);
if(getListView().getCount()<=8) {
tv_footer.setText("You have no more assignments");
} else {
tv_footer.setText(Integer.toString(getListView().getCount()) + " assignments shown");
}
getListView().addFooterView(header, null, false);
I'm not too sure why, so could you tell me? List view's aren't my thing.
I'll tick the right answer!
Instead of using the findViewById() method from your Activity's View you should assign (which I assume is the footer TextView in your R.layout.footer_row) the TextView from the inflated View.
Sample code:
TextView tv_footer = (TextView) header.findViewById(R.id.tv_footertext);
Change third line of your code as specified below. as your inflated view contains the textView you specified.
TextView tv_footer = (TextView) header.findViewById(R.id.tv_footertext);
Thanks.
can you post the logcat information,it seems that tv_footer value may not set,It may be R.id.tv_footertext is not the part of layout which you set in the setContentView(....) method check it once.It is better if you provide code in detail(since your posted code seems fine)
I have an activity with a TextView that needs to be updated from a second activity.
I can pass the Text view data to the 2nd activity ok, but when I try to update that TextView
within the 2nd activity it crashes. My code:
1st Activity (where the TextView is defined in my xml):
Intent intent = new Intent(1stactivity.this, 2ndactivity.class);
intent.putExtra("homescore_value", ((TextView)findViewById(R.id.score_home)).getText());
startActivity(intent);
// code snippet
Then in my 2nd activity:
Bundle bundle = getIntent().getExtras();
hometext = bundle.getString("homescore_value"); // this works and displays the correct String from 1st activity,
but it crashes when I try to pull in as a TextView:
// other code snipped
int homescore = 0;
String Home_nickname = "HOME ";
TextView homestext = (TextView) bundle.get("homescore_value");
hometext.setText(Home_nickname +": "+homescore );
Please help.
You are trying to get a String as a TextView (you are setting a String in the intent from the first Activity).
You trying to cast String to TextView. The code that crashes is equivalent of:
String text = bundle.get("homescore_value"); //this is ok
TextView textView = (TextView)text; //this is bad
You should do instead:
String text = bundle.get("homescore_value");
TextView textView = (TextView)findViewById(R.id.yourTextViewId);
textView.setText(text);
This line here:
intent.putExtra("homescore_value", ((TextView)findViewById(R.id.score_home)).getText());
is attaching a String along with the intent, not a TextView.
You must inflate a new TextView within the 2nd activity, by either declaring it in the layout.xml, or programmatically placing it within the layout.
Something that solved part of this problem for me was setting the receiving String variables to null like this:
public String param1new = null;
public String param2new= null;
My issue with this is I'm trying to set background colors on several TextViews and only the first one is being set at this time.