Android ViewPager 框架列表视图崩溃
Android ViewPager Frame Listview Crash
我正在尝试制作一个包含三个片段的 TabLayout (ViewPager) 应用程序。在其中一个选项卡中,我想显示一个 json 已解析的自定义列表视图。自定义列表视图和选项卡布局单独工作正常(教程中的代码)。当我将这两者结合起来时,应用程序立即崩溃,我不知道出了什么问题。希望大家帮忙
谢谢你Sam_0829..你救了我
问题是
1.Getmessages不能在MainActivity中,必须在Fragment中
调用onViewCreated时View为空,所以我声明了一个私有视图变量
私有视图 v;
//然后onCreate
视图 v = inflater.inflate(R.layout.message_tab, 容器, false);
并将其传递给
onViewCreated public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
contactList = new ArrayList<>();
lv = (ListView) v.findViewById(R.id.list);
new Getmessages().execute();
}
生活变得精彩
谢谢山姆,你教我通过日志解决问题...这是我以前从不信任的东西
消息片段(已解决的代码)->
package com.example.user.newton;
public class MessageTab extends Fragment {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
private View v;
// URL to get messages JSON
private static String url = "http://myjson.com/17oyz5";
ArrayList<HashMap<String, String>> contactList;
private MainActivity.SectionsPagerAdapter mSectionsPagerAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.message_tab, container, false);
return v;
}
private class Getmessages extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray messages = jsonObj.getJSONArray("messages");
// looping through All messages
for (int i = 0; i < messages.length(); i++) {
JSONObject c = messages.getJSONObject(i);
String message= c.getString("message");
String sender= c.getString("sender");
String mtime= c.getString("time");
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("message", message);
contact.put("sender", sender);
contact.put("mtime", mtime);
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), contactList,
R.layout.custom_layout, new String[]{"message", "sender",
"mtime"}, new int[]{R.id.name,
R.id.email, R.id.mobile});
lv.setAdapter(adapter);
}
}
@Override
public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
contactList = new ArrayList<>();
lv = (ListView) v.findViewById(R.id.list);
new Getmessages().execute();
}
}
MessageFragment 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
您似乎试图在错误的位置更新 ListView
内容:您在 MainActivity
中将 ListView
引用为 lv
,但是它需要在您的 MessageFragment
中更新。您应该将 Getmessages
移动到(例如)MessageFragment
的 onCreateView
。
请更改
pDialog = new ProgressDialog(getApplicationContext());
至
pDialog = new ProgressDialog(MainActivity.this);
&试试。
if (lv!= null) {
lv.setAdapter(适配器)
}
我正在尝试制作一个包含三个片段的 TabLayout (ViewPager) 应用程序。在其中一个选项卡中,我想显示一个 json 已解析的自定义列表视图。自定义列表视图和选项卡布局单独工作正常(教程中的代码)。当我将这两者结合起来时,应用程序立即崩溃,我不知道出了什么问题。希望大家帮忙
谢谢你Sam_0829..你救了我
问题是 1.Getmessages不能在MainActivity中,必须在Fragment中
调用onViewCreated时View为空,所以我声明了一个私有视图变量
私有视图 v;
//然后onCreate
视图 v = inflater.inflate(R.layout.message_tab, 容器, false);
并将其传递给
onViewCreated public void onViewCreated(View v, @Nullable Bundle savedInstanceState) { super.onViewCreated(v, savedInstanceState); contactList = new ArrayList<>();
lv = (ListView) v.findViewById(R.id.list);
new Getmessages().execute();
}
生活变得精彩
谢谢山姆,你教我通过日志解决问题...这是我以前从不信任的东西
消息片段(已解决的代码)->
package com.example.user.newton;
public class MessageTab extends Fragment {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
private View v;
// URL to get messages JSON
private static String url = "http://myjson.com/17oyz5";
ArrayList<HashMap<String, String>> contactList;
private MainActivity.SectionsPagerAdapter mSectionsPagerAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.message_tab, container, false);
return v;
}
private class Getmessages extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray messages = jsonObj.getJSONArray("messages");
// looping through All messages
for (int i = 0; i < messages.length(); i++) {
JSONObject c = messages.getJSONObject(i);
String message= c.getString("message");
String sender= c.getString("sender");
String mtime= c.getString("time");
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("message", message);
contact.put("sender", sender);
contact.put("mtime", mtime);
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), contactList,
R.layout.custom_layout, new String[]{"message", "sender",
"mtime"}, new int[]{R.id.name,
R.id.email, R.id.mobile});
lv.setAdapter(adapter);
}
}
@Override
public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
contactList = new ArrayList<>();
lv = (ListView) v.findViewById(R.id.list);
new Getmessages().execute();
}
}
MessageFragment 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
您似乎试图在错误的位置更新 ListView
内容:您在 MainActivity
中将 ListView
引用为 lv
,但是它需要在您的 MessageFragment
中更新。您应该将 Getmessages
移动到(例如)MessageFragment
的 onCreateView
。
请更改
pDialog = new ProgressDialog(getApplicationContext());
至
pDialog = new ProgressDialog(MainActivity.this);
&试试。
if (lv!= null) {
lv.setAdapter(适配器)
}