这是我的代码。在onClick方法中,我想刷新textView。所以每次点击按钮时,该方法都会调用int value = SecondFragment.getSmashValue();
。从那里我得到了我在另一个片段中设置的价值。我必须在我的我的onCreate方法中设置的TextView中使用这个变量。但它总是告诉我值0!我认为它显示0,因为我试图使全局变量和全局值为0.如何将我的本地变量用作全局变量?
那么,如何让这个变量在不丢失其值的情况下在全局变量?
import com.example.viewpagertest2.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Tab1Activity extends Fragment {
Button b1;
static int theValue;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.firsttabview, container, false);
b1 = (Button) rootView.findViewById(R.id.refreshButton1);
b1.setOnClickListener(handler);
TextView tv = (TextView) rootView.findViewById(R.id.SP);
tv.setText("" + theValue);
b99.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
theValue = SecondFragment.getSmashValue();
tv.setText("" + theValue);
}
});
return rootView;
}
是的,你是对的,但这不是问题,我开始认为我的变量分配 – gewhjhfkgad 2014-09-29 08:51:58
5月失败如果你正在做的错误是,在获得值的价值之后,你再次用“theValue”变量初始化它。因此它取得了“theValue”变量值。你这样做:theValue = value; – 2014-09-29 08:57:17
是的,这是真的! @Seçkin说也是,我将它改为'theValue = ....'但它仍然不起作用,也许我必须过时我的变量赋值 – gewhjhfkgad 2014-09-29 09:01:18