尝试从openweathermap获取数据时出现零点异常

时间:2018-02-16 18:24:43

标签: java android json openweathermap

我正试图从openweathermap api获取当前位置的日出和日落时间。我正在关注this教程。

我已经复制了教程中的整个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);

为:

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写为:

public View onCreateView(LayoutInflater inflater,ViewGroup容器,                          Bundle savedInstanceState){     //为此片段扩充布局     final查看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);
     }
});

解决了我的问题,但我不知道这是否是使用final的正确/好方法。如果有人在关闭之前给我指路,我将非常感激。

2 个答案:

答案 0 :(得分:0)

您的cityView代码行只是挂在那里,而不是方法或任何东西。 NullPointer来自getView()。在onCreateView发生之前你无法找到视图,并且inflater会做它的事情。在View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

之后随时将其移至onCreateView

答案 1 :(得分:0)

您需要在以下后初始化textView:

SELECT t1.name, t1.number FROM table t1 LEFT OUTER JOIN (SELECT name, number FROM table where number < 0) t2 ON t1.name = t2.name and t1.number = t2.number WHERE t1.number > 0 and t2.number IS NOT NULL UNION ALL SELECT t1.name, t1.number FROM table t1 LEFT OUTER JOIN (SELECT name, number FROM table where number < 0) t2 ON t1.name = t2.name WHERE t1.number > 0 and t2.number IS NULL;`

初始化recyclerView后,您可以写下:

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