App crash with Null Object Reference [duplicate] - android

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am using a AutoCompleteTextView in a Fragment...
but when I use a method which refreshes the Suggestions for AutoCompleteTextView, the App crashes with Null Object Reference
here's the method -
private ArrayList<String> sugs = new ArrayList<String>();
public void refreshSugs()
{
((AutoCompleteTextView) getView().findViewById(R.id.editText1)).setAdapter(new ArrayAdapter(getActivity(), 17367050, this.sugs));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Inflating the Layout
View rootView = inflater.inflate(R.layout.home_fragment, container, false);
Button buttongo = (Button) rootView.findViewById(R.id.button1);
EditText et1 = (EditText) rootView.findViewById(R.id.editText1);
Button buttonsave = (Button) rootView.findViewById(R.id.button2);
bar = (ProgressWheel) rootView.findViewById(R.id.progressBar1);
i = (ImageView) rootView.findViewById(R.id.imageView1);
background2 = (TextView) rootView.findViewById(R.id.textView1);
background3 = (TextView) rootView.findViewById(R.id.textView2);
readSugs();
refreshSugs();
return rootView;
}

Might be your getView() is null.
private ArrayList<String> sugs = new ArrayList<String>();
private View rootView;
public void refreshSugs() {
((AutoCompleteTextView) rootView.findViewById(R.id.editText1)).setAdapter(new ArrayAdapter(getActivity(), 17367050, this.sugs));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Inflating the Layout
rootView = inflater.inflate(R.layout.home_fragment, container, false);
..... your code
return rootView;
}

private ArrayList<String> sugs = new ArrayList<String>();
private View rootView;
public void refreshSugs(View rootView) {
((AutoCompleteTextView) rootView.findViewById(R.id.editText1)).setAdapter(new ArrayAdapter(getActivity(), 17367050, this.sugs));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Inflating the Layout
rootView = inflater.inflate(R.layout.home_fragment, container, false);
..... your code...........
refreshSugs0(rootView);
return rootView;
}

Related

How use a button inside a fragment?

I'm trying to use 4 fragments
[F1]
[F2][F3][F4]
F1 will have 3 buttons to change between F2,F3 and F4 but i don´t know how to use the buttons properties like in a normal Activity
That's what i have in my F1 code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_01, container, false);
}
Check this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_01, container, false);
// here you have the reference of your button
Button yourButton = (Button)view.findViewById(R.id.your_button_id);
return view;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_01, container, false);
Button firstButton = (Button)view.findViewById(R.id.firstbutton);
Button seconButton = (Button)view.findViewById(R.id.seconbutton);
Button thirdButton = (Button)view.findViewById(R.id.thirdbutton);
return view;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
Button button = (Button) rootView.findViewById(R.id.buttonSayHi);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onButtonClicked(v);
}
});
return rootView;
}
public void onButtonClicked(View view)
{
//do your stuff here..
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frameLayoutFragmentContainer, new FragmentTwo(), "NewFragmentTag");
ft.commit();
ft.addToBackStack(null);
}
}
It's a simple way to reference a button or any widget in a fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);
btnOne = view.findViewById(R.id.btnOne);
return view;
You should get the view first, and then get the button from the view
like this:
View rootView = inflater.inflate(R.layout.fragment_01, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
You should do some tutoriasl first, its not that hard.

communicate from dialog fragment to fragment

I have two fragments:AllItemsFragment, CreateItemDialogFragment
The AllItemsFragment displays a list of items in the sqlite,CreateItemDialogFragment is used to create the item to the SQLite.
Now I have a question is after I creating a item in createItemDialogFragment,the dialog dismiss, how to update the display of the items in AllItemsFragment
In AllItemsFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(LIFETAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_all_items, container, false);
ButterKnife.bind(this, view);
tvTitle.setText("All Items");
DbHandler dbHandler = new DbHandler(getActivity(),null,null,1);
ArrayList<Item> items = dbHandler.getAllItems();
AllItemsAdapter adapter = new AllItemsAdapter(getActivity(),items);
lvItems.setAdapter(adapter);
return view;
}
In CreateItemDialogFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view =inflater.inflate(R.layout.fragment_create_item_dialog,null);
ButterKnife.bind(this, view);
itemPrice.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
return view;
}
#OnClick(R.id.clear)
void clearDialog(){
dismiss();
}
//create item
#OnClick(R.id.save)
void saveItem() {
if(!itemName.getText().equals(null)||!itemPrice.getText().equals(null)){
String name = itemName.getText().toString();
String priceStr = itemPrice.getText().toString();
Double price = Double.parseDouble(priceStr);
Item item = new Item(name,price);
DbHandler dbHandler = new DbHandler(getActivity(),null,null,1);
dbHandler.addItems(item);
dismiss();
}
}
Try to update the data on onResume callback of the AllItemsFragment.
#Override
public void onResume(){
super.onResume();
// update data over here.
}

Update textview in fragment

This might be a very simple problem, but I can't seem to get it to work.
I'm trying to update a textview in a Fragment on "OnCreateView"
My code looks like this
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_display_product, container, false);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return inflater.inflate(R.layout.fragment_display_product, container,false);
}
The text in textview txtAge never get set and I can't seem to find a reason.
I'm new to android so sorry if this is a stupid question.
/Birger
Do it like this-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_display_product, null);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return v;
}
Return the same view which you have inflated already.

fragments use assets with swipe views appcompat

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

How to get an id of a View inside a Fragment

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

Categories

Resources