Attempt to invoke virtual method - Null object reference - java

I am trying to implement an external colorwheel and the fragment where it should appear in keeps crashing my app. I think I understood why this happen´s, but after around 6 hours of trying to fix it I´m about to give up. I know this was asked befor, but I could not derive a fix for my specific problem. Here ist my code:
private ColorchoiceViewModel galleryViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(ColorchoiceViewModel.class);
View root = inflater.inflate(R.layout.fragment_colorchoice, container, false);
galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
}
});
ColorPickerView colorPickerView = (ColorPickerView) root.findViewById(R.id.colorPickerView);
colorPickerView.setColorListener (new ColorListener() {
#Override
public void onColorSelected(ColorEnvelope colorEnvelope) {
Toast.makeText(getActivity(), "Color:" + colorEnvelope.getColorRGB(), Toast.LENGTH_SHORT).show();
}
});
return root;
}
}
And this is the Logcat-Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.skydoves.colorpickerpreference.ColorPickerView.setColorListener(com.skydoves.colorpickerpreference.ColorListener)' on a null object reference
at com.lsh.homeauto.ui.colorchoice.ColorchoiceFragment.onCreateView(ColorchoiceFragment.java:43)
It would be really great if someone could help me fix this. Thanks in advance^^
Edit: Here´s my XML, as requested:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="horizontal"
android:gravity="center">
<com.skydoves.colorpickerpreference.ColorPickerView
android:id="#+id/colorPickerView"
android:layout_width="300dp"
android:layout_height="300dp"
app:palette="#drawable/palette"
app:selector="#drawable/wheel" />
</LinearLayout>

This might be because you are trying to use setColorListener on a view which is not even created. Note that you are using this method in onCreateView method. I suggest you to move those lines in onViewCreated method.
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ColorPickerView colorPickerView = (ColorPickerView) view.findViewById(R.id.colorPickerView);
colorPickerView.setColorListener (new ColorListener() {
#Override
public void onColorSelected(ColorEnvelope colorEnvelope) {
Toast.makeText(getActivity(), "Color:" + colorEnvelope.getColorRGB(), Toast.LENGTH_SHORT).show();
}
});
}

Related

(Android Studio) Can't use context/getActivity() in Fragment [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
First of all, I am absolute beginner for this, I'm sorry if this such a dumb mistakes. But I always got this error
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object referenceat com.example.loginregister.ui.home.HomeFragment$1.onResponse(HomeFragment.java:104)
whenever I tried to run my app. I already changed getActivity() to HomeFragment.this , or getApplicationActivity() but still error, I'm stuck this for a long time because I not really understand how it's work, I hope someone can explain to me how to call context in fragment, here my java
public class HomeFragment extends Fragment {
private RecyclerView rvData;
private RecyclerView.Adapter adData;
private RecyclerView.LayoutManager lmData;
private List<DataModel> listData = new ArrayList<>();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rvData = getActivity().findViewById(R.id.rv_user);
lmData = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
rvData.setLayoutManager(lmData);
retrieveData();
return inflater.inflate(R.layout.fragment_home, container, false);
}
public void retrieveData(){
APIRequestData ardData = RetroServer.connectRetrofit().create(APIRequestData.class);
Call<ResponseModel> showData = ardData.ardRetrieveData();
showData.enqueue(new Callback<ResponseModel>() {
#Override
public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
int code = response.body().getCode();
String message = response.body().getMessage();
Toast.makeText(getActivity(), "Code: "+code+"| Message: "+message, Toast.LENGTH_SHORT).show();
listData = response.body().getData();
adData = new AdapterData(getActivity(),listData);
rvData.setLayoutManager(lmData);
adData.notifyDataSetChanged();
}
#Override
public void onFailure(Call<ResponseModel> call, Throwable t) {
Toast.makeText(getActivity(), "Fail to Retrieve Data", Toast.LENGTH_SHORT).show();
}
});
}
and here is my fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_user"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/card_item"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
use this code
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
rvData = view.findViewById(R.id.rv_user);
return view;
}
As I see your layout is supposedly layout for a fragment. You should edit your code like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_home.xml, container, false);
rvData = inflate.findViewById(R.id.rv_user);
lmData = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
rvData.setLayoutManager(lmData);
retrieveData();
return inflate;
}
If you really need your "context" basically the private fragment variable
private Context context;
Then you may retrieve it in onAttach method somewhat like this:
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
this.context = context;
}
This enables you to use context specific features like getting colors etc.

Failing at updating a TextView with Android Emulator

I'm trying to update a TextView object's text by calling the setText() method. I provide a string value directly to it, but I can't get it to update on the UI of the app running on the Emulator.
This is taking place on a fragment (one of the fragments automatically generated when a project with a simple activity is created on Android Studio)
A couple points about my situation thus far:
I tried calling the setText() method with the runOnUiThread "pattern" to no avail.
getActivity().runOnUiThread(new Runnable()
{
#Override
public void run()
{
textView.setText("Service online");
}
});
I checked property mText of the TextView instance. It IS UPDATED. It just doesn't update on the UI :/
In short, no matter what I try to do, the UI element sticks to whatever string value is set on the XML Fragment file (or no value, if I delete the android:text attribute). Other posts on Stack Overflow similar to this issue did not help me either. Any idea what it could be?
Also, I'm posting the entire fragment related java code:
public class FirstFragment extends Fragment
{
public Gson serializer;
public TextView textView;
private NetworkManager networkManager;
private boolean serviceIsBound;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
textView = (TextView) view.findViewById(R.id.main_window);
textView.setText(R.string.app_name);
return inflater.inflate(R.layout.fragment_first, container, false);
}
#Override
public void onStart() {
super.onStart();
Intent bindIntent = new Intent(getActivity(), NetworkManager.class);
getActivity().bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
#Override
public void onStop()
{
super.onStop();
getActivity().unbindService(serviceConnection);
serviceIsBound = false;
}
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v("DEV UPDATE", "Starting Request ");
if (serviceIsBound)
{
GetAPIStatusResult result = networkManager.GetAPIStatus();
if (result.GetStatus())
{
Log.v("REQUEST RESULT", "API is Fine");
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText("Service online");
}
});
}
else
{
Log.v("REQUEST RESULT", "API is Down or a problem occurred");
textView.setText("Service down");
}
}
}
});
}
private ServiceConnection serviceConnection = new ServiceConnection()
{
#Override
public void onServiceConnected(ComponentName className, IBinder service)
{
NetworkManager.NetworkManagerServiceBinder binder = (NetworkManager.NetworkManagerServiceBinder) service;
networkManager = binder.GetService();
serviceIsBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg)
{
serviceIsBound = false;
}
};
}
The associated XML for the fragment:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<TextView
android:id="#+id/main_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Here will appear API status"
app:layout_constraintBottom_toTopOf="#id/button_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="SendRequest"
android:text="#string/SendRequest"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/main_window" />
</androidx.constraintlayout.widget.ConstraintLayout>
As user Cheticamp commented, I had an issue on my onCreateView() method, where I was calling the infalter.inflate() method twice, and not returning my view object.
I replaced the second inflate() method call with a return of my view object and it immediately worked! My UI was now being updated as expected!
You're trying to reference a view that belongs to the activity. If you want to update something in the activity you need to look at other methods rather than trying to directly reference the views.
A good place to start would be an interface you pass to the fragment that is created by the activity. Call the interface method from the fragment when you want to set the next. Then let the activity handle the updating. This is cleaner too as each view is responsible for its own elements only.
You also don't need to use runOnUIThread as onViewCreated isn't an ansychronos function you're already on the UI thread anyway.
Hopefully that helps.

null object reference of TextView in Fragment

I have two fragments and a main activity. The first fragment, FriendsFragment (extends ListFragment) is displayed on the screen and when the user clicks an item, the main Activity replaces the FriendsFragment with FeedFragment, then calls a method from FeedFragment to update the textView.
I'm getting an error that the textView object in the FeedFragment class is null even though I instantiate using the findViewById method.
I have looked at related questions and have tried the solutions but nothing is working. I've tried doing getView().findViewById(R.id.feed_view), getActivity().findViewById(R.id.feed_view), and I've tried putting these in onActivityCreated() and onCreateView()
The only thing that worked is writing this code in onActivityCreated():
text = getView().findViewById(R.id.feed_view);
text.setText("some string");
but this is not what I want
FeedFragments.java
public class FeedFragment extends Fragment {
private static String TAG = "Feed Fragment";
private TextView text;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.i(TAG, "Entered FeedFragment.java onActivityCreated()");
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Log.i(TAG, "Entered FeedFragment.java onCreateView()");
View v = inflater.inflate(R.layout.fragment_feed, container,false);;
text = v.findViewById(R.id.feed_view);
return v;
}
public void updateDisplay(int position)
{
Log.i(TAG, "FeedFragment.java: updateDisplay()");
text.setText(position);
}
MainActivity.java
// previously declared feedFragment: feedFragment = new FeedFragment();
public void onItemSelected(int position)
{
Log.i(TAG, "Entered onItemSelected(" + position + ")");
fragManager = getFragmentManager();
FragmentTransaction fragTransaction = fragManager.beginTransaction();
fragTransaction.replace(R.id.fragment_container, feedFragment, "feed_fragment");
fragTransaction.addToBackStack(null);
fragTransaction.commit();
feedFragment.updateDisplay(position);
}
fragment_feed.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/feed_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/greeting" />
</ScrollView>
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
onViewcreated() is being called after updateDisplay() because the system doesn't run it right away.
you need to pass a value to the feedFragment on it's creation and store it in a bundle and retrieve it after the fragment internal code is run by the system.
the way you solve it is like this:
when you instantiate feedFragment you do it like this:
feedFragment = FeedFragment.newInstance(position)
and inside FeedFragment class you should have a static code:
private static final String ARG_PARAM1 = "param1";
public static FeedFragment newInstance(int param1) {
FeedFragment fragment = new FeedFragment();
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
and non static code:
private int mParam1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getInt(ARG_PARAM1);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Log.i(TAG, "Entered FeedFragment.java onCreateView()");
View v = inflater.inflate(R.layout.fragment_feed, container,false);;
text = v.findViewById(R.id.feed_view);
text.setText(String.valueOf(mParam1));
return v;
}
I wrapped mParam1 in String.valueOf() because when you pass integer to setText it thinks you try to use a Strings.xml resource instead of the number you chose.
also I used very generic variable names. please change them to something meaningful so that your code makes sense.

Can PlaceAutocompleteFragment (overlay mode) be called from inside the Fragment class

I'm working on my project to create an activity(extends AppCompatActivity) with navigation drawer for switching between multiple Fragments and one of them is a MapFragment(extends Fragment) with a MapView implementation. I would like to call the AutoComplete Widget, provided by Google, inside MapFragment class.
Here is my code inside class MapFragment
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
.build();
autocompleteFragment.setFilter(typeFilter);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
String placeDetailsStr = place.getName() + "\n"
+ place.getId() + "\n"
+ place.getLatLng().toString() + "\n"
+ place.getAddress() + "\n"
+ place.getAttributions();
Log.i("OnPlaceSelected", placeDetailsStr);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15));
}
#Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i("OnPlaceSelected", "An error occurred: " + status);
}
});
syntax error was at
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
said that
Inconvertible type; cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.location.places.ui.PlaceAutocompleteFragment'
But when I put this set of code inside MainActivity, it works just fine. So I'm wondering if there's any way to call AutoComplete Widget inside Fragment it's just can't.
Any Answer will be appreciated. :)
Use getActivity()
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
You have to use something like this if you're trying to find the fragment from within a fragment:
placeAutocompleteFragment = (PlaceAutocompleteFragment)getChildFragmentManager().findFragmentById(R.id.fragmentPlaces);
You might run into issues with onPlaceSelected not being called, this is because your owning activity has to forward the result into the fragment.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
mapFragment.onActivityResult(requestCode, resultCode, data);
}
and mapFragment has to forward the result into the places fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
placeAutocompleteFragment.onActivityResult(requestCode, resultCode, data);
}
Even with this, I've had issues with onPlacesSelected not being called. Its usually best to just not nest the map and places fragments, but just have them separate in the owning activity.
I have been searching around on the interwebz too and couldn't find a solution, until I realised I had the same exact problem with the MapFragment, so I applied that technique and it worked!
Fragment:
private SupportPlaceAutocompleteFragment autocompleteFragment;
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
...
autocompleteFragment = (SupportPlaceAutocompleteFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
if(autocompleteFragment==null){
autocompleteFragment = (SupportPlaceAutocompleteFragment) SupportPlaceAutocompleteFragment.instantiate(context, "com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment");
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
}
#Override
public void onError(Status status) {
}
});
fm.beginTransaction().replace(R.id.autocomplete_fragment, autocompleteFragment).commit();
}
...
Note: I am using the support fragment (android.support.v4.app.Fragment), that's why I use SupportPlaceAutocompleteFragment
XML:
<RelativeLayout
android:id="#+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
You shoud use SupportPlaceAutocompleteFragment instead of PlaceAutocompleteFragment, it's replaced when using Fragment supportv4
SupportPlaceAutocompleteFragment autocompleteFragment
= (SupportPlaceAutocompleteFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
I had the very same question, and thanks to #Kalpesh idea, it works fine, and no problem of eventListenner, here's my code if it can help someone.
I have a tablayout with a viewpager and 3 fragment we can swipe right or left.
googlePLacesLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorFragment_bg">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp">
<fragment
android:id="#+id/autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp" />
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/largeTexxt"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
MyFragmentClass.java
package com.raccoon.trash.aregood.TabFragments;
public class MyFragmentClass extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(
R.layout.googlePLacesLayout, container, false);
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setHint("Let's find some restaurants");
final TextView textView = (TextView) mRelativeLayout.findViewById(R.id.largeTexxt);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
textView.setText("Place: " + place.getName()+ place.getId()+
place.getAddress()+ place.getPhoneNumber()+ place.getWebsiteUri());
System.out.println("Place: " + place.getName()+ place.getId()+
place.getAddress()+ place.getPhoneNumber()+ place.getWebsiteUri());
}
#Override
public void onError(Status status) {
System.out.println("An error occurred: " + status);
}
});
return mRelativeLayout;
}
}

Null pointer when setting text/background on android app [duplicate]

This is a canonical question for a problem frequently posted on StackOverflow.
I'm following a tutorial. I've created a new activity using a wizard. I get NullPointerException when attempting to call a method on Views obtained with findViewById() in my activity onCreate().
Activity onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View something = findViewById(R.id.something);
something.setOnClickListener(new View.OnClickListener() { ... }); // NPE HERE
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
Layout XML (fragment_main.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="packagename.MainActivity$PlaceholderFragment" >
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/something" />
</RelativeLayout>
The tutorial is probably outdated, attempting to create an activity-based UI instead of the fragment-based UI preferred by wizard-generated code.
The view is in the fragment layout (fragment_main.xml) and not in the activity layout (activity_main.xml). onCreate() is too early in the lifecycle to find it in the activity view hierarchy, and a null is returned. Invoking a method on null causes the NPE.
The preferred solution is to move the code to the fragment onCreateView(), calling findViewById() on the inflated fragment layout rootView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
View something = rootView.findViewById(R.id.something); // not activity findViewById()
something.setOnClickListener(new View.OnClickListener() { ... });
return rootView;
}
As a side note, the fragment layout will eventually be a part of the activity view hierarchy and discoverable with activity findViewById() but only after the fragment transaction has been run. Pending fragment transactions get executed in super.onStart() after onCreate().
Try OnStart() method and just use
View view = getView().findViewById(R.id.something);
or Declare any View using getView().findViewById method in onStart()
Declare click listener on view by anyView.setOnClickListener(this);
Try to shift your accessing views to the onViewCreated method of fragment because sometimes when you try to access the views in onCreate method they are not rendered at the time resulting null pointer exception.
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
View something = findViewById(R.id.something);
something.setOnClickListener(new View.OnClickListener() { ... }); // NPE HERE
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
Agreed, this is a typical error because people often don't really understand how Fragments work when they begin working on Android development. To alleviate confusion, I created a simple example code that I originally posted on Application is stopped in android emulator , but I posted it here as well.
An example is the following:
public class ContainerActivity extends FragmentActivity implements ExampleFragment.Callback
{
#Override
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
this.setContentView(R.layout.activity_container);
if (saveInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.activity_container_container, new ExampleFragment())
.addToBackStack(null)
.commit();
}
getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener()
{
public void onBackStackChanged()
{
int backCount = getSupportFragmentManager().getBackStackEntryCount();
if (backCount == 0)
{
finish();
}
}
});
}
#Override
public void exampleFragmentCallback()
{
Toast.makeText(this, "Hello!", Toast.LENGTH_LONG).show();
}
}
activity_container.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/activity_container_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ExampleFragment:
public class ExampleFragment extends Fragment implements View.OnClickListener
{
public static interface Callback
{
void exampleFragmentCallback();
}
private Button btnOne;
private Button btnTwo;
private Button btnThree;
private Callback callback;
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
this.callback = (Callback) activity;
}
catch (ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "Activity must implement Callback interface.", e);
throw e;
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_example, container, false);
btnOne = (Button) rootView.findViewById(R.id.example_button_one);
btnTwo = (Button) rootView.findViewById(R.id.example_button_two);
btnThree = (Button) rootView.findViewById(R.id.example_button_three);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
btnThree.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v)
{
if (btnOne == v)
{
Toast.makeText(getActivity(), "One.", Toast.LENGTH_LONG).show();
}
else if (btnTwo == v)
{
Toast.makeText(getActivity(), "Two.", Toast.LENGTH_LONG).show();
}
else if (btnThree == v)
{
callback.exampleFragmentCallback();
}
}
}
fragment_example.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/example_button_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="#string/hello"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<Button
android:id="#+id/example_button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/example_button_one"
android:layout_alignRight="#+id/example_button_one"
android:layout_below="#+id/example_button_one"
android:layout_marginTop="30dp"
android:text="#string/hello" />
<Button
android:id="#+id/example_button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/example_button_two"
android:layout_alignRight="#+id/example_button_two"
android:layout_below="#+id/example_button_two"
android:layout_marginTop="30dp"
android:text="#string/hello" />
</RelativeLayout>
And that should be a valid example, it shows how you can use an Activity to display a Fragment, and handle events in that Fragment. And also how to communicate with the containing Activity.
The view "something" is in fragment and not in activity, so instead of accessing it in activity you must access it in the fragment class like
In PlaceholderFragment.class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container,
false);
View something = root .findViewById(R.id.something);
something.setOnClickListener(new View.OnClickListener() { ... });
return root;
}
You are trying to access UI elements in the onCreate() but , it is too early to access them , since in fragment views can be created in onCreateView() method.
And onActivityCreated() method is reliable to handle any actions on them, since activity is fully loaded in this state.
Add the following in your activity_main.xml
<fragment
android:id="#+id/myFragment"
android:name="packagename.MainActivity$PlaceholderFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</fragment>
Since you have declared your View in the fragment_main.xml,move that piece of code where you get the NPE in the onCreateView() method of the fragment.
This should solve the issue.
in the posted code above in the question there is a problem :
you are using R.layout.activity_main in oncreate method, but the xml files name is "fragment_main.xml" , means you are trying to get the view of fragment_main.xml file which is not being shown so it gives null pointer exception. change the code like :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);// your xml layout ,where the views are
View something = findViewById(R.id.something);
something.setOnClickListener(new View.OnClickListener() { ... }); // NPE HERE
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
You have to remember important thing is :
NullPointerException occurs when you have declared your variable and trying to retreive its value before assigning value to it.
Use onViewCreated() Method whenever using or calling views from fragments.
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
View v = view.findViewById(R.id.whatever)
}
I've got the same NullPointerException initializing a listener after calling findViewById() onCreate() and onCreateView() methods.
But when I've used the onActivityCreated(Bundle savedInstanceState) {...} it works. So, I could access the GroupView and set my listener.
I hope it be helpful.
Most popular library for finding views which is used by almost every developer.
ButterKnife
As I can their are enough answers explaining finding views with proper methodology. But if you are android developer and code frequently on daily basis then you can use butter-knife which saves a lot time in finding views and you don't have write code for it, With in 2-3 steps you can find views in milliseconds.
Add dependency in app level gradle:
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
Add plugin for butter knife:
File -> Settings -> plugins->
Then search for Android ButterKnife Zelezny and install plugin and restart your studio and you are done with it.
Now just go to Oncreate method of your activity and right click on your layout_name and tap on generate button and select butterknife injection option and your views references will be automatically created like mention below:
#BindView(R.id.rv_featured_artist)
ViewPager rvFeaturedArtist;
#BindView(R.id.indicator)
PageIndicator indicator;
#BindView(R.id.rv_artist)
RecyclerView rvArtist;
#BindView(R.id.nsv)
NestedScrollingView nsv;
#BindView(R.id.btn_filter)
Button btnFilter;

Categories