Eclipse for Android Application中的语法错误

时间:2014-09-19 21:05:55

标签: java android eclipse

我想我疯了,Eclipse抛出这些错误“语法错误,插入”;“完成语句”“语法错误,插入”)“完成表达式”“语法错误,插入”}“完成ClassBody “”语法错误,插入“}”来完成MethodBody“我似乎无法解决原因!

问:任何人都可以解决语法错误的原因或者Eclipse会说错误的原因吗?

注意:代码的想法是使用引用按下按钮后更改按钮上的文本:Change button text and action - android development

在我添加以下代码行后出现了这些错误:

/** Called when user clicks Start */
public void sendStart(View view) {
    // Do something in response to button
     final Button b_start = (Button) findViewById(R.id.b_start);
     b_start.setTag (1);
     b_start.setOnClickListener( new View.OnClickListener() {

    public void onClick(View v) {
    final int status =(Integer) v.getTag();
     if(status == 1) {
         b_start.setText("Stop");
         v.setTag(0); 
     } else {
         b_start.setText("Start");
         v.setTag(1); 
     }

    }

整个班级代码如下:

package com.example.rius;

import android.support.v7.app.ActionBarActivity;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; // view method to create methods
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;
import android.content.Intent; // Intent class for new activities (windows)

public class MainActivity extends ActionBarActivity {

 public final static String EXTRA_MESSAGE = "com.example.rius.MESSAGE"; // Key for intents extra data
 int set_state_start_one = 0;
 int set_state_start_two = 0;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


/** Called when user clicks RPM */
public void sendRPM(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, DisplayRPMActivity.class); // Created Intent to bring up new activity (RPM Screen)
    startActivity(intent); // Start a new activity (window)
}
/** Called when user clicks SPEED */
public void sendSpeed(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, DisplaySpeedActivity.class); // Created Intent to bring up new activity (RPM Screen)
    startActivity(intent); // Start a new activity (window)
}
/** Called when user clicks DIAGNOSTICS */
public void sendDiag(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, DisplayDiagnosticActivity.class); // Created Intent to bring up new activity (RPM Screen)
    startActivity(intent); // Start a new activity (window)
}
/** Called when user clicks THE TOGGEL BUTTON TO CONNECT */
public void startconnection(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {
        // Enable vibrate
        WifiConfiguration wificonfig = new WifiConfiguration();
        wificonfig.SSID = String.format("\"%s\"", "XXXXXX");
        wificonfig.preSharedKey = String.format("\"%s\"",  "XXXXXX");

        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        //Remember ID
        int netId = wifiManager.addNetwork(wificonfig);
        wifiManager.disconnect();
        wifiManager.enableNetwork(netId, true);
        wifiManager.reconnect();


    } else {
        // Disable vibrate
        WifiConfiguration wificonfig = new WifiConfiguration();
        wificonfig.SSID = String.format("\"%s\"", "XXXXXX");
        wificonfig.preSharedKey = String.format("\"%s\"",  "XXXXXX");
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        //Remember ID

        wifiManager.disconnect();
    }
} // togglebutton final bracket

/** Called when user clicks Start */
public void sendStart(View view) {
    // Do something in response to button
     final Button b_start = (Button) findViewById(R.id.b_start);
     b_start.setTag (1);
     b_start.setOnClickListener( new View.OnClickListener() {

    public void onClick(View v) {
    final int status =(Integer) v.getTag();
     if(status == 1) {
         b_start.setText("Stop");
         v.setTag(0); 
     } else {
         b_start.setText("Start");
         v.setTag(1); 
     }

    }





} // Final bracket

1 个答案:

答案 0 :(得分:2)

您错过了})的结束setOnClickListener

b_start.setOnClickListener( new View.OnClickListener() {
    public void onClick(View v) {
        final int status =(Integer) v.getTag();
        if(status == 1) {
            b_start.setText("Stop");
            v.setTag(0); 
        } else {
            b_start.setText("Start");
            v.setTag(1); 
        }
    }
});