2017-02-03 6 views
-3

私は最初のAndroidアプリを試してみると、本当に先に進まない問題が発生しました。私のアプリでは、ArrayListを作成します。これはオブジェクトで埋められます。各オブジェクトには4つの属性があります。今度は、各オブジェクトの各属性を固有のTextViewで記述したいと思います。しかし、私の問題があります。同じコードが通常のJavaアプリケーションで実行されていて、System.out.Print("getter method")を使用した場合、私が望む価値が得られますが、.setText("getter method")を使用すると、スクリーンショットに表示されているものが表示されます。Android TextView setText with Getter返信

ActivityClass:

public class ShowBeaconActivity extends Activity { 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_showbeacons); 
    setUpShowBeacons(); 
} 

public void setUpShowBeacons(){ 
    // 
    //Beacon 1 
    // 
    TextView beaconOneUUID = (TextView)findViewById(R.id.txtBeaconOneUUIDValue); 
    beaconOneUUID.setText(BeaconListe.getBeacon(0,"UUID")); 

    TextView beaconOneMajor = (TextView)findViewById(R.id.txtBeaconOneMajorValue); 
    beaconOneMajor.setText(BeaconListe.getBeacon(0,"Major")); 

    TextView beaconOneMinor = (TextView)findViewById(R.id.txtBeaconOneMinorValue); 
    beaconOneMinor.setText(BeaconListe.getBeacon(0,"Minor")); 

    TextView beaconOneRSSI = (TextView)findViewById(R.id.txtBeaconOneRSSIValue); 
    beaconOneRSSI.setText(BeaconListe.getBeacon(0,"RSSI")); 
    // 
    //Beacon 2 
    // 
    TextView beaconTwoUUID = (TextView)findViewById(R.id.txtBeaconTwoUUIDValue); 
    beaconTwoUUID.setText(BeaconListe.getBeacon(1,"UUID")); 
    ... 
} 

}

BeaconClass:

public class Beacon { 
private String UUID; 
private String Major; 
private String Minor; 
private int RSSI; 

public Beacon(String UUID, String Major,String Minor, int RSSI){ 
    this.UUID = UUID; 
    this.Major = Major; 
    this.Minor = Minor; 
    this.RSSI = RSSI; 
} 
// 
//Getter 
// 
public String getUUID(){ 
    return UUID; 
} 

public String getMajor(){ 
    return Major; 
} 

public String getMinor() { 
    return Minor; 
} 

public int getRSSI() { 
    return RSSI; 
} 
// 
//Setter 
// 
public void setRSSI(int RSSI){ 
    this.RSSI = RSSI; 
} 

}

BeaconListeClass

public class BeaconListe { 
private static ArrayList<Beacon>Liste = new ArrayList<Beacon>(); 
// 
//Getter 
// 
public static String getBeacon(int index, String value){ 
    String result = ""; 
    switch(value){ 
     case "UUID": result = Liste.get(index).getUUID(); 
      break; 
     case "Major": result = Liste.get(index).getMajor(); 
      break; 
     case "Minor": result = Liste.get(index).getMinor(); 
      break; 
     case "RSSI": int resultTemp = Liste.get(index).getRSSI(); 
      result = String.valueOf(resultTemp); 
      break; 
    } 
    return result; 
} 
// 
//Setter 
// 
public static void addBeacon(String UUID, String Major, String Minor, int RSSI){ 
    Liste.add(new Beacon(UUID, Major, Minor, RSSI)); 
} 

} App Screenshot

答えて

0
それが原因で、null値がクラッシュすることができます

public static String getBeacon(int index, String value) 
    { 
     String result = ""; 
     switch(value){ 
      Beacon becon = Liste.get(index); 
      case "UUID":  
       result = becon!=null?becon.getUUID():""; 
       break; 
      case "Major": 
       result = becon!=null?becon.getMajor():""; 
       break; 
      case "Minor": 
       result = becon!=null?becon.getMinor():""; 
       break; 
      case "RSSI": 
       resultTemp = becon!=null?becon.getRSSI():0; 
       result = String.valueOf(resultTemp); 
       break; 
     } 
     return result==null?"":result; 
    } 
0

してみてください。この

TextView beaconOneUUID = (TextView)findViewById(R.id.txtBeaconOneUUIDValue); 
if(BeaconListe.getBeacon(0,"UUID") != null){ 
beaconOneUUID.setText(BeaconListe.getBeacon(0,"UUID")); 
} 

    TextView beaconOneMajor = (TextView)findViewById(R.id.txtBeaconOneMajorValue); 
if(BeaconListe.getBeacon(0,"Major") != null){ 
beaconOneMajor.setText(BeaconListe.getBeacon(0,"Major")); 
} 

    TextView beaconOneMinor = (TextView)findViewById(R.id.txtBeaconOneMinorValue); 
if(BeaconListe.getBeacon(0,"Minor") != null){ 
beaconOneMinor.setText(BeaconListe.getBeacon(0,"Minor")); 
} 
    TextView beaconOneRSSI = (TextView)findViewById(R.id.txtBeaconOneRSSIValue); 
if(BeaconListe.getBeacon(0,"RSSI") != null){ 
beaconOneRSSI.setText(BeaconListe.getBeacon(0,"RSSI")); 
} 
    // 
    //Beacon 2 
    // 
    TextView beaconTwoUUID = (TextView)findViewById(R.id.txtBeaconTwoUUIDValue); 
if(BeaconListe.getBeacon(1,"UUID")){ 
beaconTwoUUID.setText(BeaconListe.getBeacon(1,"UUID")); 
} 
+0

ありがとう、あなたの答えをありがとうが、それは助けにはならない。そして、なぜそれが助けになったのか分かりません。おそらく私はそれを言及すべきだった。私のボタン「Show Beacons」をクリックすると、このアクティビティが開きます。また、少なくとも4つのビーコン(これも制限です)を私のリストに追加していないとします。だから、このウィンドウがポップアップするなら、私のリストにはTextViewごとに価値があると確信しています:) – Tobias

0

私が見るところでは、.setText("getter methods")は問題なく動作します。問題はそれが設定するテキストです。ここに表示されていないBeaconListeを入力する機能を確認してください。 私はあなたのコードを試してみましたが、次の

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_showbeacons); 
    populateBeaconListe(); 
    setUpShowBeacons(); 
} 
public void populateBeaconListe(){ 
    BeaconListe.addBeacon("B1UUID","B1MAJOR","B1MINOR",1111); 
    BeaconListe.addBeacon("B2UUID","B2MAJOR","B2MINOR",2222); 
    BeaconListe.addBeacon("B3UUID","B3MAJOR","B3MINOR",3333); 
    BeaconListe.addBeacon("B4UUID","B4MAJOR","B4MINOR",4444); 
} 

と、それだけで正常に動作します。スクリーンショットを参照screenshots

+0

私に正しい方向を教えていただきありがとうございます:)。私は私のaddbeaconメソッドが間違っているかもしれないと思っていませんでした。すべて私は "String uuid = String.valueOf((TextView)findViewById(R.id.txtUUID))"をStringに変換していました。getText()。toString(StringView.getTextUUID) ) "今はうまく動作します。 – Tobias

関連する問題