Relatively new to Java and Android programming can anybody help as to why I'm getting a NullPointerException?
public class DpsFragment extends Fragment {
Weapon weppy;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
weppy.setMaxdmg(200);
weppy.setMindmg(100);
TextView tv= (TextView) getView().findViewById(R.id.textView1);
tv.setText("hello");
return inflater.inflate(R.layout.dpsfrag, container, false);
}
public class Weapon {
private int mindmg;
private int maxdmg;
public Weapon(int mindmg, int maxdmg) {
this.setMindmg(mindmg);
this.setMaxdmg(maxdmg);
}
public int getMindmg() {
return mindmg;
}
public void setMindmg(int mindmg) {
this.mindmg = mindmg;
}
public int getMaxdmg() {
return maxdmg;
}
public void setMaxdmg(int maxdmg) {
this.maxdmg = maxdmg;
}
}}
Very simple code, I know, but I have no idea where I have gone wrong? Thanks for any help .
weppy is null so NPE ...i think you forgot to initialize it.
it may be weppy = new Weapon(mindmg,maxdmg);
ALSO
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
weppy = new Weapon(100,200); <<<<<<See here
TextView tv= (TextView) getView().findViewById(R.id.textView1);
tv.setText("hello");
return inflater.inflate(R.layout.dpsfrag, container, false);
}
Also you can not use getView() inside onCreateView because the view you are accessing has not been created yet. So you should do it like this:
View view = inflater.inflate(R.layout.dpsfrag, container, false);
TextView tv= (TextView) view.findViewById(R.id.textView1);
tv.setText("hello");
return view;
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
i have started to learn android recently and i was looking at a fragment example in a video tutorial
in the tutorial they haven't made the textview static but when i tried to implement fragment with some changes in the code i started to get null pointer exception my code looks like this
public class BlankFragment2 extends Fragment {
TextView tv;
public BlankFragment2() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tv= (TextView) getActivity().findViewById(R.id.txt2);
}
public void setName(String name){
//tv=(TextView)getActivity().findViewById(R.id.txt2);
if(tv == null){
System.out.println("tv is null");
}
tv.setText("Hi "+name);
}
}
i am getting null pointer exception at settext unless i make the textview static can someone please explain why
this is not duplicate of what is NPE i know what NPE is and why it shows up my question here is that after initializing the textview in onActivityCreated why it is null again in setname function
Not getActivity().findViewById(), it must be view.findViewById() because Activity.findViewById() will look for a View in the Activity layout, whereas view.findViewById() will look for the View in the Fragment layout.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
return inflatedView;
}
And in onViewCreated you can initialise your TextView
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// here goes your intialisation using view
tv= (TextView) view.findViewById(R.id.txt2);
}
Make sure you have a TextView with the id txt2 in fragment_blank_fragment2.xml
NullPointerException is thrown when an application attempts to use an
object reference that has the null value.
FYI
You should return VIEW's object
Check findViewById(R.id.txt2 . Make sure you this ID present in XML.
Don't
tv= (TextView) getActivity().findViewById(R.id.txt2);
Do
tv= (TextView) view.findViewById(R.id.txt2);
Try this way
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
tv= (TextView) view.findViewById(R.id.txt2);
return view;
}
Sorry guys it was a stupid mistake on my side,
in my mainactivity i wrote
BlankFragment2 frag2 = new BlankFragment2();
frag2.setName(name);
i corrected it by writing
BlankFragment2 frag2=(BlankFragment2)getFragmentManager().findFragmentById(R.id.frag2);
fragb.setName(name);
now it is working
I am trying to run an AndroidApp on my phone. I am able to show my welcome fragment, moreover I can trigger the log-message. Unfortunately I am getting a null pointer exception if I want to change the text value from 'welcome' to 'Neuer Text'. What went wrong? I am quite new to android development.
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) v.findViewById(R.id.welcome_text);
text1.setText("NeuerText");
}
}
In the onClick(), v is the view item that was clicked, e.g. the button, not the view that is inflated in onCreateView().
You should use getView():
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
switch (v.getId) {
case R.id.button1:
TextView text1 = (TextView) getView().findViewById(R.id.welcome_text);
text1.setText("NeuerText");
break;
}
}
}
Also, if you don't want that action taking place for every button you may want to consider using a switch statement in your onClick().
You cant get the TextView from the button's onClick passed parameter ("View v"), since this is the actual button's view.
You should do something like:
public class WelcomeFragment extends Fragment implements OnClickListener {
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) view.findViewById(R.id.welcome_text);
text1.setText("NewerText"); //Also fixed typo
}
}
In OnClick(View v) , v is the button that you are clicking on. The TextView does not belong to Button, it belongs to R.layout.fragment_welcome. So you find and initialize the TextView inside onCreateView() of the fragment, and then use it inside onClick();
Some what like this:
public class WelcomeFragment extends Fragment implements OnClickListener {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
tv = (TextView) v.findViewById(R.id.welcome_text);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
tv.setText("NeuerText");
}
}
I am using fragments and I canĀ“t use the assets to change my letter. I am using swipe page with appcompat. Here is my code:
public class AcercaFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.acerca, container, false);
TextView logouno = (TextView) getView().findViewById(R.id.logouno);
Typeface typeface = Typeface.createFromAsset(getActivity()
.getAssets(), "fonts/Roboto-Thin.ttf");
logouno.setTypeface(typeface);
return view;
}
}
Try with this little change:
public class AcercaFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.acerca, container, false);
TextView logouno = (TextView) view.findViewById(R.id.logouno);
Typeface typeface = Typeface.createFromAsset(getActivity()
.getAssets(), "fonts/Roboto-Thin.ttf");
logouno.setTypeface(typeface);
return view;
}
}
The way you applied your font was ok, but you must get your TextView from the View view.
You can't access views from getView() in your onCreateView method. In here, you should only return an inflated view:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.acerca, container, false);
}
Then, initialize the UI elements like so:
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
TextView logouno =(TextView) getView().findViewById(R.id.logouno);
Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Thin.ttf");
logouno.setTypeface(typeface);
}
I'm trying to convert a layout so that it will include fragments.
One of the views is an ImageButton that has a listener.
The code worked fine as an Activity but makes trouble as a Fragment...
First problem was that I couldn't use findViewById,
but I was able to find the answer for that here and fixed it with getView().
I can't find an answer to the second problem...
after I declared:
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
I want to set an on click listener:
camBt.setOnClickListener(listener);
But it keeps acting like the ImageButton (camBt) doesn't exist...
I don't even know what's the problem... it worked fine in the Activity...
Here's the full code.
Many thanks!
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
return V;
}
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
}
Move
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView().
Like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.camera_fragment, container, false);
// Here it is
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(listener);
return v;
}
And one more thing you need to understand that variable name should starts with small letter. So you should use View v instead of View V.
You should add
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView() like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
return V;
}
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)V.findViewById(R.id.button1);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
camBt.setOnClickListener(listener);
return V;
}
}
Hi I am trying to get an id of some View in a Fragment from a string, however it doesn't seem to work... Is there a special case for fragments?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View singleplayerView = inflater.inflate(R.layout.singleplayer_tab, container, false);
setupMap();
return singleplayerView;
}
private void setupRank() {
TextView view = (TextView) getByStringId("rank0");
view.setText("hello");
}
private final View getByStringId(final String id) {
return findViewById(getResources().getIdentifier(id, "id", getActivi
ty().getPackageName()));
}
So I am trying to set the textView field rank0 in the layout that is used for my fragment... but right now it gets a NUllPointer because it couldnt find the id
Thank you
you can define your Layout In your onCreateView() inside Fragments like this:
View V = new View(getActivity());
V = inflater.inflate(R.layout.fragment_layout,container, false);
Then
TextView SomeField = (TextView)V.findViewById(R.id.TxtSomeField);
Inside fragment, add getActivity before findViewById:
getActivity().findViewById(...);
Try this,
String id;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getArguments();
id = extra.getString("id", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_calendar_details, container, false);
Button b = (Button) view.findViewById(id);
return view;
}