簡體   English   中英

從一個類到另一個類的靜態變量的訪問

[英]Access of static variables from one class to another

我很難理解靜態變量。 任何人都可以解釋為什么當我在 FragmentJoy 中調用變量“startingDate”時,它沒有返回正確的值? 假設所有其余代碼都運行良好,不是 NULL 值並且 databaseCategories 運行良好。 如何在“onDataChange()”方法中保存一個值並在 FragmentJoy 中使用它?

我無法刪除:

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {)

這是儀表板活動

public class Dashboard extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

static String startingDate;
DatabaseReference databaseCategories;
ArrayList<Category> currentJoyCategories;


User currentUser; //holds the information of the current user


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    . . . . . .

    databaseCategories = FirebaseDatabase.getInstance().getReference("Categories");

    currentJoyCategories = new ArrayList<>();

    databaseCategories.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

               //want to do
               startingDate = "some string";
         }

         @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
} //end of Dashboard class

在另一堂課中,我想做:

public class FragmentJoy extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_joy,container,false);

         //not storing the string "some string"
        System.out.println("testing " + Dashboard.startingDate);

        return view;

} //end of onCreateView method
} //end of class

您需要將startingDate聲明為public static String startingDate

用這個

public static String startingDate;

而不是這個

static String startingDate;

然后你可以像這樣在你的FragmentJoy使用它

Log.e("testing ",Dashboard.startingDate);

在這里,我向您展示了如何將一個class靜態方法用於另一個類。

例如,我在HomeFragment類中定義了一個靜態方法,

公共類 HomeFragment 擴展 BaseFragment {

    Unbinder unbinder;
    int mode;

    public static HomeFragment getInstance(int mode) {
        HomeFragment homeFragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(KeyUtil.KEY_MODE, mode);
        homeFragment.setArguments(bundle);
        return homeFragment;
    }
    ....................
}

而且,我在我的MainActivity類中使用了這個static方法,

public class MainActivity extends AppCompatActivity {

    ...........................

        /**
         * Get the fragment while need to push.
         *
         * @return: fragment
         */
        private Fragment openFragment() {
            switch (navItemIndex) {
                case 0:
                    // Here I have called static method
                    return HomeFragment.getInstance(0);
                }
         }

        ........................
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM