getApplicationContext返回null

时间:2016-03-07 01:01:52

标签: java android android-fragments android-context

我有以下片段。问题是,每次我启动此应用程序时崩溃都会出现以下异常:attempt to invoke virtual method android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext() on a null object reference。根据我的理解,getActivity被正确调用,但FragmentActivityTesting不会返回任何上下文,我不确定为什么会这样:

public class FragmentATesting extends Fragment {

@Override
     public void onAttach(Activity activity) {
    super.onAttach(activity);
}
@Override
public void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_a,container,false);

}
@Override
public void onActivityCreated(Bundle SavedInstanceState){
    super.onActivityCreated(SavedInstanceState);
}
@Override
public void onStart() {
    super.onStart();
    final SessionManager session = new SessionManager(getActivity().getApplicationContext());
    TextView username = (TextView) getView().findViewById(R.id.username_info_txt);
    HashMap<String,String> userString = session.getUserDetails();
    String usernamee = userString.get("username");
    username.setText(usernamee);
    vote(usernamee);
}
@Override
public void onResume() {
    super.onResume();
    final SessionManager session = new SessionManager(getActivity().getApplicationContext());
    TextView username = (TextView) getView().findViewById(R.id.username_info_txt);
    HashMap<String,String> userString = session.getUserDetails();
    String usernamee = userString.get("username");
    username.setText(usernamee);
    vote(usernamee);
}
@Override
public void onPause() {
    super.onPause();
}
@Override
public void onStop() {
    super.onStop();
}
@Override
public void onDestroyView() {
    super.onDestroyView();
}
@Override
public void onDestroy() {
    super.onDestroy();
}
private void vote(final String userId) {
    class UploadImage extends AsyncTask<String, Void, String> {

        RequestHandler rh = new RequestHandler();

        @Override
        protected String doInBackground(String... params) {
            HashMap<String, String> data = new HashMap<>();
            data.put("username",userId);
            String result = rh.sendPostRequest(SL_URL2, data);

            return result;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            JSONObject jsonObject = null;
            String likess = "";
            try {
                jsonObject = new JSONObject(s);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                likess = jsonObject.getString("amount");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            TextView amount = (TextView) getView().findViewById(R.id.likes_info_txt);
            amount.setText(likess + " Likes");

        }}
    UploadImage ui = new UploadImage();
    ui.execute();


}`

它在我的ViewPager中定义如下:

public class FragmentActivityTesting extends FragmentActivity {
ViewPager viewPager = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment_activity_testing);
    viewPager = (ViewPager) findViewById(R.id.pic_pager);
    setStatusBarColor();
    FragmentManager fragmentManager = getSupportFragmentManager();
    viewPager.setAdapter(new MyAdapter(fragmentManager));
    viewPager.setCurrentItem(1);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("CLOSE_ALL");
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };
    registerReceiver(broadcastReceiver, intentFilter);

}
@TargetApi(21)
public void setStatusBarColor() {
    Window window = this.getWindow();
    // clear FLAG_TRANSLUCENT_STATUS flag:
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

    if (Integer.valueOf(android.os.Build.VERSION.SDK) >= 21) {
        window.setStatusBarColor(this.getResources().getColor(R.color.green));

    }

}}

class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    if (position ==0) {
        fragment = new FragmentATesting();
    }
    else if (position == 1) {
        fragment = new FragmentBTesting();
    }
    else if (position == 2) {
        fragment = new FragmentCTesting();
    }
    return fragment;
}

@Override
public int getCount() {
    return 3;
}

}

0 个答案:

没有答案