在此代码中,第一个红色交通信号灯显示,但单击按钮时不会更改。是的,我知道有类似这样的问题,但它们似乎没有帮助。所以,如果你能告诉我什么是错的,我会非常感激。
package bluetoothscanneractivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.Manifest;
import java.util.ArrayList;
public class BluetoothScannerActivity extends AppCompatActivity {
//private final BroadcastReceiver FoundReceiver = null;
protected ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>();
private ListView foundDevicesListView;
private ArrayAdapter<BluetoothDevice> btArrayAdapter;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_scanner);
final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
final Button scanb = (Button) findViewById(R.id.button);
final ListView foundDevicesListView = (ListView) findViewById(R.id.listView1);
btArrayAdapter = new ArrayAdapter<BluetoothDevice>(this,
android.R.layout.simple_list_item_1, foundDevices);
foundDevicesListView.setAdapter(btArrayAdapter);
//Turn on Bluetooth
if (myBlueToothAdapter == null)
Toast.makeText(BluetoothScannerActivity.this, "Your device doesnt support Bluetooth", Toast.LENGTH_LONG).show();
else if (!myBlueToothAdapter.isEnabled()) {
Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(BtIntent, 0);
Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show();
}
// Quick permission check
int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
if (permissionCheck != 0) {
this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
}
scanb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
btArrayAdapter.clear();
myBlueToothAdapter.startDiscovery();
Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show();
}
});
registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
IntentFilter filter = new IntentFilter(
BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(FoundReceiver, filter);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(FoundReceiver);
}
private final BroadcastReceiver FoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a new device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (!foundDevices.contains(device)) {
foundDevices.add(device);
Toast.makeText(BluetoothScannerActivity.this, "name: " + device.getName() + " " + device.getAddress(), Toast.LENGTH_LONG).show();
btArrayAdapter.notifyDataSetChanged();
}
}
// When discovery cycle finished
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
if (foundDevices == null || foundDevices.isEmpty()) {
Toast.makeText(BluetoothScannerActivity.this, "No Devices", Toast.LENGTH_LONG).show();
}
}
}
};
}
答案 0 :(得分:1)
我在答案中添加了评论以显示更改。
changeLights()
功能。index+!
似乎是拼写错误,请使用index++
添加1。
var list = [
"http://imgur.com/FHT9OoG.jpg",
"http://imgur.com/XZ1PxGh.jpg",
"http://imgur.com/5DUX0Wy.jpg",
"http://imgur.com/WpeEMZU.jpg"
];
var index = 0;
function changeLights() {
index++ // Add 1 to index.
// If index is more than the array length, reset to 0
// Or you can use: if(index == list.length){
if (index > list.length-1){
index = 0;
}
var image = document.getElementById("light");
image.src=list[index];
}
<img id = "light" src="http://imgur.com/FHT9OoG.jpg">
<button type="button" onclick="changeLights()">Change Lights</button>
如果您有任何疑问,请在下面留言,我会尽快给您回复。
我希望这会有所帮助。快乐的编码!
答案 1 :(得分:0)
您的意思是在changeLights()
内将索引增加1吗?您似乎正在为索引添加!
,而将其转换为字符串。
此外,您还没有关闭changeLights()
功能(您只关闭了if
条件。)
检查控制台,查看单击按钮时是否出现错误消息。
答案 2 :(得分:0)
firefox中的调试器会出现以下错误:
SyntaxError: expected expression, got keyword 'if' lights.html:27:4
有点神秘,所以我们看一下之前的内容,看看我们输入了!
而不是1
,所以我们修复它,然后再试一次。
SyntaxError: missing } after function body lights.html:32:25
所以我们修复它,然后再试一次。