logcat错误android studio编程片段

宁可枝头抱香死,何曾吹落北风中。这篇文章主要讲述logcat错误android studio编程片段相关的知识,希望能为你提供帮助。
我的片段类是从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 GetMethodExextends 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); }

}
当我单击应用程序上的选项时应用程序崩溃时会发生这种情况。任何建议如何解决这个问题?
logcat的:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object referenceat com.kazam.salahtimes.salahtimesparser$GetMethodEx.onPreExecute(salahtimesparser.java:45) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) at android.os.AsyncTask.execute(AsyncTask.java:535) at com.kazam.salahtimes.salahtimesparser.onCreateView(salahtimesparser.java:37) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

答案这是一个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; }

另一答案findViewById不适用于片段。你需要做类似下面的事情
ImageView imageView = (ImageView) getView().findViewById(R.id.foo);

另一答案您可以在onCreateView中找到viewById,而不是使用onPreExecute,因为您的View对象未全局声明。
试试这段代码:
@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); }

【logcat错误android studio编程片段】在GetMethodEx()的onPostExecute()中。

    推荐阅读