Java logcat错误android studio编程片段

Java logcat错误android studio编程片段,java,android,android-fragments,android-studio,android-activity,Java,Android,Android Fragments,Android Studio,Android Activity,我的片段类正在解析json文件中的信息。但因为这是第一次活动,这是我第一次尝试把它变成一个片段 我的片段类: public class salahtimesparser extends Fragment { View rootView; TextView fajrTV; TextView zuhrTV; TextView asrTV; TextView maghribTV; TextView ishaTV; @Override p

我的片段类正在解析json文件中的信息。但因为这是第一次活动,这是我第一次尝试把它变成一个片段

我的片段类:

public class salahtimesparser extends Fragment {
    View rootView;
    TextView fajrTV;
    TextView zuhrTV;
    TextView asrTV;
    TextView maghribTV;
    TextView ishaTV;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_salahtimesparser, container, false);
        new GetMethodEx().execute("http://muslimsalat.com/11717/daily/4.json?key=ea4417ebf152c478082e1ff6a1a759d9");
        return rootView;}

    public class GetMethodEx  extends AsyncTask<String, Void, InputStream> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            fajrTV = (TextView) rootView.findViewById(R.id.fajr);
            zuhrTV = (TextView) rootView.findViewById(R.id.zuhr);
            asrTV = (TextView) rootView.findViewById(R.id.asr);
            maghribTV = (TextView) rootView.findViewById(R.id.maghrib);
            ishaTV = (TextView) rootView.findViewById(R.id.isha);
        }

        @Override
        protected InputStream doInBackground(String... params) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(params[0]);
                HttpResponse response = client.execute(post);
                int status = response.getStatusLine().getStatusCode();
                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream in = entity.getContent();
                    return in;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(InputStream in) {
            super.onPostExecute(in);
            try {
                JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

                String fajr = null;
                String zuhr = null;
                String asr = null;
                String maghrib = null;
                String isha = null;

                reader.beginObject();
                while (reader.hasNext()) {
                    String name = reader.nextName();
                    if (name.equals("items")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            reader.beginObject();
                            while (reader.hasNext()) {
                                String innerName = reader.nextName();
                                final boolean isInnerNull = reader.peek() == JsonToken.NULL;
                                if (innerName.equals("fajr") && !isInnerNull) {
                                    fajr = reader.nextString();
                                } else if (innerName.equals("dhuhr") && !isInnerNull) {
                                    zuhr = reader.nextString();
                                } else if (innerName.equals("asr") && !isInnerNull) {
                                    asr = reader.nextString();
                                } else if (innerName.equals("maghrib") && !isInnerNull) {
                                    maghrib = reader.nextString();
                                } else if (innerName.equals("isha") && !isInnerNull) {
                                    isha = reader.nextString();
                                } else {
                                    reader.skipValue();
                                }
                            }
                            reader.endObject();
                        }
                        reader.endArray();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
                reader.close();

                fajrTV.setText(fajr);
                zuhrTV.setText(zuhr);
                asrTV.setText(asr);
                maghribTV.setText(maghrib);
                ishaTV.setText(isha);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getActivity().getMenuInflater().inflate(R.menu.main2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

findViewById不适用于片段。你需要像下面这样做

ImageView imageView = (ImageView) getView().findViewById(R.id.foo);

这是一个NPE,因为
onCreateView()
中的rootView是本地对象:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //don't declare again use the global object rootView  
        rootView = inflater.inflate(R.layout.fragment_salahtimesparser, container, false);
        new GetMethodEx().execute("http://muslimsalat.com/11717/daily/4.json?key=ea4417ebf152c478082e1ff6a1a759d9");
        return rootView;
}

您可以在onCreateView中查找DviewById,而不是使用onPreExecute内部,因为您的视图对象不是全局声明的

请尝试以下代码:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_salahtimesparser, container, false); fajrTV = (TextView) rootView.findViewById(R.id.fajr); zuhrTV = (TextView) rootView.findViewById(R.id.zuhr); asrTV = (TextView) rootView.findViewById(R.id.asr); maghribTV = (TextView) rootView.findViewById(R.id.maghrib); ishaTV = (TextView) rootView.findViewById(R.id.isha); new GetMethodEx().execute("http://muslimsalat.com/11717/daily/4.json?key=ea4417ebf152c478082e1ff6a1a759d9"); return rootView;}
使用


在GetMethodEx()的onPostExecute()中。

@johnab您想讨论什么?发布您的问题请进一步中止,因为我不了解onCreateView()方法中的
rootView
对象是本地java对象。在onPreExecute()方法中,您试图访问从未初始化的全局对象
rootView
。我们可以在聊天中讨论这个问题吗?请打开一个,仅在此处继续。。一旦其自动达到限制,其将要求提供聊天光盘。。
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_salahtimesparser, container, false); fajrTV = (TextView) rootView.findViewById(R.id.fajr); zuhrTV = (TextView) rootView.findViewById(R.id.zuhr); asrTV = (TextView) rootView.findViewById(R.id.asr); maghribTV = (TextView) rootView.findViewById(R.id.maghrib); ishaTV = (TextView) rootView.findViewById(R.id.isha); new GetMethodEx().execute("http://muslimsalat.com/11717/daily/4.json?key=ea4417ebf152c478082e1ff6a1a759d9"); return rootView;}
if(getView()!=null){
fajrTV.setText(fajr);
zuhrTV.setText(zuhr);
asrTV.setText(asr);
maghribTV.setText(maghrib);
ishaTV.setText(isha);
}