【发布时间】:2015-05-06 15:53:38
【问题描述】:
我遇到了变量 c 的问题。一个错误:
错误:(34, 17) 错误: 无法为最终变量 c 赋值
一直让我很烦。我是一个新的 android studio 用户,我想解决这个问题。带有红色下划线的c 仅在我想要增加它的onClick 方法中。
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class plane extends ActionBarActivity {
public final int c=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_plane);
final ImageView img=(ImageView)findViewById(R.id.iv);
img.setImageResource(R.drawable.pe1);
Button next=(Button)findViewById(R.id.btn_next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(c==0){
img.setImageResource(R.drawable.pe2);
c=c+1;}
if (c==1){
img.setImageResource(R.drawable.pe3);
c=c+1;}
if (c==2){
img.setImageResource(R.drawable.pe4);
c=c+1;}
if (c==3){
img.setImageResource(R.drawable.pe5);
c=c+1;}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_plane, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
标签: java android variables android-studio increment