【问题标题】:Multiple markers at this line - Syntax error on tokens, Annotation Name expected此行有多个标记 - 标记上的语法错误,需要注释名称
【发布时间】:2016-06-14 00:00:03
【问题描述】:

此行有多个标记
- 位置无法解析为变量
- 不能引用在不同方法中定义的内部类中的非最终变量项

int multiple=0;
ImageView imageView;
TextView tot_calories;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.items_details);
    Spinner mspin=(Spinner) findViewById(R.id.spinner1);
    Integer[] items = new Integer[]{1,2,3,4};

    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items);
    mspin.setAdapter(adapter);

    TextView name_select=(TextView)findViewById(R.id.SelectedName);
    name_select.setText(constant.food_items[constant.SelectedIndex]);
    imageView =(ImageView) findViewById(R.id.imagedetail);
    UpdateImage(constant.food_items[constant.SelectedIndex]);
    TextView calories=(TextView)findViewById(R.id.calories111);
    calories.setText(constant.food_calories[constant.index]+"");

    tot_calories=(TextView)findViewById(R.id.caloriestotal);
    mspin.setOnItemSelectedListener(new       AdapterView.OnItemSelectedListener()     {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
            // Your code here
            multiple=items[position]*constant.food_calories[constant.index];
        }
        public void onNothingSelected(AdapterView<?> adapterView) {
            return;
        }
    });
    tot_calories.setText(""+multiple);
}

【问题讨论】:

  • 您能发布您的完整课程/活动代码吗?

标签: java android syntax spinner


【解决方案1】:

用这个更新这行代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class itemsdetail extends Activity {

int multiple=0;
    ImageView imageView;
    TextView tot_calories;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.items_details);
        Spinner mspin=(Spinner) findViewById(R.id.spinner1);
        Integer[] items = new Integer[]{1,2,3,4};

        ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items);
        mspin.setAdapter(adapter);

        TextView name_select=(TextView)findViewById(R.id.SelectedName);
        name_select.setText(constant.food_items[constant.SelectedIndex]);
        imageView =(ImageView) findViewById(R.id.imagedetail);
        UpdateImage(constant.food_items[constant.SelectedIndex]);
        TextView calories=(TextView)findViewById(R.id.calories111);
        calories.setText(constant.food_calories[constant.index]+"");

        tot_calories=(TextView)findViewById(R.id.caloriestotal);
        mspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()     {
          public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
            // Your code here
            multiple=items[position]*constant.food_calories[constant.index];
          }
          public void onNothingSelected(AdapterView<?> adapterView) {
            return;
          } 
        });

        tot_calories.setText(""+multiple);


}   
}

祝你好运!

【讨论】:

  • 就在这一行之后 public void onNothingSelected(AdapterView> adapterView) { return; }
    添加
    });
  • 不能引用在不同方法@Avani中定义的内部类中的非最终变量倍数
  • 这是您的完整代码吗?如果是,则在末尾添加一个右括号}
  • 这个错误是不言自明的。您不能在另一个内部类(OnItemSelectedListener)中使用非最终变量。如果你想这样使用它,你必须将multiple 设为班级成员。
  • 声明变量 int multiple = 0;在 oncreate() 之前会解决你的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多