-2

I am trying to get sunrise and sunset time for my current location from openweathermap api. I am following this tutorial.

I have copied the entire Function.java in the tutorial and trying to use the data to update my ViewPagerAdapter's Fragment.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class BlankFragment extends Fragment {

    public BlankFragment() {
        // Required empty public constructor
    }
    TextView cityField = getView().findViewById(R.id.tv_city);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

        RecyclerView rv = rootView.findViewById(R.id.rv_recycler_view);
        rv.setNestedScrollingEnabled(true);


        Function.placeIdTask asyncTask =new Function.placeIdTask(new Function.AsyncResponse() {
            public void processFinish(String weather_city, String weather_description, String weather_temperature, String weather_humidity, String weather_pressure, String weather_updatedOn, String weather_iconText, String sun_rise) {

                cityField.setText(weather_city);
             }
        });
        asyncTask.execute("25.180000", "89.530000"); //  asyncTask.execute("Latitude", "Longitude")
        rv.setHasFixedSize(true);
        MyAdapter adapter = new MyAdapter(new String[]{"Hello", "World"});
        rv.setAdapter(adapter);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);

        return rootView;
    }
}

and I have named my target for the city name as tv_city:

     <TextView
        android:id="@+id/tv_city"
        android:layout_toRightOf ="@+id/iv_image"
        android:layout_width="wrap_content"
        android:layout_height="40sp"
        android:text="Hello World"
        android:textSize="30sp"
        android:layout_marginBottom="5sp"
        android:paddingLeft="5sp"
        android:gravity="top" >
    </TextView>

The problem is, possibly I am making some mistake to fetch the data, as I am getting nul exception error for this line:

TextView cityField = getView().findViewById(R.id.tv_city);

as:

Process: com.example.phocast, PID: 9820                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
     at com.example.phocast.BlankFragment.<init>(BlankFragment.java:17)

WIth my limited knowledge in java, I am unable to solve it. Kindly help.

Update actually, writing oncreateview as:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

RecyclerView rv = rootView.findViewById(R.id.rv_recycler_view);
rv.setNestedScrollingEnabled(true);


Weather_OWM.placeIdTask asyncTask =new Weather_OWM.placeIdTask(new Weather_OWM.AsyncResponse() {
    public void processFinish(String weather_city, String weather_description, String weather_temperature, String weather_humidity, String weather_pressure, String weather_updatedOn, String weather_iconText, String sun_rise) {

       TextView cityField = rootView.findViewById(R.id.tv_city);
       cityField.setText(weather_city);
     }
});

solves my problem, but i dont know if this is proper/good way to use final. I will be really grateful if someone shows me the way before closing it.

BaRud
  • 3,055
  • 7
  • 41
  • 89

2 Answers2

0

Your cityView line of code is just hanging out there, not in a method or anything. The NullPointer is coming from getView(). You can't find the view before onCreateView occurs and the inflater does its thing. Move it to onCreateView, anytime after View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

Robert Nekic
  • 3,087
  • 3
  • 24
  • 36
0

You need initialize the textView after:

View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

After initialize the recyclerView you can write this:

cityField = rootView.findViewById(R.id.tv_city);

JaFer
  • 213
  • 2
  • 10