尝试从OpenWeatherMap获取数据时的Null点异常



我正在尝试从openweathermap API获得我当前位置的日出和日落时间。我正在关注本教程。

我在教程中复制了整个Function.java,并尝试使用数据来更新我的ViewPagerAdapter片段。

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

我将城市名称的目标命名为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>

问题是,我可能会犯一些错误来获取数据,因为我会发现此行的NUL异常错误:

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)

凭借我在Java的有限知识,我无法解决它。请帮助。

更新实际上,将oncreateview编写为:

公共视图oncreateview(layoutinflater膨胀者,ViewGroup容器, 捆绑savedinstancestate({ //将此片段的布局充气 最终查看rootview = forter.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);
     }
});

解决了我的问题,但是我不知道这是使用最终的适当/好方法。如果有人在关闭之前向我展示道路,我将非常感谢。

您的CityView代码线只是在那里闲逛,而不是在方法或其他任何方面。NULLPOINTER来自GetView((。在发生OtCreateview之前,您找不到视图,而Afterater则做到了。将其移至 View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

之后的任何时间,

您需要在以下内容之后初始化文本视图。

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

初始化recyclerview后您可以写下:

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

最新更新