Can't acces TextView defined in onCreate [duplicate] - java

This question already has answers here:
How do I declare a TextView variable so that I can use it anywhere?
(2 answers)
Closed 6 years ago.
I am still a Java beginner and I have the problem that I have a onClick function and of course the onCreate in which i define the TextView to a value. But when I want to edit the TextView from the onClick i can't do that. Now I would like to know how I can make the TextView value "global".
final TextView result = (TextView) findViewById(R.id.result);

You have to instantiate the TextView after the setcontentView() method :
public class MainActivity extends Activity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textview);
}
}

You can define your TextView above method onCreate.
TextView result;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
setContentView(R.layout.yourLayout);
TextView result = (TextView) findViewById(R.id.result);
}

In your class you create an attribute:
private Textview tv;
then in onCreate method you initialise this variable like this :
tv = (TextView) findViewById(R.id.result);
Then you can use it wherever you want in your class by calling "tv".

Related

findViewById method outside the onCreate method?

lately, I was working on a very small training project.
But there is one error in my code:
public class MainActivity extends AppCompatActivity {
TextView textView = (TextView) findViewById(R.id.textview);
#override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView.setText("Welcome");
}
}
but when I tried to cut the textview global variable and paste it inside the onCreate method, the error has gone. Why is this error occur although I already have inflated the textView in a global variable ?!
You need to call findViewById(...) after calling setContentView(...).
Fetching the view before inflating the layout isn’t possible since there is no view.
findViewById() should only be used after setContentView() in onCreate.
That's where your entire layout get inflated. Only then you can access views using findViewById().
This is what you want:
public class MainActivity extends AppCompatActivity {
TextView textView;
#override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
textView.setText("Welcome");
}
}

how can i assign functions to buttons in android studio with out having the vm shut down [duplicate]

This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 2 years ago.
i am new to java and android studio ; i have set myself the goal of making a simple counter app in which one taps a button and a counter increases. Android studio shows no errors until i run t on my phone where it crashes as soon as i touch a button
i have tried to define the buttons as TextView tv; with no luck
public class MainActivity extends AppCompatActivity {
private int score;//this lets me use it else where within the public class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startCounter(View v) {
score = 0;
TextView tv = findViewById(R.id.textView); //the text view defines it as tv
tv.setText(score); //set the text of the label
}
public void buttonOnClick(View v) {
score++; //adds 1 to score
TextView tv = findViewById(R.id.textView); //the text view defines it as tv
tv.setText(score); //set the text of the label
}
}
i expect that the result is that the text view is updated to show the variable score but instead i get the error Invalid ID 0x00000000. and then D/AndroidRuntime: Shutting down VM
I just replace your code so please copy and follow
int score=0;
public class MainActivity extends AppCompatActivity {
private int score;//this lets me use it else where within the public class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewByID(R.id.tvID);
Button myButton = (Button)findViewByID(R.id.buttonID);
myButton.setOnClickListener(new View.OnClickListener{
score++;
tv.setText(score.toString());
});
}

Unable to display textview on button click in android

I want to create this textview, and then display it after this button is clicked, but no textview is displayed.
I dont't want to use findViewById(), if possible, because I want to create a new textview every time the button is pressed. I've tried making a linear layout first, but I've seen a lot of websites say that you don't need to, and I would prefer not to. Any advice would be helpful. Thank you.
EditText name=layout.findViewById(R.id.enterName);
final String Name=name.getText().toString();
Button create=layout.findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView ProgrammaticallyTextView = new TextView(MainActivity.this);
ProgrammaticallyTextView.setText(Name);
ProgrammaticallyTextView.setTextSize(22);
popup.dismiss();
}
});
There are no error messages and the logcat doesn't say that anything is wrong.
Try like this :
private LinearLayout lLayout;
private EditText lEditText;
private Button lButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lLayout = (LinearLayout) findViewById(R.id.linearLayout);
lButton = (Button) findViewById(R.id.button);
lEditText = (EditText) findViewById(R.id.editText);
lButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("Text New");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
lLayout.addView(createTextView(lEditText.getText().toString()));
}
};
}
private TextView createTextView(String text) {
final LayoutParams loutParams = new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(loutParams );
textView.setText("Text is " + text);
return textView;
}
Use the activity's findViewById() method to reference your layout views. For example, you can replace
EditText name=layout.findViewById(R.id.enterName);
Button create=layout.findViewById(R.id.create);
with
EditText name=getActivity().findViewById(R.id.enterName);
Button create=getActivity().layout.findViewById(R.id.create);
Note: if you are not using fragments then there is no need to use getActivity since findViewById() is a method of the superclass AppCompactActvity( or Activity).
I guess your code is not working because the Button View and Editext Views have not been reference when activity starts for the oncreate() method

Why does this code give me a null pointer exception? [duplicate]

This question already has answers here:
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
(9 answers)
When do activity's instance variables get initialized?
(2 answers)
Closed 4 years ago.
public class FindBeerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_beer);
}
Spinner color = (Spinner) findViewById(R.id.color);
TextView brands = (TextView) findViewById(R.id.brands);
public void onClickFindBeer(View view) {
//Get the selected item in the spinner
String beerType = String.valueOf(color.getSelectedItem());
//Display the selected item
brands.setText(beerType);
}
}
I know if I move the findviewbyid's into the onclick method, the code will run fine but i would like to understand why it wont work this way even though the setContentView has already been called.
Perhaps you misplaced the closing curly-brace. This one should fix your NPE.
public class FindBeerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_beer);
Spinner color = (Spinner) findViewById(R.id.color);
TextView brands = (TextView) findViewById(R.id.brands);
}
public void onClickFindBeer(View view) {
//Get the selected item in the spinner
String beerType = String.valueOf(color.getSelectedItem());
//Display the selected item
brands.setText(beerType);
}
}

Cannot Switch my Visibility on a TextView through an OnClick event

I have 2 textviews on my android app with ids : textView1 , textView2 . Among others on my main layout OnClickListener I am trying to make these 2 textViews INVISIBLE with this :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View activity = findViewById(R.id.mainlayout);
activity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView1.setVisibility(View.INVISIBLE);
textView2.setVisibility(View.INVISIBLE);
}});
}
I get ... cannot be resolved messages .
Thank you guyz all in advance for your immediate responses.
You need to initialize textviews
TextView textView1,textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
View activity = (View)findViewById(R.id.mainlayout);
Try this:
findViewById(R.id.textView1).setVisibility(View.INVISIBLE);
findViewById(R.id.textView2).setVisibility(View.INVISIBLE);
R.id.textView1 & R.id.textView2 should correspond to the android:id fields of the TextViews you want to make invisible in R.layout.activity_main
If you're still not getting the expected behavior, make sure you're not confusing View.GONE and View.INVISIBLE

Categories