片段Tabhost内部的嵌套片段在获取上下文时遇到问题。吹气

时间:2013-09-11 07:20:03

标签: android android-fragments nested android-context fragment-tab-host

我正在使用actionbarsherlocktabhost个片段,并在我的一个标签中显示一个名为segmentedControlFrag的片段,其中包含4个分段控制按钮(标记为“2小时”,“8小时” “,”24小时“和”1wk“)以及占据屏幕其余部分的框架。

segmentedcontrol组中的某个按钮被选中时,async请求会获取一些重要信息,该框架将替换为新的(visualFragmentFrag)片段,其中包含来自请求额外的。

它工作正常,我可以点击分段控制组中的每个按钮,它们各自加载自己的visualTrendsFrag变体就好了。我甚至可以选择一个完全不同的选项卡并返回它将加载我上次选择的分段控件就好了。

只有当我在onCreateView的{​​{1}}内添加此行代码时才会出现此问题:

segmentedControlFrag

当我添加这行代码时,logcat中的问题是所有与上下文相关的空指针错误。虽然如果我省略上面的那行确实有效,但我怀疑我在segmentText.check(R.id.button_one); 中管理片段的方式也是错误的。

segmentedControlFrag:

segmentedControlFrag

VisualTrendsFrag(frag嵌套在segmentedControlFrag框架内:

public class TrendsSegmentedControlFrame extends SherlockFragment {

 private ImageButton update_btn;
 private View view;
 private LayoutInflater myInflater;
 private ViewGroup myContainer;
 SegmentedRadioGroup segmentText;
 SegmentedRadioGroup segmentImg;
 private String hoursVar="2";
 private static final int TWO_HRS = 1;
 private static final int EIGHT_HRS = 2;
 private static final int TWENTYFOUR_HRS = 3;
 private static final int ONE_WEEK = 4;

    private int mTabState=0;



 @Override
 public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  Log.i("TrendsSegControlFrag", "getLayoutInflater");
  return super.getLayoutInflater(savedInstanceState);

 }
 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  Log.i("TrendsSegControlFrag", "onActivityCreated");
  super.onActivityCreated(savedInstanceState);
 }
 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  Log.i("TrendsSegControlFrag", "onCreate");
  super.onCreate(savedInstanceState);
 }
 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub
  Log.i("TrendsSegControlFrag", "onDestroy");
  super.onDestroy();

 }
 @Override
 public void onDestroyView() {
  // TODO Auto-generated method stub
  Log.i("TrendsSegControlFrag", "onDestroyView");
  super.onDestroyView();

 }

 @Override
 public void onPause() {
  Log.i("TrendsSegControlFrag", "onPause");
  // TODO Auto-generated method stub
  super.onPause();
 }
 @Override
 public void onResume() {
  Log.i("TrendsSegControlFrag", "onResume");
  // TODO Auto-generated method stub
  super.onResume();
 }
 @Override
 public void onSaveInstanceState(Bundle outState) {
  // TODO Auto-generated method stub
  Log.i("TrendsSegControlFrag", "onSaveInstanceState");
  super.onSaveInstanceState(outState);
 }
 @Override
 public void onStart() {
  // TODO Auto-generated method stub
  Log.i("TrendsSegControlFrag", "onStart");

  super.onStart();
 }
 @Override
 public void onStop() {
  Log.i("TrendsSegControlFrag", "onStop");
  // TODO Auto-generated method stub
  super.onStop();
 }
 @Override
 public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {


  view = inflater.inflate(R.layout.trends_segmented_control_frame, container, false);

  segmentText = (SegmentedRadioGroup) view.findViewById(R.id.segment_text);
  RadioButton btn = (RadioButton)view.findViewById(R.id.button_one);


  segmentText.setOnCheckedChangeListener(new OnCheckedChangeListener() {



   @Override
   public void onCheckedChanged(RadioGroup group, int checkedId) {
    Log.i("TrendsSegControlFrag-onCheckChangedCalled","");


    LocationInfo locationInfo = new LocationInfo(getActivity());
    String latitude = Float.toString(locationInfo.lastLat);
    String longitude = Float.toString(locationInfo.lastLong);


    if (checkedId == R.id.button_one) {
     Log.i("TrendsSegControlFrag-onCheckChanged","button_one | 2hrs");
     hoursVar="2";

     RequestParams params= new RequestParams();
     params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
     params.put("hours", hoursVar);
     params.put("long", longitude);
     params.put("lat", latitude);

     RestClient.post(TabHostFragmentActivity.token,TREND_COUNT_URL, params, new JsonHttpResponseHandler() {

      @Override
      public void onFailure(Throwable arg0, JSONObject arg1) {
       //NEED TO ADD CODE IN CASE OF FAIL

      }
      @Override
      public void onSuccess(JSONObject json) {

       String trend_count="0";
       int trend_count_int=0;
       try {
        trend_count=json.getString("trend_count");

       } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
       trend_count_int=Integer.parseInt(trend_count);
       if(trend_count_int>0){
        Log.i("hoursVar:"+hoursVar+" | trend_count:"+trend_count, "TrendsSegmentedContrlolFrag");
        Fragment visualTrends = new VisualTrendsFrag();
        Bundle args = new Bundle();
        args.putString("hoursVar", hoursVar);
        args.putString("trend_count", trend_count);
        visualTrends.setArguments(args);
        FragmentManager fm = getChildFragmentManager();

        if (fm != null) {
         FragmentTransaction ft = fm.beginTransaction();
         ft.replace(R.id.fragment_content, visualTrends);
         ft.commitAllowingStateLoss();
        }

       }else{
        Log.i("trends_count was 0", "TrendsSegmentedContrlolFrag");
       }
      }
     }); 
     //mToast.setText("Displaying all users you've been near in the last 2 hours");


    } else if (checkedId == R.id.button_two) {
     hoursVar="8";
     Log.i("TrendsSegControlFrag-onCheckChanged","button_two | 8hrs");


     RequestParams params= new RequestParams();
     params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
     params.put("hours", hoursVar);
     params.put("long", longitude);
     params.put("lat", latitude);

     RestClient.post(TabHostFragmentActivity.token,TREND_COUNT_URL, params, new JsonHttpResponseHandler() {

      @Override
      public void onFailure(Throwable arg0, JSONObject arg1) {
       //NEED TO ADD CODE IN CASE OF FAIL

      }
      @Override
      public void onSuccess(JSONObject json) {

       String trend_count="0";
       int trend_count_int=0;
       try {
        trend_count=json.getString("trend_count");

       } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
       trend_count_int=Integer.parseInt(trend_count);
       if(trend_count_int>0){
        Log.i("hoursVar:"+hoursVar+" | trend_count:"+trend_count, "TrendsSegmentedContrlolFrag");
        Fragment visualTrends = new VisualTrendsFrag();
        Bundle args = new Bundle();
        args.putString("hoursVar", hoursVar);
        args.putString("trend_count", trend_count);
        visualTrends.setArguments(args);
        FragmentManager fm = getChildFragmentManager();

        if (fm != null) {
         FragmentTransaction ft = fm.beginTransaction();
         ft.replace(R.id.fragment_content, visualTrends);
         ft.commitAllowingStateLoss();
        }

       }else{
        Log.i("trends_count was 0", "TrendsSegmentedContrlolFrag");
       }
      }
     }); 
     //mToast.setText("Displaying all users you've been near in the last 2 hours");




    } else if (checkedId == R.id.button_three) {
     hoursVar="24";


     Log.i("TrendsSegControlFrag-onCheckChanged","button_three | 24hrs");

     RequestParams params= new RequestParams();
     params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
     params.put("hours", hoursVar);
     params.put("long", longitude);
     params.put("lat", latitude);

     RestClient.post(TabHostFragmentActivity.token,TREND_COUNT_URL, params, new JsonHttpResponseHandler() {

      @Override
      public void onFailure(Throwable arg0, JSONObject arg1) {
       //NEED TO ADD CODE IN CASE OF FAIL

      }
      @Override
      public void onSuccess(JSONObject json) {

       String trend_count="0";
       int trend_count_int=0;
       try {
        trend_count=json.getString("trend_count");

       } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
       trend_count_int=Integer.parseInt(trend_count);
       if(trend_count_int>0){
        Log.i("hoursVar:"+hoursVar+" | trend_count:"+trend_count, "TrendsSegmentedContrlolFrag");

        Fragment visualTrends = new VisualTrendsFrag();
        Bundle args = new Bundle();
        args.putString("hoursVar", hoursVar);
        args.putString("trend_count", trend_count);
        visualTrends.setArguments(args);
        FragmentManager fm = getChildFragmentManager();

        if (fm != null) {
         FragmentTransaction ft = fm.beginTransaction();
         ft.replace(R.id.fragment_content, visualTrends);
         ft.commitAllowingStateLoss();
        }

       }else{
        Log.i("trends_count was 0", "TrendsSegmentedContrlolFrag");
       }
      }
     }); 
     //mToast.setText("Displaying all users you've been near in the last 2 hours");




    }else if (checkedId == R.id.button_four) {
     hoursVar="168";



     Log.i("TrendsSegControlFrag-onCheckChanged","button_four | 168hrs");

     RequestParams params= new RequestParams();
     params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
     params.put("hours", hoursVar);
     params.put("long", longitude);
     params.put("lat", latitude);

     RestClient.post(TabHostFragmentActivity.token,TREND_COUNT_URL, params, new JsonHttpResponseHandler() {

      @Override
      public void onFailure(Throwable arg0, JSONObject arg1) {
       //NEED TO ADD CODE IN CASE OF FAIL

      }
      @Override
      public void onSuccess(JSONObject json) {

       String trend_count="0";
       int trend_count_int=0;
       try {
        trend_count=json.getString("trend_count");

       } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
       trend_count_int=Integer.parseInt(trend_count);
       if(trend_count_int>0){
        Log.i("hoursVar:"+hoursVar+" | trend_count:"+trend_count, "TrendsSegmentedContrlolFrag");

        Fragment visualTrends = new VisualTrendsFrag();
        Bundle args = new Bundle();
        args.putString("hoursVar", hoursVar);
        args.putString("trend_count", trend_count);
        visualTrends.setArguments(args);
        FragmentManager fm = getChildFragmentManager();

        if (fm != null) {
         FragmentTransaction ft = fm.beginTransaction();
         ft.replace(R.id.fragment_content, visualTrends);
         ft.commitAllowingStateLoss();
        }

       }else{
        Log.i("trends_count was 0", "TrendsSegmentedContrlolFrag");
       }
      }
     }); 
     //mToast.setText("Displaying all users you've been near in the last 2 hours");




    }

   }
  });

  if (savedInstanceState == null) {
   segmentText.check(R.id.button_one);


        }


  //code goes here
  return view;
 }

}

编辑:我开始认为它不是一个上下文问题,因为它是一个LayoutInflator问题,这里是lib im使用它导致“下面的logcat中的popupwindow错误”: https://github.com/lorensiuswlt/NewQuickAction enter image description here

1 个答案:

答案 0 :(得分:0)

您正在编译哪个版本的Android?据我所知,4.2之前的版本不支持嵌套片段。