Android getActivity()方法返回null

Android getActivity()方法返回null,android,Android,我写了这段代码,但是getActivity方法返回null outonCreateView方法 public class HomeScreen extends Fragment { private Context context; ViewPager viewPager; GridView listGrid; Bitmap[] bitmaps ; LinearLayout indicator; Button first,second; String[] path ; TextView im

我写了这段代码,但是
getActivity
方法返回null out
onCreateView
方法

public class HomeScreen extends Fragment {

private Context context;

ViewPager viewPager;
GridView listGrid;

Bitmap[] bitmaps ;
LinearLayout indicator;
Button first,second;
String[] path ;
TextView imageTitle;

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

    View v = inflater.inflate(R.layout.home_screen, container, false);
    listGrid = (GridView) v.findViewById(R.id.grid_view);
    viewPager = (ViewPager)v.findViewById(R.id.view_pager);

    indicator = (LinearLayout)v.findViewById(R.id.indicator);
    first = (Button) v.findViewById(R.id.bfirst);
    second = (Button) v.findViewById(R.id.bsecond);
    imageTitle = (TextView) v.findViewById(R.id.myImageTitle);


    Typeface tf = Typeface.createFromAsset(HomeScreen.this.getActivity().getAssets(), "fonts/Medium.otf");

    imageTitle.setTypeface(tf);

    new GetCategories().execute();

    return v;
}

class GetCategories extends AsyncTask {

    @Override
    protected Object doInBackground(Object[] params) {

        JSONArray dataJsonArr = null;

        JsonParser jParser = new JsonParser();

        // get json string from url
        JSONObject json = jParser.getJSONFromUrl("http://192.168.88.12/index.php");

        try{
            // get the array of users
            dataJsonArr = json.getJSONArray("Users");

            //Arrays of data
            bitmaps = new Bitmap[dataJsonArr.length()];
            path = new String[dataJsonArr.length()];


            for (int i = 0; i < dataJsonArr.length(); i++) {

                JSONObject c = dataJsonArr.getJSONObject(i);

                InputStream in = new URL(c.getString("image")).openStream();
                bitmaps[i] = BitmapFactory.decodeStream(in);
                path[i] = c.getString("title");
            }

        }catch (JSONException e){
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Object o) {

        ListAdapter adapter=new ListAdapter(HomeScreen.this.getActivity(), path,bitmaps);
        listGrid.setAdapter(adapter);

        ImageAdapter sliderAdapter = new ImageAdapter(HomeScreen.this.getActivity(),bitmaps,indicator,first,second,imageTitle,path);
        viewPager.setAdapter(sliderAdapter);
    }
}

public void onItemClick(int mPosition){
    Log.i("Log:", " on item click : " + context);

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    context = activity;
}
}

您没有使用
片段事务
与您的
片段
交互。这意味着
FragmentManager
Fragment
生命周期中无法发挥作用,因此,您的
活动
,因为
片段
从未附加到
活动

,因为您的片段到目前为止未附加到任何活动

getActivity
onAttach(活动)
returs之前和
onDetach()之后返回
null
,最好检查片段的正确用法

另外,停止使用这种上下文引用

ListAdapter.this.getContext()
主屏幕。this.getActivity()
它们被删减为崩溃,远离android逻辑


为了避免这些问题,考虑使用<代码>加载程序< /代码>而不是<代码> AycCastase<代码>。这是一个很好的教程,说明加载程序是如何实现的:

何时/何处是<代码> OnItMeClinux(< /代码>)?我猜是在片段外部,最有可能是在其附加的分离生命周期之外?提供更多详细信息。从adapter类调用的onItemClick()显示视图列表@MHListAdapter=new ListAdapter(HomeScreen.this.getActivity(),路径,位图);setAdapter(适配器);这是从ListAdapter类@biegleuxLike@biegleux调用的onCreateView()onItemClick()中的行。请提供更多相关代码。否则我们无能为力。这将不起作用,因为
onCreateView()
当前甚至没有被调用。他只是通过调用
片段
的默认构造函数来创建一个新的
片段
。如果您查看源代码,默认构造函数不会与
片段的生命周期方法交互。发布的代码有这么多问题,所以我想这些都是好的想法开始。
public class ListAdapter extends ArrayAdapter {

private final Activity context;
private final String[] title;
private final Bitmap[] image;

public ListAdapter(Activity context, String[] title, Bitmap[] image) {
    super(context, R.layout.list_row, title);
    this.context=context;
    this.title=title;
    this.image=image;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.list_row, null, true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.text);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.image);

    txtTitle.setTypeface(Typeface.createFromAsset(ListAdapter.this.getContext().getAssets(), "fonts/Light.otf"));

    txtTitle.setText(title[position]);
    imageView.setImageBitmap(image[position]);

    rowView.setOnClickListener(new OnItemClickListener(position));

    return rowView;
}

/********* Called when Item click in ListView ************/
private class OnItemClickListener  implements View.OnClickListener {
    private int mPosition;

    OnItemClickListener(int position){
        mPosition = position;
    }

    @Override
    public void onClick(View v) {
        HomeScreen homeScreen = new HomeScreen();
        homeScreen.onItemClick(mPosition);

    }
}
}