【发布时间】:2017-09-11 02:08:33
【问题描述】:
我正在尝试制作一个包含三个片段的 TabLayout (ViewPager) 应用程序。在其中一个选项卡中,我想显示一个 json 解析的自定义列表视图。自定义列表视图和选项卡布局可以单独工作(教程中的代码)。当我将这两者结合起来时,应用程序立即崩溃,我无法弄清楚出了什么问题。希望大家帮忙
谢谢 Sam_0829....你救了我
问题是 1.Getmessages必须在Fragment中,而不是MainActivity
-
调用 onViewCreated 时视图为空,所以我声明了一个私有视图变量
私人视图 v;
//然后onCreate
查看 v = inflater.inflate(R.layout.message_tab, container, 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();
}
}
消息片段布局
<?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>
【问题讨论】:
-
发布您的崩溃报告
-
使用 adb...log cat 没有任何与崩溃相关的内容..
标签: android listview android-viewpager android-tablayout android-json