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");
}
}
Related
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".
I am trying to get the Id of a text view in a fragment's onCreate() method. It returns null everytime.
I want to display my contacts in a fragment. For that at the end of FetchContact() method I assign the contact list to TextView.
Since my code is returning null when I find the TextView my application gives NullPointerException.
Anyone who could tell me how to get the Id of TextView of a Fragment in onCreate() method.
I tried making an object of activity class to get the TextView, still the code returned null. No luck.
public class ContactsFragment extends Fragment {
public TextView outputtext;
Context context;
class ActivityObj extends Activity{
public TextView text;
Context objContext;
#Override
public void onCreate(Bundle savedInstanceState) {
text =(TextView)findViewById(R.id.textContact);
objContext = getApplicationContext();
super.onCreate(savedInstanceState);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
ActivityObj obj = new ActivityObj();
super.onCreate(savedInstanceState);
outputtext =(TextView) getView().findViewById(R.id.textContact);
// outputtext= obj.text;
context=getActivity().getApplicationContext();
//fetchContacts();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.contacts_layout, container, false);
return rootView;
}
find the TextView my application gives NullPointerException.
Because in Fragment lifecycle onCreate method called before onCreateView method.
override onViewCreated which call after onCreateView method and use use first parameter of onViewCreated method for accessing views from Fragment layout:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView text =(TextView)view.findViewById(R.id.textContact);
/// your code here....
}
use
rootView.findViewById(R.id....) in your oncreateView there is where you should do the UI related work
You should use like this on onActivityCreated method
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView tv = (TextView) getActivity().findViewById(R.id.textView1);
}
}
Or use callbacks
You cannot initialize any view using findViewById before onCreateView method calls.
So you must have to wait for the callback to run. And there are two ways to get it. And it will work on onActivityCreated because it called after onCreateView
As described by ρяσѕρєя K using the View param came from onViewCreated.
You can call getView() in any callback(delegate) which call after
onCreateView()
Like
TextView tv = (TextView) getView().findViewById(R.id.tv_id);
Can someone tell me where I am going wrong?
I'm trying to change my textview called MyText to something else, but when I try I get the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2071)
public class MainScreen extends Activity {
TextView texting = (TextView) findViewById(R.id.MyText);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
texting.setText("Test");
};
}
This is how your code should look like:
public class MainScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
// If you're not using this view anywhere, you don't have to declare it globally. Use only a local variables
TextView texting = (TextView) findViewById(R.id.MyText);
texting.setText("Test");
};
}
You should ALWAYS initialize your views after you have your root layout. In this case, right after you did setContentView(R.layout.activity_main_screen); This scenario applies to Activites.
In case you use a fragment you should do this right after you inflated your root layout in the onCreateView method. Something like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = (LinearLayout) inflater.inflate(R.layout.my_fragment, container, false);
// THIS is where you start initializing your views
return view;
}
The TextView is not available until after setContentView is called. So, findViewById will return null when MainScreen is instantiated. The solution is to move the findViewById call to the onCreate method as follows:
public class MainScreen extends Activity {
TextView texting;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
texting = (TextView) findViewById(R.id.MyText);
texting.setText("Test");
}
}
Do the following:
public class MainScreen extends Activity {
TextView texting;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
texting = = (TextView) findViewById(R.id.MyText);
texting.setText("Test");
};
}
You have to initialize your TextView object inside your onCreate method.
I'm trying to set a drawable resource as background for my main relative layout via java, but whenever I do it, my app crashes.
Here is the part of the code which doesn't work fine:
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (RelativeLayout) findViewById(R.layout.activity_game);
setContentView(R.layout.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Any idea?
Thanks in advance
Call findViewById() only after setContentView() and supply an identifier in R.id (and not R.layout) so that it can return something other than null.
Remember,you are trying to find a view by id but what you are doing is actually trying to inflate a layout the wrong way.Change the R.layout.activity_game to R.id.activity_game and make sure the relative layout is givent the
android:id = "#+id/activity_game"
The full code should be
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layout = (RelativeLayout) findViewById(R.id.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Hope it helps.Happy coding.
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