我从MySQL获取数据并使用php以json格式显示,现在我想在Android中显示此数据 我获取数据并保存在" myJSON"变量并使用此代码显示数据
String myJSON;
private static final String TAG_NEWS="allnews";
private static final String TAG_ID = "id";
private static final String TAG_ONVAN = "Onvan";
private static final String TAG_TARIKH ="Tarikh";
private static final String TAG_CNID ="cnid";
JSONArray jnews = null;
ArrayList<HashMap<String, String>> newsList;
ListView list;
和
protected void SaveNewNews(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
jnews = jsonObj.getJSONArray(TAG_NEWS);
Log.i(DBAdapter.TAG, "news " + jnews.length());
for(int i=0;i<jnews.length();i++){
JSONObject c = jnews.getJSONObject(i);
String id = c.getString(TAG_ID);
String title = c.getString(TAG_ONVAN);
String data = c.getString(TAG_TARIKH);
String nid = c.getString(TAG_CNID);
Log.i(DBAdapter.TAG, id);
HashMap<String,String> nnews = new HashMap<String,String>();
nnews.put(TAG_ID,id);
nnews.put(TAG_ONVAN,title);
nnews.put(TAG_TARIKH,data);
nnews.put(TAG_CNID,nid);
newsList.add(nnews);
ListAdapter adapter = new SimpleAdapter(
SplashActivity.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_ONVAN,TAG_TARIKH,TAG_CNID},
new int[]{R.id.id, R.id.title, R.id.data, R.id.nid}
);
list.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
gonext();
}
在日志中,您可以看到它获取数据列表news 5
并获得第一个ID 35
,但应用停止并且不执行任何操作
07-08 09:36:43.456: I/Ekhtraat app(1333): news 5
07-08 09:36:43.456: I/Ekhtraat app(1333): 35
07-08 09:36:43.476: W/System.err(1333): java.lang.NullPointerException
07-08 09:36:43.486: W/System.err(1333): at ir.sheikhoo.ekhteraat_app.GetActivity.SaveNewNews(GetActivity.java:315)
07-08 09:36:43.496: W/System.err(1333): at ir.sheikhoo.ekhteraat_app.GetActivity$GetData.onPostExecute(GetActivity.java:256)
07-08 09:36:43.496: W/System.err(1333): at ir.sheikhoo.ekhteraat_app.GetActivity$GetData.onPostExecute(GetActivity.java:1)
07-08 09:36:43.496: W/System.err(1333): at android.os.AsyncTask.finish(AsyncTask.java:631)
07-08 09:36:43.507: W/System.err(1333): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-08 09:36:43.507: W/System.err(1333): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-08 09:36:43.507: W/System.err(1333): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 09:36:43.507: W/System.err(1333): at android.os.Looper.loop(Looper.java:137)
07-08 09:36:43.516: W/System.err(1333): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-08 09:36:43.516: W/System.err(1333): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 09:36:43.516: W/System.err(1333): at java.lang.reflect.Method.invoke(Method.java:525)
07-08 09:36:43.526: W/System.err(1333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-08 09:36:43.526: W/System.err(1333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-08 09:36:43.526: W/System.err(1333): at dalvik.system.NativeStart.main(Native Method)
07-08 09:36:43.536: I/Choreographer(1333): Skipped 168 frames! The application may be doing too much work on its main thread.
请你检查一下哪里出错了。
答案 0 :(得分:1)
看起来你没有初始化newsList
。
在newsList = new ArrayList<>();
行中添加并查看它的位置。 (在SaveNewNews()
方法的开头尝试)。
或者将行ArrayList<HashMap<String, String>> newsList;
更改为也初始化,即ArrayList<HashMap<String, String>> newsList = new ArrayList<>();