Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Android] Listview and SimpleAdapter

Ca plante :(

Sujet résolu
    12 octobre 2012 à 12:18:35

    Salut tout les Androidiens,

    Quand j’exécute ce code j'ai ceci comme erreurs:


    10-12 12:09:55.824: I/test(670): {id=1, vote=50}
    10-12 12:09:55.834: W/dalvikvm(670): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
    10-12 12:09:55.834: E/AndroidRuntime(670): Uncaught handler: thread main exiting due to uncaught exception
    10-12 12:09:55.844: E/AndroidRuntime(670): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teatowermobile/com.example.teatowermobile.MainActivity}: java.lang.NullPointerException
    10-12 12:09:55.844: E/AndroidRuntime(670):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at android.os.Handler.dispatchMessage(Handler.java:99)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at android.os.Looper.loop(Looper.java:123)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at android.app.ActivityThread.main(ActivityThread.java:4363)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at java.lang.reflect.Method.invokeNative(Native Method)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at java.lang.reflect.Method.invoke(Method.java:521)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at dalvik.system.NativeStart.main(Native Method)
    10-12 12:09:55.844: E/AndroidRuntime(670): Caused by: java.lang.NullPointerException
    10-12 12:09:55.844: E/AndroidRuntime(670):         at com.example.teatowermobile.MainActivity.onCreate(MainActivity.java:96)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    10-12 12:09:55.844: E/AndroidRuntime(670):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    10-12 12:09:55.844: E/AndroidRuntime(670):         ... 11 more
    10-12 12:09:55.874: I/dalvikvm(670): threadid=7: reacting to signal 3
    10-12 12:09:55.874: E/dalvikvm(670): Unable to open stack trace file '/data/anr/traces.txt': Permission denied


    XML de base

    <secret><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
    <ImageView 
        android:id="@+id/logo"
        android:src="@drawable/logo_final_color" 
        android:contentDescription="@string/logo"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
    ></ImageView>
    
    <TextView 
        android:id="@+id/welcome_message"
        android:text="@string/welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/logo"
        android:textColor="#60851d"
        android:layout_marginTop="25sp"
        android:layout_centerHorizontal="true"
        android:textSize="20sp"
        android:textStyle="bold"
    ></TextView>
    <TextView 
        android:id="@+id/explanation1"
        android:text="@string/explanation1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/welcome_message"
        android:textColor="#000000"
        android:layout_centerHorizontal="true"
        android:textSize="10sp"
        android:textStyle="bold"
    ></TextView>
    <TextView 
        android:id="@+id/explanation2"
        android:text="@string/explanation2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/explanation1"
        android:textColor="#60851d"
        android:layout_marginTop="30sp"
        android:layout_centerHorizontal="true"
        android:textSize="12sp"
        android:textStyle="bold"
    ></TextView>
    <LinearLayout 	
        android:id="@+id/linear"
        android:layout_below="@id/explanation2"
    	android:layout_centerHorizontal="true"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:layout_marginTop="10sp"
    >
    
    <EditText 
    	android:id="@+id/edit_t_code"
    	android:hint="@string/texthint"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:singleLine="true"
    	android:layout_weight="20"
    	android:layout_marginLeft="5sp"
    ></EditText>
    
    <Button 
    	android:id="@+id/button_done"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:text="@string/done"
    	android:layout_weight="80"
    	android:layout_marginTop="-2sp"
    ></Button>
    
    </LinearLayout>
    <TextView 
        android:id="@+id/explanation3"
        android:text="@string/explanation3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_below="@id/linear"
        android:textSize="8sp"
        android:textStyle="bold"
    	android:layout_marginLeft="5sp"
    ></TextView>
     
     <!-- List Divider -->
    <View
        android:layout_width="fill_parent"
    	android:layout_height="1dip"
    	android:background="?android:attr/listDivider" 
    />
    
    <ListView 
    	android:id="@+id/list"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_below="@id/explanation3"
    ></ListView>
    
    </RelativeLayout>
    


    XML de mon Listview

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        
    
        <TextView android:id="@+id/id"
    	         android:layout_width="fill_parent"
    	         android:layout_height="fill_parent"
    	         android:textSize="10sp"
    	         android:textStyle="bold"
    	         />
     
    	<TextView android:id="@+id/vote"
    	         android:layout_width="fill_parent"
    	         android:layout_height="fill_parent"
    	/>
        
    </LinearLayout>
    


    Mon fichier Java

    public class MainActivity extends Activity {
    
    	ImageView logo ;
    	JSONArray jArray;
    	String result = null;
    	InputStream is = null;
    	StringBuilder sb=null;
    	ListView top;
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            logo = (ImageView) findViewById(R.drawable.logo_final_color);
            top = (ListView) findViewById(R.id.list);
            
            setContentView(R.layout.activity_main);
            
          //http post
          try{
               HttpClient httpclient = new DefaultHttpClient();
               HttpPost httppost = new HttpPost("adresse^^");
               HttpResponse response = httpclient.execute(httppost);
               HttpEntity entity = response.getEntity();
               is = entity.getContent();
               }catch(Exception e){
                   Log.e("log_tag", "Error in http connection"+e.toString());
              }
          //convert response to string
          try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                 sb = new StringBuilder();
                 sb.append(reader.readLine() + "\n");
    
                 String line="0";
                 while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                  }
                  is.close();
                  result=sb.toString();
                  }catch(Exception e){
                        Log.e("log_tag", "Error converting result "+e.toString());
                  }
          //paring data
          int pr_id;
          int pr_vote;
          List<HashMap<String,String>> liste = new  ArrayList<HashMap<String, String>>();
          HashMap<String, String> element;
          try{
                jArray = new JSONArray(result);
                JSONObject json_data=null;
                for(int i=0;i<jArray.length();i++){
                       json_data = jArray.getJSONObject(i);
                       //Gives 1 back
                       pr_id=json_data.getInt("id_product");
                       //Gives 50 back
                       pr_vote=json_data.getInt("vote");
                       element = new HashMap<String, String>();
                       element.put("id", Integer.toString(pr_id));
                       element.put("vote", Integer.toString(pr_vote));
                       liste.add(element);
                   }
                Log.i("test", liste.get(0).toString());
          		ListAdapter adapter = new SimpleAdapter(this.getBaseContext(),liste,R.layout.affichageitem,new String[] {"id", "vote"},
        		new int[] {R.id.id, R.id.vote });
         
         		if (adapter != null) {
            		top.setAdapter(adapter);
    			}
                }
                catch(JSONException e1){
              	  Toast.makeText(getBaseContext(), "No product found" ,Toast.LENGTH_LONG).show();
                } catch (ParseException e2) {
                	Log.e("log_tag", "Parse exception "+e2.toString());
          	}
          }
    }
    


    Je ne m'en sors plus alors je demande de l'aide.

    Merci.

    [EDIT] PROBLEM SOLVED!
    • Partager sur Facebook
    • Partager sur Twitter

    [Android] Listview and SimpleAdapter

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown