logcat错误android studio编程片段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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);
}
}
当我单击应用程序上的选项时应用程序崩溃时会发生这种情况。任何建议如何解决这个问题?
logcat的:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View
android.view.View.findViewById(int)' on a null object reference
at 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);
}
在GetMethodEx()的onPostExecute()中。
以上是关于logcat错误android studio编程片段的主要内容,如果未能解决你的问题,请参考以下文章
Appium 错误:Logcat 捕获失败:使用 Android Studio 生成 ENOENT
错误记录Android Studio Logcat 报错 ( read: unexpected EOF! )
错误记录Android Studio 的 Flutter 代码界面没有 Logcat 面板 ( 2021年08月28日最新解决方案 )