我有一个listview
,其中包含特殊ID @android:id/list
json数据只有一个包含对象的数组。
public class MainActivity extends ListActivity {
String KEY_DESCRIPT = "DESCRIPTION:";
String KEY_NAME = "NAME:";
String line = null;
JSONArray datalist=null;
JSONObject getobj=null;
BufferedReader br;
StringBuilder sb;
String Web=null;
JSONArray jresult=null;
URL url;
HttpURLConnection cn;
String TAG="MYACTIVITY";
String name = null;
String [] sortedlist;
ProgressBar loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loading= (ProgressBar) findViewById(R.id.progressBar1);
Web = "http://api.mtgdb.info/search/Dragon";
new jsonGet().execute(Web);
}
protected class jsonGet extends AsyncTask<String , Void, JSONArray>{
@Override
protected JSONArray doInBackground(String... params) {
try {
url= new URL(params[0]);
cn=(HttpURLConnection) url.openConnection();
br= new BufferedReader(new InputStreamReader(cn.getInputStream()));
sb= new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line+"\n");
}
br.close();
datalist= new JSONArray(sb.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return datalist;
}
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
jresult=result;
passList();
}
}
public void passList() {
loading.setVisibility(View.INVISIBLE);
if (jresult==null){
Log.e(TAG, "ERROR");
}else{
ArrayList<HashMap<String, String>> newlist =new ArrayList<HashMap<String,String>>();
for (int i =0;i<jresult.length();i++){
try{
getobj= jresult.getJSONObject(i);
name=getobj.getString("name");
String description =getobj.getString("description");
HashMap<String, String> secondlist= new HashMap<String, String>();
secondlist.put(KEY_NAME, name);
secondlist.put(KEY_DESCRIPT, description);
newlist.add(secondlist);
}catch (Exception e){
Log.i(TAG, "THis error");
}
}
Arrays.sort(sortedlist);
String [] keys ={KEY_NAME,KEY_DESCRIPT};
int [] ids ={android.R.id.text1,android.R.id.text2};
SimpleAdapter adapter =
new SimpleAdapter(this,
newlist, android.R.layout.simple_list_item_2, keys, ids);
setListAdapter(adapter);
}
}
堆栈跟踪:
1-06 22:49:10.019: D/AndroidRuntime(1476): Shutting down VM
11-06 22:49:10.023: W/dalvikvm(1476): threadid=1: thread exiting with uncaught exception (group=0xa4d50b20)
11-06 22:49:10.023: E/AndroidRuntime(1476): FATAL EXCEPTION: main
11-06 22:49:10.023: E/AndroidRuntime(1476): Process: com.example.mtg, PID: 1476
11-06 22:49:10.023: E/AndroidRuntime(1476): java.lang.NullPointerException
11-06 22:49:10.023: E/AndroidRuntime(1476): at java.util.ComparableTimSort.sort(ComparableTimSort.java:142)
11-06 22:49:10.023: E/AndroidRuntime(1476): at java.util.Arrays.sort(Arrays.java:1970)
11-06 22:49:10.023: E/AndroidRuntime(1476): at com.example.mtg.MainActivity.passList(MainActivity.java:122)
11-06 22:49:10.023: E/AndroidRuntime(1476): at com.example.mtg.MainActivity$jsonGet.onPostExecute(MainActivity.java:91)
11-06 22:49:10.023: E/AndroidRuntime(1476): at com.example.mtg.MainActivity$jsonGet.onPostExecute(MainActivity.java:1)
11-06 22:49:10.023: E/AndroidRuntime(1476): at android.os.AsyncTask.finish(AsyncTask.java:632)
11-06 22:49:10.023: E/AndroidRuntime(1476): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-06 22:49:10.023: E/AndroidRuntime(1476): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-06 22:49:10.023: E/AndroidRuntime(1476): at android.os.Handler.dispatchMessage(Handler.java:102)
11-06 22:49:10.023: E/AndroidRuntime(1476): at android.os.Looper.loop(Looper.java:136)
11-06 22:49:10.023: E/AndroidRuntime(1476): at android.app.ActivityThread.main(ActivityThread.java:5001)
11-06 22:49:10.023: E/AndroidRuntime(1476): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 22:49:10.023: E/AndroidRuntime(1476): at java.lang.reflect.Method.invoke(Method.java:515)
11-06 22:49:10.023: E/AndroidRuntime(1476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-06 22:49:10.023: E/AndroidRuntime(1476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-06 22:49:10.023: E/AndroidRuntime(1476): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您的例外发生在这里:
Arrays.sort(sortedlist);
问题是,您将该变量声明为String [] sortedlist;
,但您从未使用数组初始化该变量(即sortedlist = new String[x];
)。这意味着sortedlist
仍为null
(这是新对象类型的标准值),这会导致NullPointerException
。
检查您为什么不这样做并更新/修复您的代码。
请注意,禁止将null
元素写入该数组。这意味着,如果您致电String[] sortedlist = {"a", null, "b"};
,数组NullPointerException
也会产生Arrays.sort(sortedlist)
。