2017-02-19 101 views
-1

我有一個問題,我不知道如何通過我的setter和getter將我的json解析放在textView上。 我知道如何把我的json放在listView上,我顯示他但不是在TextView上顯示我的代碼。Android Studio解析TextView

該類代表我的佈局,列表視圖,但我把文本視圖測試

public class AfficheurListView extends AppCompatActivity { 
String json_string; 
JSONObject jObj = null; 
ValeursAdapter valeursAdapter; 
TextView textView; 
Valeurs valeurss; 
ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.display_listview_layout); 
    json_string=getIntent().getExtras().getString("json_data"); 

    listView =(ListView)findViewById(R.id.listview); 
    textView=(TextView)findViewById(R.id.textView4); 

    valeursAdapter=new ValeursAdapter(this,R.layout.new_layout); 

    listView.setAdapter(valeursAdapter); 
    textView.setText(valeurss.getMpx()); 

    try { 
     /* 
      Tab json [] 
     jsonObject=new JSONObject(json_string); 
     jsonArray=jsonObject.getJSONArray("server_reponse"); 
     int count=0 ;*/ 

     // Json Object {} 
     String mpx,rds,al,ar,frequence,pilots,id,id_SIGFOX,timestamps,rf; 
     jObj = new JSONObject(json_string); 
     mpx= jObj.getString("MPX"); 
     rds =jObj.getString("RDS"); 
     rf=jObj.getString("RF"); 
     frequence =jObj.getString("Frequence"); 
     timestamps=jObj.getString("timestamp"); 
     id= jObj.getString("id"); 
     id_SIGFOX= jObj.getString("id_SIGFOX"); 
     pilots= jObj.getString("PILOT"); 
     al= jObj.getString("a_l"); 
     ar= jObj.getString("a_r"); 

     Valeurs valeurs=new Valeurs(mpx,rds,al,ar,frequence,pilots,id,timestamps,id_SIGFOX,rf); 
     valeursAdapter.add(valeurs); 
     //textView.setText(valeurss.getMpx()); 


    } catch (JSONException e) { 

     e.printStackTrace();} 

} 
} 

這個類是我的變量的getter和setter:

public class Valeurs{ 


    private String mpx,rds,al,ar,pilots,frequence,id,timestamps,id_SIGFOX,rf; 
     public Valeurs(String mpx, String rds, String al, String ar, String pilots, String frequence, String id, String timestamps, String id_SIGFOX, String rf) 

     { 
      this.setMpx(mpx); 
      this.setRds(rds); 
      this.setAl(al); 
      this.setAr(ar); 
      this.setPilots(pilots); 
      this.setFrequence(frequence); 
      this.setId(id); 
      this.setTimestamps(timestamps); 
      this.setId_SIGFOX(id_SIGFOX); 
      this.setRf(rf); 


     } 


    public String getMpx() { 
     return mpx; 
    } 

    public void setMpx(String mpx) { 
     this.mpx = mpx; 
    } 

    public String getRds() { 
     return rds; 
    } 

    public void setRds(String rds) { 
     this.rds = rds; 
    } 

    public String getAl() { 
     return al; 
    } 

    public void setAl(String al) { 
     this.al = al; 
    } 

    public String getAr() { 
     return ar; 
    } 

    public void setAr(String ar) { 
     this.ar = ar; 
    } 

    public String getPilots() { 
     return pilots; 
    } 

    public void setPilots(String pilots) { 
     this.pilots = pilots; 
    } 

    public String getFrequence() { 
     return frequence; 
    } 

    public void setFrequence(String frequence) { 
     this.frequence = frequence; 
    } 

    public String getTimestamps() { 
     return timestamps; 
    } 

    public void setTimestamps(String timestamps) { 
     this.timestamps = timestamps; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getId_SIGFOX() { 
     return id_SIGFOX; 
    } 

    public void setId_SIGFOX(String id_SIGFOX) { 
     this.id_SIGFOX = id_SIGFOX; 
    } 

    public String getRf() { 
     return rf; 
    } 

    public void setRf(String rf) { 
     this.rf = rf; 
    } 
} 

該類把我的變量上我的lisView的新佈局:

public class ValeursAdapter extends ArrayAdapter{ 

    List list=new ArrayList<>(); 
    public ValeursAdapter(Context context, int resource) { 
     super(context, resource); 
    } 


    public void add(Valeurs object) { 
     super.add(object); 
     list.add(object); 


    } 

    @Override 
    public int getCount() { 

     return list.size(); 

    } 

    @Nullable 
    @Override 
    public Object getItem(int position) { 
     return list.get(position); 

    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row; 
     row=convertView; 

     ValeursDefini valeursDefini; 
     if(row==null) 
     { 
      LayoutInflater layoutInflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row=layoutInflater.inflate(R.layout.new_layout,parent,false); 
      valeursDefini=new ValeursDefini(); 
      valeursDefini.tx_mpx=(TextView)row.findViewById(R.id.tx_mpx); 
      valeursDefini.tx_al=(TextView)row.findViewById(R.id.tx_al); 
      valeursDefini.tx_ar=(TextView)row.findViewById(R.id.tx_ar); 
      valeursDefini.tx_rds=(TextView)row.findViewById(R.id.tx_rds); 
      valeursDefini.tx_frequence=(TextView)row.findViewById(R.id.tx_frequence); 
      valeursDefini.tx_pilots=(TextView)row.findViewById(R.id.tx_pilots); 

      row.setTag(valeursDefini); 

     } 
      else { 
     valeursDefini=(ValeursDefini)row.getTag(); 
     } 
     Valeurs valeurs= (Valeurs) this.getItem(position); 
     valeursDefini.tx_mpx.setText(valeurs.getMpx()+"--"); 
     valeursDefini.tx_rds.setText(valeurs.getRds()+"--"); 
     valeursDefini.tx_al.setText(valeurs.getAl()+"--"); 
     valeursDefini.tx_ar.setText(valeurs.getAr()+"--"); 
     valeursDefini.tx_pilots.setText(valeurs.getPilots()+"--"); 
     valeursDefini.tx_frequence.setText(valeurs.getFrequence()+"--"); 

     return row; 
    } 
    static class ValeursDefini 
    { 
     TextView tx_mpx,tx_rds,tx_al,tx_ar,tx_frequence,tx_pilots; 

    } 
} 

日誌:

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.suprem.myapplication, PID: 2288 
       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.suprem.myapplication/com.suprem.myapplication.AfficheurListView}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.suprem.myapplication.Valeurs.getMpx()' on a null object reference 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.suprem.myapplication.Valeurs.getMpx()' on a null object reference 
        at com.suprem.myapplication.AfficheurListView$override.onCreate(AfficheurListView.java:36) 
        at com.suprem.myapplication.AfficheurListView$override.access$dispatch(AfficheurListView.java) 
        at com.suprem.myapplication.AfficheurListView.onCreate(AfficheurListView.java:0) 
        at android.app.Activity.performCreate(Activity.java:5990) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  
        at android.app.ActivityThread.access$800(ActivityThread.java:151)  
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  
        at android.os.Handler.dispatchMessage(Handler.java:102)  
        at android.os.Looper.loop(Looper.java:135)  
        at android.app.ActivityThread.main(ActivityThread.java:5254)  
        at java.lang.reflect.Method.invoke(Native Method)  
        at java.lang.reflect.Method.invoke(Method.java:372)  
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  

我想在textView4上顯示我的變量mpx,通過使用我的類Valeur獲得mpx在getter 上的值。

回答

1

您沒有初始化Valeurs valeurss;

相關問題