Android 片段中onCreateView方法外部的上下文更改为null

Android 片段中onCreateView方法外部的上下文更改为null,android,android-fragments,Android,Android Fragments,我对安卓系统比较陌生。我正在编写一个应用程序,它在主要活动中使用了一个片段。该片段还充当位置侦听器,并实现onLocationChange方法。这是因为,基于位置更改,新的文本视图将添加到此片段中 片段中的onCreateView方法创建了其滚动视图,并将其添加到主活动中。在相同的onCreateView方法中,我还捕获了片段的布局xml中存在的上下文和线性布局标记,我想在其中插入在onLocationChange方法中创建的文本视图 我看到的问题是onLocationChanged方法和upd

我对安卓系统比较陌生。我正在编写一个应用程序,它在主要活动中使用了一个片段。该片段还充当位置侦听器,并实现onLocationChange方法。这是因为,基于位置更改,新的文本视图将添加到此片段中

片段中的onCreateView方法创建了其滚动视图,并将其添加到主活动中。在相同的onCreateView方法中,我还捕获了片段的布局xml中存在的上下文和线性布局标记,我想在其中插入在onLocationChange方法中创建的文本视图

我看到的问题是onLocationChanged方法和updateView方法中的上下文和布局值为null。使用调试器,我确认它们仅在onCreateMethod中具有非null值

以下是fragment类的代码片段:

public class ResultsFragment extends Fragment implements LocationListener,OnCompleteListener {

private LoadPlaces loadPlaces;
HashMap<String, Double> map=new HashMap<String, Double>();
LinearLayout layout;
Context context;
Activity activity;
ScrollView view;

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    view =(ScrollView)inflater.inflate(R.layout.scroll_view, container, false);
    layout=(LinearLayout)view.findViewById(R.id.resultsView);
    context=view.getContext();

    return view;
 }

@Override
public void onLocationChanged(final Location location) {
    loadPlaces=new LoadPlaces(context); //context is null here
    loadPlaces.setOnCompleteListener(this);
    loadPlaces.execute(String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude()));
}

private void updateResultView(){
    String status=loadPlaces.getResultStatus();
    if("OK".equals(status)){
        for (Place p:loadPlaces.getPlacesResult()){
            TextView txt=new TextView(context); //context is null here
            txt.setText(p.name);
            txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            layout.addView(txt); //layout is null here
        }   
    }
}


@Override
public void onComplete(String result) {
    updateResultView();
}
公共类ResultsFragment扩展片段实现LocationListener、OnCompleteListener{
私人载货处;
HashMap=newHashMap();
线性布局;
语境;
活动;
滚动视图;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=(滚动视图)充气器。充气(R.layout.scroll\u视图,容器,false);
布局=(LinearLayout)视图。findViewById(R.id.resultsView);
context=view.getContext();
返回视图;
}
@凌驾
更改了公共无效位置(最终位置){
loadPlaces=newloadplaces(context);//这里的context为null
loadPlaces.setOnCompleteListener(此);
执行(String.valueOf(location.getLatitude()),String.valueOf(location.getLatitude());
}
私有void updateResultView(){
String status=loadPlaces.getResultStatus();
如果(“确定”。等于(状态)){
对于(位置p:loadPlaces.getPlacesResult()){
TextView txt=new TextView(context);//此处的context为null
txt.setText(p.name);
setLayoutParams(新的LayoutParams(LayoutParams.FILL\u PARENT,LayoutParams.WRAP\u CONTENT));
layout.addView(txt);//此处的布局为空
}   
}
}
@凌驾
未完成的公共void(字符串结果){
updateResultView();
}
以下是片段的布局xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/results"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:clickable="true"
android:background="@color/grey"
android:layout_marginTop="25dip">

<LinearLayout android:id="@+id/resultsView"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">                  
 </LinearLayout>

这是主要活动的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="30dip" 
android:layout_gravity="center"
android:background="@color/background">
 <fragment android:name="com.app.xxx.ResultsFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="fill_parent"
          android:layout_height="0dp"/>

仅供参考,保存片段的活动从未暂停/终止。当定位服务在后台运行时,它始终可见。因此,在此过程中,我认为片段不会被破坏

我是否错误地处理了这个问题?是否有更好的方法来访问或保存片段的上下文并在以后使用它?任何提示都会有所帮助

提前感谢。

更改为:

@Override
public void onLocationChanged(final Location location) {
    if(getActivity()!=null){
    loadPlaces=new LoadPlaces(getActivity()); //context is null here
    loadPlaces.setOnCompleteListener(this);
    loadPlaces.execute(String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude()));
    }
}
问题是在
onCreateView
之前调用位置更改侦听器


您可以忽略所有onLocationChanged调用,直到使用上述函数将片段附加到活动。

这是错误的context=view.getContext();应该是context=getActivity();但是为什么需要
上下文上下文
活动活动活动
活动
是一个
上下文
对吧…?我知道活动是一个上下文,这是我拥有view.getContext的原因,而活动和上下文的两个独立变量仅用于测试目的,因为我得到了空值。我将只使用最终他们中的一个。
public void onAttach(android.app.Activity activity) {
    super.onAttach(activity);
    context = (Context)getActivity();
};