- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想制作一个从本地 JSON 文件获取数据并将其放入自定义 ExpandableListView 的应用程序。这是我的代码:
父类:
public class Parent {
private String name;
private ArrayList<ChildData> child;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<ChildData> getChild() {
return child;
}
public void setChild(ArrayList<ChildData> child) {
this.child = child;
}
}
ChildData 类:
public class ChildData {
private String opis1;
private String opis2;
private String img_id;
public String getOpis1() {
return opis1;
}
public void setOpis1(String opis1) {
this.opis1 = opis1;
}
public String getOpis2() {
return opis2;
}
public void setOpis2(String opis2) {
this.opis2 = opis2;
}
public String getImg_id() {
return img_id;
}
public void setImg_id(String img_id) {
this.img_id = img_id;
}
}
MainActivity 类:
public class MainActivity extends ExpandableListActivity {
private ArrayList<Parent> parents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getExpandableListView().setGroupIndicator(null);
getExpandableListView().setDividerHeight(1);
registerForContextMenu(getExpandableListView());
//Creating static data in arraylist
final ArrayList<Parent> dataList = putDataIntoArrays();
// Adding ArrayList data to ExpandableListView values
loadHosts(dataList);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("data.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
public ArrayList<Parent> putDataIntoArrays() {
JSONObject obj = null;
ArrayList<Parent> list = new ArrayList<>();
Parent parent = new Parent();
ChildData cd = new ChildData();
try {
obj = new JSONObject(loadJSONFromAsset());
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONArray m_jArry = obj.getJSONArray("data");
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
parent.setName(jo_inside.getString("title"));
Log.d("Test", "Parent name: " + parent.getName());
cd.setOpis1(jo_inside.getString("desc"));
cd.setOpis2(jo_inside.getString("desc2"));
cd.setImg_id(jo_inside.getString("img"));
Log.d("Test", "O1: " + cd.getOpis1());
Log.d("Test", "O2: " + cd.getOpis2());
Log.d("Test", "IMG: " + cd.getImg_id());
parent.getChild().add(cd);
Log.d("Test", "Parent: " + parent.getChild());
list.add(parent);
Log.d("Test", "List: " + list.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
private void loadHosts(final ArrayList<Parent> newParents)
{
if (newParents == null)
return;
parents = newParents;
// Check for ExpandableListAdapter object
if (this.getExpandableListAdapter() == null)
{
//Create ExpandableListAdapter Object
final ExpandableListAdapter mAdapter = new ExpandableListAdapter();
// Set Adapter to ExpandableList Adapter
this.setListAdapter(mAdapter);
}
else
{
// Refresh ExpandableListView data
((ExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();
}
}
}
和data.json
{
"data": [
{
"title": "T1",
"desc": "D1",
"desc2": "opis1",
"img": "1"
},
{
"title": "T2",
"desc": "D2",
"desc2": "opis2",
"img": "2"
},
{
"title": "T3",
"desc": "D3",
"desc2": "opis3",
"img": "3"
},
{
"title": "T4",
"desc": "D4",
"desc2": "opis4",
"img": "1"
}
]
}
此外,当我运行我的应用程序时,我没有从日志中获得任何结果:
Log.d("Test", "Rodzic1: " + parent.getChild());
Log.d("Test", "List: " + list.get(i));
最佳答案
您没有在您的 Parent
类中初始化此 ArrayList。
private ArrayList<ChildData> child;
在您的 Parent
构造函数中,您应该添加:
child = new ArrayList<ChildData>();
我猜这就是您的 NullPointerException
的来源。
关于java - Android - 来自 Assets 和 NullPointerException 的 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30829792/
我在我的应用程序中使用rails 4 和 ruby 1.9.3 以及fancybox2-rails gem,但是 Assets 管道存在一个普遍问题。如果我运行 rake task 进行预编译,那
我把我所有的 Assets 都放在我的包里例如myBundle/Resources/public/css/ 然后像这样加载它们: {% block stylesheets %} {{ parent()
我正在创建RoR-6应用程序,并从此行的application.html.erb文件中引发以下错误: javascript_include_tag 'application', 'data-turbo
我正在对 Play Subproject 功能进行更多扩展测试,如下所述:http://www.playframework.com/documentation/2.0/SBTSubProjects .
我是 symfony 2 和 Assetic 的新手。我想在 CSS 中使用 assetic 和 Sass。我使用自定义字体。我在资源下的包中创建文件夹“assets/css”,里面有 _base.s
这是我的目录结构(这只是一个测试项目): stan@mypc:/generate_assets$ sudo tree -L 3 -p . ├── [drwxr-xr-x] assets │ └─
我使用 node.js connect/express。 有人知道支持 TypeScript 源代码即时编译和缩小的 Assets 管理器吗? 知道如何以编程方式调用编译器吗? 我一直在使用 conn
因此,我们在 Assets 管道摘要方面遇到了一个小问题,因为我们有许多 HTML 模板作为 Assets (对于 AngularJS),并且我们使用 asset_path 在 Javascript
我想从iPad的照片库中获取图像URL。 当我尝试从Image Piicker的信息中获取 UIImagePickerControllerReferenceURL 时 我将URL设为: assets
我正在使用带有Assetic的Symfony 2.1.10版,并且在上次 Composer 更新之后,当我尝试运行php app/console assetic:dump时出现以下错误 Dumping
我的 Assets 管道有问题,我已经有一个名为 Assets 的资源/ Controller 。所以我已将 assets.prefix 选项更改为“/externals”。 config.asset
更新到 Expo sdk 34 后,出现此错误:TypeError:Asset.fromModule 不是函数。 (在“Asset.fromModule(image)”中,“Asset.fromMod
将 Play 框架项目从 2.2.4 迁移到 2.3.8 后遇到问题: 这是运行命令“activatorcompile”的错误输出: [error] ...\workspace\testproject
我在将 Google Analytics 添加到我的 Rails 4 应用程序时遇到了一些困难(参见 this post)。 我通过在 /public/assets/google-analytics.
我正在使用 Symfony2 和 Assetic。最近我一直在做很多 CSS 工作,所以在某个时候我需要命令 $ php app/console assetic:dump --env=prod --n
我正在 Android 开发中迈出第一步,并遇到了 Assets 一词。 据我了解, Assets 只是一个未被解析或引用为资源的文件。 这个词有更准确的定义吗?我应该在哪些情况下在我的应用程序中使用
我添加了 danial-farid--angular-file-upload通过 Rails Assets 将库添加到我的 Rails 4 应用程序。主库加载正常,但 sprockets 找不到它包含
这是我的代码,尽管编码非常粗糙: public void loadStack(AssetManager manager, String path) { String[] lis
我正在使用 Assets 管理我的 Symfony 2 框架中的 CSS 文件。它在生产模式下运行良好。 我的问题是在 Debug模式下,assetic 不断将我的文件合并为一个输出文件。这使得跟踪特
我想以编程方式从重复的相册中删除 Assets (照片不是)。我可以使用照片框架删除相册 我想知道如何从相册中删除 Assets 而不从照片应用中完全删除它。我想在多个地方使用它,例如将 Assets
我是一名优秀的程序员,十分优秀!