這是我的代碼。在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