非法开始表达(Android)

时间:2015-12-28 08:55:09

标签: java android android-studio

学习编码..有人可以帮助我吗

关于,获取用户输入然后吐司作为输出

public class MainActivity extends ActionBarActivity {

private EditText input;
private Button output;


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

    addListenerOnButton();
}

public void addListenerOnButton() {
    input = (EditText) findViewById(R.id.editText input);
    output = (Button) findViewById(R.id.button output);
    output.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast toast;
            toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
            toast.makeText(MainActivity.this, input.getText(), toast.LENGTH_SHORT).show();
        }
    });
}

虽然编译这个..am得到错误非法开始表达

C:\Users\VG\AndroidStudioProjects\MakeToast\app\src\main\java\com\example\vg\maketoast\MainActivity.java
Error:(29, 54) error: ')' expected
Error:(29, 60) error: illegal start of expression
Error:(30, 51) error: ')' expected
Error:(30, 58) error: illegal start of expression
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

6 个答案:

答案 0 :(得分:1)

input = (EditText) findViewById(R.id.editText input);
output = (Button) findViewById(R.id.button output);


input = (EditText) findViewById(R.id.editText /*input*/);
output = (Button) findViewById(R.id.button /*output*/);

答案 1 :(得分:1)

使用以下代码:

活动代码:

public class MainActivity extends Activity {
    private EditText input;
    private Button output;

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


    public void addListenerOnButton() {
        input = (EditText) findViewById(R.id.input);
        output = (Button) findViewById(R.id.output);
        output.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast;
                toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
                toast.makeText(MyActivity.this, input.getText(), toast.LENGTH_SHORT).show();
            }
        });
    }
}

<强> activity_my.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="myText" />

    <Button
        android:id="@+id/output"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/input"
        android:text="Button Output" />

</RelativeLayout>

答案 2 :(得分:1)

指教

请勿使用ActionBarActivity。自版本22.1.0起,不推荐使用类 ActionBarActivity 。您应该使用 AppCompatActivity

您无法在xml ID部分中添加空格

错误的概念

R.id.editText input

必须是(Just Remove Space)

R.id.editTextinput

最后

input = (EditText) findViewById(R.id.editTextinput);
output = (Button) findViewById(R.id.buttonoutput);

答案 3 :(得分:0)

你的错误在下面的两行中提到了正确的ID。

input = (EditText) findViewById(R.id.editText input);
output = (Button) findViewById(R.id.button output);

答案 4 :(得分:0)

input =(EditText)findViewById(R.id.editText输入); 输出=(按钮)findViewById(R.id.button输出);

input =(EditText)findViewById(R.id.editText / input /); 输出=(按钮)findViewById(R.id.button / 输出 /);

答案 5 :(得分:0)

<noscript><p title="</noscript><img src=x onerror=prompt(document.domain)>"><noscript><p title="</noscript><img src=x onerror=prompt(document.domain)>">

    <noscript><p title="</noscript><img src=x onerror=prompt(document.domain)>">