多个标记和语法错误

时间:2019-03-02 07:30:07

标签: android syntax-error

我真的对蚀和编程还很陌生。我刚刚开始做这个简单的项目,我有些困惑。 任何建议将不胜感激。它说我有多个错误,但是当我开始修复它时,我不知道从哪里开始。另一个新的语法错误弹出。

这是我的xml代码

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mod7.Main" >

    <TextView 
        android:id="@+id/txtResult" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="26dp" 
        android:text="" 
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/btnCost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="94dp"
        android:text="Compute" />

    <EditText
        android:id="@+id/txtTickets"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtResult"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="74dp"
        android:ems="10"
        android:hint="Number ofTickets" />

    <Spinner
        android:id="@+id/txtGroups"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtTickets"
        android:layout_centerHorizontal="true"
        android:entries="@array/Groups" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtResult"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="Ticket Vault"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

这是我的主要Java代码

package com.example.mod7;

import java.text.DecimalFormat;

import com.example.mod7.R;
import android.app.Activity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class Main extends Activity 

    double costPerTicket=59.99;
    int numberofTickets;
    double totalCost;
    String groupChoice; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText tickets= (EditText)findViewById(R.id.txtTickets);
    final Spinner group = (Spinner)findViewById(R.id.txtGroups);
    Button cost=(Button)findViewById(R.id.btnCost);
    final TextView result=(TextView)findViewById(R.id.txtResult);


    cost.setOnClickListener(new OnClickListener() {






    @Override
    public void onClick(view arg0) {
    numberofTickets=Integer.parseInt(tickets.getText().toString());
    totalCost=costPerTicket*numberofTickets;
    DecimalFormat currency=new DecimalFormat("$###,###.##");
    groupChoice=group.getSelectedItem().toString();
    result.setText("Total cost for"+ groupChoice + "is"+ currency.format(totalCost));
};

@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);
};

Here in my my main java, there are lots of errors. On my String=groupChoice it says" Syntax error insert } to complete classbody"

And I can't really fix them cause another error appears

2 个答案:

答案 0 :(得分:0)

活动后没有括号。应该是:

public class Main extends Activity {

;onCreate结束之后,您正在使用onOptionsItemSelected。您不必在方法末尾使用;

答案 1 :(得分:0)

您的代码中有许多基本语法错误。我建议您先学习Java。波纹管是您的代码的更正版本。

package com.example.mod7;

import java.text.DecimalFormat;

import com.example.mod7.R;
import android.app.Activity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class Main extends Activity { 

    double costPerTicket=59.99;
    int numberofTickets;
    double totalCost;
    String groupChoice; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText tickets= (EditText)findViewById(R.id.txtTickets);
        final Spinner group = (Spinner)findViewById(R.id.txtGroups);
        Button cost=(Button)findViewById(R.id.btnCost);
        final TextView result=(TextView)findViewById(R.id.txtResult);


        cost.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(view arg0) {
                numberofTickets=Integer.parseInt(tickets.getText().toString());
                totalCost=costPerTicket*numberofTickets;
                DecimalFormat currency=new DecimalFormat("$###,###.##");
                groupChoice=group.getSelectedItem().toString();
                result.setText("Total cost for"+ groupChoice + "is"+ currency.format(totalCost));
            }
        });
    }

    @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);
    }
}