2017-02-11 16 views
-1

をスーパークラスのデータメンバにアクセスできません。 は、それから私はShowPairedDevicesを拡張し、同じ活動にデモという名前の別のクラスを作成しました。私はすべてのBluetoothペアリングデバイスを表示する<strong>ShowPairedDevices</strong>という名前のスーパークラスを持っている<strong>ShowPairedDevices.java</strong>という名前のアクティビティを作成したのと同じ活動にサブクラスで

public class ShowPairedDevices extends AppCompatActivity { 
ArrayList<String> pairedDevicesList = new ArrayList<>(); 
public static ArrayAdapter<String> storePairedDevices; 
BluetoothAdapter myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show_paired_devices); 

    ListView myListView = (ListView) findViewById(R.id.listview); 
    Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices(); 
    for(BluetoothDevice device : pairedDevices){ 
     pairedDevicesList.add(device.getName()); 
    } 

    storePairedDevices = new ArrayAdapter< (this,android.R.layout.simple_list_item_1,pairedDevicesList); 
    myListView.setAdapter(storePairedDevices); 
} 

}

それから私は、サブクラスでpairedDevicesListの名前、それはit.Iもサブクラスでスーパークラスのオブジェクトを作成しようとしましたし、その後を通じて、スーパークラスのメンバーを呼び出す解決することはできませんスーパークラスのArrayListのにアクセスしようそのオブジェクト、しかしそれも動作しません。助けてください。ここ
は、サブクラスのコードは次のとおりです。

class Demo extends ShowPairedDevices{ 
    pairedDevicesList.add("another device name"); 
} 

答えて

2

「デモ」クラスには意味がありません。

あなたは、単にクラススコープでそのような変数にアクセスすることはできません。
は機能で包み、または現在の関数をオーバーライドし、そのようなデータへのアクセス:

public class Demo extends ShowPairedDevices { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     pairedDevicesList.add("another device name"); 
    } 
} 
+0

Moonbloomありがとうございました。 –

0

私はあなたがShowPairedDevicesクラスの異なるパッケージであなたの子クラスを配置していると思います。あなたはArrayListのpairedDevicesListために保護されたキーワードを使用する必要があり、その場合には

。これはJavaのアクセス修飾子と呼ばれます。ここではいくつかの点でhttps://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

を参照するための便利なリンク、あなたは、変数や関数を再利用するために、この実装が必要になります。

関連する問題