0
我正在學習如何與Android Studio中創建應用程序, 一些錯誤,這是我的代碼來源:上創建應用程序
package com.example.droide;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class DroideSphereRuhlmannActivity extends Activity implements OnClickListener {
private TextView logview;
private Button connect, deconnect;
private ImageView forwardArrow, backArrow, rightArrow, leftArrow, stop, topleftArrow, toprightArrow, backleftArrow, backrightArrow;
private BluetoothAdapter mBluetoothAdapter = null;
private String[] logArray = null;
private BtInterface bt = null;
static final String TAG = "Droid";
static final int REQUEST_ENABLE_BT = 3;
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String data = msg.getData().getString("receivedData");
addToLog(data);
}
};
final Handler handlerStatus = new Handler() {
public void handleMessage(Message msg) {
int status = msg.arg1;
if(status == BtInterface.CONNECTED) {
addToLog("Connected");
} else if(status == BtInterface.DISCONNECTED) {
addToLog("Disconnected");
}
}
};
private void addToLog(String message){
for (int i = 1; i < logArray.length; i++){
logArray[i-1] = logArray[i];
}
logArray[logArray.length - 1] = message;
logview.setText("");
for (int i = 0; i < logArray.length; i++){
if (logArray[i] != null){
logview.append(logArray[i] + "\n");
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_remote);
logview = (TextView)findViewById(R.id.logview);
logArray = new String[3];
connect = (Button)findViewById(R.id.connect);
connect.setOnClickListener(this);
deconnect = (Button)findViewById(R.id.deconnect);
deconnect.setOnClickListener(this);
forwardArrow = (ImageView)findViewById(R.id.forward_arrow);
forwardArrow.setOnClickListener(this);
backArrow = (ImageView)findViewById(R.id.back_arrow);
backArrow.setOnClickListener(this);
rightArrow = (ImageView)findViewById(R.id.right_arrow);
rightArrow.setOnClickListener(this);
leftArrow = (ImageView)findViewById(R.id.left_arrow);
leftArrow.setOnClickListener(this);
stop = (ImageView)findViewById(R.id.stop);
stop.setOnClickListener(this);
topleftArrow = (ImageView)findViewById(R.id.topleft_arrow);
topleftArrow.setOnClickListener(this);
toprightArrow = (ImageView)findViewById(R.id.topright_arrow);
toprightArrow.setOnClickListener(this);
backrightArrow = (ImageView)findViewById(R.id.backright_arrow);
backrightArrow.setOnClickListener(this);
backleftArrow = (ImageView)findViewById(R.id.backleft_arrow);
backrightArrow.setOnClickListener(this);
}
@Override
public void onResume() {
super.onResume();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Log.v(TAG, "Device does not support Bluetooth");
}
else{
if (!mBluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else{
bt = new BtInterface(handlerStatus, handler);
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent moreData){
if (requestCode == REQUEST_ENABLE_BT){
if (resultCode == Activity.RESULT_OK){
bt = new BtInterface(handlerStatus, handler);
}
else if (resultCode == Activity.RESULT_CANCELED)
Log.v(TAG, "BT pas active");
else
Log.v(TAG, "code n'est pas valide");
}
else{
Log.v(TAG, "code n'est pas reconnu");
}
}
@Override
public void onClick(View v) {
if(v == connect) {
addToLog("Appareillage en cours");
bt.connect();
}
else if(v == deconnect) {
addToLog("connexion en cours");
bt.close();
}
else if(v == forwardArrow) {
bt.sendData("F");
}
else if(v == backArrow) {
bt.sendData("B");
}
else if(v == rightArrow) {
bt.sendData("R");
}
else if(v == leftArrow) {
bt.sendData("L");
}
else if(v == topleftArrow){
bt.sendData("I");
}
else if(v == toprightArrow){``
bt.sendData("J");
}
else if(v == backleftArrow){
bt.sendData("G");
}
else if(v ==backrightArrow){
bt.sendData("H");
}
else if(v == stop) {
bt.sendData("S");
}
錯誤日誌:
在那之後,我得到了一些錯誤,如:
"Error:(156, 10) error: reached end of file while parsing"
或
"Error:Execution failed for task ':app:compileDebugJavaWithJavac'."
所以現在我不現在如何解決這個問題。
請向我們展示確切的錯誤消息以及哪條chuffed導致它。 –
你的代碼最後缺少一些花括號。我建議你學習一些Java基礎知識,並從一個更簡單的應用程序開始。 –
我想同樣的事情,但我不知道我可以加上這些花括號 –