gpt4 book ai didi

android - 在 Android 的 Fragment 中获取应用程序上下文?

转载 作者:IT王子 更新时间:2023-10-28 23:51:02 28 4
gpt4 key购买 nike

我通过在一个 Activity 中使用应用程序上下文将一些数据存储到全局类。稍后我必须在 fragment 中检索这些值。我做了这样的事情来存储在 Global Class 中。

AndroidGlobalClass  AGC = ((AndroidGlobalClass) getApplicationContext());
AGC.setUser_access("XYZ");
AGC.setFirst_name("ABC");

在 list 中我已经完成了:

<application
android:name=".AndroidGlobalClass"
android:theme="@style/AppTheme" >
<activity
android:name="abc.SignInActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

现在当我尝试使用此获取应用程序上下文时...我没有获取上下文...

AndroidGlobalClass  AGC = ((AndroidGlobalClass) getApplicationContext());

这是我的 fragment Activity

public class Fragment_NewsFeed extends Fragment {
public Fragment_NewsFeed() {
}

RestImplimentationMethods RIM;
AndroidGlobalClass AGC;

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

最佳答案

您可以使用以下方式获取上下文getActivity().getApplicationContext();

关于android - 在 Android 的 Fragment 中获取应用程序上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20464273/

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