getActivity()方法返回null

时间:2015-03-08 14:24:28

标签: android

我编写了此代码,但getActivity方法返回null 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;
}
}

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);

    }
}
}

2 个答案:

答案 0 :(得分:0)

您没有使用FragmentTransactionFragment进行互动。这意味着FragmentManager无法在Fragment生命周期中发挥作用,因此Activitynull,因为Fragment永远不会附于Activity

答案 1 :(得分:0)

这样做是因为到目前为止,您的片段未附加到任何活动。

getActivitynull returs之前返回onAttach(Activity),在onDetach()更好地结帐Fragment Documentation之后返回正确使用片段。

此外,请停止使用此类对上下文的引用

ListAdapter.this.getContext()HomeScreen.this.getActivity()他们修剪崩溃,远离Android逻辑。

要避免这些问题,请考虑使用Loader而不是AsyncTask这是一个很好的教程,如何加载器工作实现:https://stackoverflow.com/a/20991394/944070