bonjour voici j ai erreur dont j arrive pas a trouvé la source :
je veux parser mes fichiers json localement dans mon disque .
voici je vous met tout ce que j ai codé et vraiment je ne sais d ou vient l erreur j ai besoin de votre aide et merci
produit:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 public class Product { public int storeId; public int catalogId; public int langId; public int productId; public int parent_category_rn; public String shouldCachePage; }
mon fichier json
ma classe parseur
Code : Sélectionner tout - Visualiser dans une fenêtre à part [{"storeId":"10101","catalogId":"10101","langId":-2,"productId":"10251","parent_category_rn":"10104","shouldCachePage":""}]
ma classe adapter
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class ParseJsonProduct { public ArrayList<Product> parse (Context context, int fichierjson ) throws JSONException { ArrayList<Product> pro = new ArrayList<Product>(); InputStream is = context.getResources().openRawResource(fichierjson); String rawJson = stream2String(is); JSONArray ob = new JSONArray(rawJson); if(!rawJson.equals("")) { for(int i=0;i<ob.length();i++) { JSONObject p = ob.getJSONObject(i); Product pr = new Product(); pr.catalogId = p.getInt("catalogId"); pr.storeId = p.getInt("storeId"); pr.langId = p.getInt("langId"); pr.productId = p.getInt("productId"); pr.parent_category_rn = p.getInt("parent_category_rn"); pr.shouldCachePage = p.getString("shouldCachePage"); pro.add(pr); } } return pro ; } private String stream2String(InputStream stream) { InputStreamReader reader = new InputStreamReader(stream); BufferedReader buffer = new BufferedReader(reader); StringBuilder sb = new StringBuilder(); try { String cur; while ((cur = buffer.readLine()) != null) { sb.append(cur); } } catch (IOException e) { e.printStackTrace(); } try { stream.close(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } }
et en fin ma classe main
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class ProductAdapter extends BaseAdapter{ private LayoutInflater mInflater; // private Product produit = new Product(); private ArrayList<Product> produits = new ArrayList<Product>() ; public ProductAdapter(Context context , ArrayList<Product> produits) { this.mInflater= LayoutInflater.from(context); this.produits=produits; } public int getCount() { // TODO Auto-generated method stub return produits.size(); } public Product getItem(int position) { // TODO Auto-generated method stub return produits.get(position); } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public static class ViewHolder { TextView txtText; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.produit, null); holder = new ViewHolder(); holder.txtText= (TextView) convertView.findViewById(R.id.txtTitle); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final Product item = produits.get(position); holder.txtText.setText(item.catalogId + " " + item.langId+ " " + item.productId +"" + item.storeId+"" + item.parent_category_rn+ "" + item.shouldCachePage+ ""); return convertView; }
et voici l erreur dont je ne sais meme je n e sais si j ai mal codé ou non j ai besoin de votre aide et merci d avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class Main extends Activity { /** Called when the activity is first created. */ ListView mlistView ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mlistView = (ListView)findViewById(R.id.txtTitle); ParseJsonProduct parser = new ParseJsonProduct(); ArrayList<Product> produit = null; try { produit =parser.parse(this,R.raw.fichierjson); } catch (JSONException e) { // Une erreur s'est produite lors du traitement du // fichier json Log.e("ParserJsonProduct", "ERREUR : " + e.getMessage()); e.printStackTrace(); } if(produit!=null) { ProductAdapter pr = new ProductAdapter(this, produit); mlistView.setAdapter(pr); mlistView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // Product p = (Product) ((ProductAdapter)mlistView.getAdapter()).getItem(arg2); // Toast.makeText(Main.this, p.productId+"",Toast.LENGTH_LONG).show(); } }); } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 04-14 09:28:18.009: ERROR/AndroidRuntime(5933): Caused by: java.lang.NullPointerException 04-14 09:28:18.009: ERROR/AndroidRuntime(5933): at com.Categorie.json.Main.onCreate(Main.java:48) 04-14 09:28:18.009: ERROR/AndroidRuntime(5933): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-14 09:28:18.009: ERROR/AndroidRuntime(5933): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
Partager