gpt4 book ai didi

java - Android应用程序,无法引用非最终变量,但又不想将其设为最终变量

转载 作者:行者123 更新时间:2023-12-02 05:54:05 26 4
gpt4 key购买 nike

您好 stackoverflow 用户,

我有一个问题,我有一个名为 counter 的变量,它通过 2 个按钮递增或递减,该变量用 2 个 TextViews 显示。问题是该变量需要设置为本地而不是全局(全局正在工作,但是我使用通货膨胀多次复制图层,如果他是全局的,当我增加或减少他在每个布局中修改的布局的值时)

错误是这样的:

Description Resource    Path    Location    Type
Cannot refer to a non-final variable counter inside an inner class defined in a different method Tabel.java /Instances_temperature/src/com/example/instances_temperature line 67 Java Problem

第 67 行是这样的:

if( mAutoIncrement && counter < 35)

如果我将变量设置为final,则无法通过递减(counter--)或递增(counter++)来访问它

希望我解释了该程序目前的工作原理,谢谢您的帮助。

下面是我的java文件代码:

public class Tabel extends ActionBarActivity {

int i;

int value; // Ignore this, this is the number that is taked from mainactivity for Inflation
//int counter=20; // counter default value for start = 20

static int REP_DELAY = 50; // Constant value for long click update
private Handler repeatUpdateHandler = new Handler();
private boolean mAutoIncrement = false;
private boolean mAutoDecrement = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabel);
Intent intentObject = getIntent();
value = intentObject.getIntExtra("max", 0);
LinearLayout layout = (LinearLayout)findViewById(R.id.container2);


for(i=1;i<=value;i++)
{
LayoutInflater layoutinflate = null;
layoutinflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowview = layoutinflate.inflate( R.layout.inflation_layout, null);

Button add,sub;
final TextView display;
final TextView showup;

int counter = 20;

add = (Button) rowview.findViewById(R.id.plus);
sub = (Button) rowview.findViewById(R.id.minus);
display = (TextView) rowview.findViewById(R.id.showtemp);
showup = (TextView) rowview.findViewById(R.id.showvalue);

class RptUpdater implements Runnable {
public void run() {
if( mAutoIncrement && counter < 35){
//increment();
if(counter<35)
{
counter++;
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C ");
}
else
{
Context context = getApplicationContext();
CharSequence text = "Maximum value is 35°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.TOP, 0, 100);
}
repeatUpdateHandler.postDelayed( new RptUpdater(), REP_DELAY );
}
else

if( mAutoDecrement && counter > 10){
// decrement();
if(counter>10)
{
counter--;
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C ");
repeatUpdateHandler.postDelayed( new RptUpdater(), REP_DELAY );
}
else
{
Context context = getApplicationContext();
CharSequence text = "Minimum value is 10°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.BOTTOM, 0, 50);
}
}

}
}


showup.setText(" "+counter+"°C ");
display.setText(""+counter+"°C");
add.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
if(counter<35){
counter++;
display.setText( "" + counter+"°C ");
showup.setText(" "+counter+"°C ");
}
else{
Context context = getApplicationContext();
CharSequence text = "Maximum value is 35°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.TOP, 0, 100);
}
}
});

sub.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

if(counter>10){
counter--;
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C ");
}
else{
Context context = getApplicationContext();
CharSequence text = "Minimum value is 10°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.BOTTOM, 0, 50);
}
}

});
add.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
mAutoIncrement = true;
repeatUpdateHandler.post(new RptUpdater() );
return false;
}
});
add.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoIncrement )
mAutoIncrement = false;
return false;
}
});
sub.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
mAutoDecrement = true;
repeatUpdateHandler.post(new RptUpdater() );
return false;
}
});
sub.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoDecrement )
mAutoDecrement = false;
return false;
}
});



layout.addView(rowview);
}



//showvalue.setText(String.valueOf(getIntent().getExtras().getInt("max")));


if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tabel, 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);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabel,
container, false);
return rootView;
}
}

protected void onSaveInstanceState(Bundle savedInstance) {
super.onSaveInstanceState(savedInstance);
//savedInstance.putInt("myCounter",counter);
}

}

最佳答案

如果你想要一个肮脏的修复。将 counter 设置为 final int[] counter = {20},然后当您想访问它时,只需调用 counter[0]

关于java - Android应用程序,无法引用非最终变量,但又不想将其设为最终变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23265005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com